├── .DS_Store ├── .gitignore ├── Application ├── .DS_Store ├── Application.iml ├── build.gradle ├── src │ ├── .DS_Store │ └── main │ │ ├── .DS_Store │ │ ├── AndroidManifest.xml │ │ ├── java │ │ ├── .DS_Store │ │ └── com │ │ │ ├── .DS_Store │ │ │ └── example │ │ │ ├── .DS_Store │ │ │ └── android │ │ │ ├── .DS_Store │ │ │ └── camera2basic │ │ │ ├── .DS_Store │ │ │ ├── AutoFitTextureView.java │ │ │ ├── Camera2BasicFragment.java │ │ │ └── CameraActivity.java │ │ └── res │ │ ├── .DS_Store │ │ ├── drawable-hdpi │ │ ├── ic_action_info.png │ │ ├── ic_launcher.png │ │ └── tile.9.png │ │ ├── drawable-mdpi │ │ ├── ic_action_info.png │ │ └── ic_launcher.png │ │ ├── drawable-xhdpi │ │ ├── ic_action_info.png │ │ └── ic_launcher.png │ │ ├── drawable-xxhdpi │ │ ├── ic_action_info.png │ │ └── ic_launcher.png │ │ ├── layout-land │ │ └── fragment_camera2_basic.xml │ │ ├── layout │ │ ├── activity_camera.xml │ │ └── fragment_camera2_basic.xml │ │ ├── values-sw600dp │ │ ├── template-dimens.xml │ │ └── template-styles.xml │ │ ├── values-v11 │ │ └── template-styles.xml │ │ ├── values-v21 │ │ ├── base-colors.xml │ │ └── base-template-styles.xml │ │ └── values │ │ ├── base-strings.xml │ │ ├── colors.xml │ │ ├── strings.xml │ │ ├── styles.xml │ │ ├── template-dimens.xml │ │ └── template-styles.xml └── tests │ ├── AndroidManifest.xml │ └── src │ └── com │ └── example │ └── android │ └── camera2basic │ └── tests │ └── SampleTests.java ├── CONTRIB.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── READMEs.md ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── packaging.yaml ├── screenshots ├── .DS_Store ├── icon-web.png └── main.png └── settings.gradle /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SundayLab/Camera2BasicStream/8a961bb5a2bbabf7c154ed25afbfb017c73f561d/.DS_Store -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | 15 | # Gradle files 16 | .gradle/ 17 | build/ 18 | /*/build/ 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Log Files 27 | *.log 28 | 29 | .idea/ 30 | Camera2BasicStream.iml 31 | -------------------------------------------------------------------------------- /Application/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SundayLab/Camera2BasicStream/8a961bb5a2bbabf7c154ed25afbfb017c73f561d/Application/.DS_Store -------------------------------------------------------------------------------- /Application/Application.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | -------------------------------------------------------------------------------- /Application/build.gradle: -------------------------------------------------------------------------------- 1 | 2 | buildscript { 3 | repositories { 4 | jcenter() 5 | maven { 6 | url 'https://maven.google.com/' 7 | name 'Google' 8 | } 9 | google() 10 | } 11 | 12 | dependencies { 13 | classpath 'com.android.tools.build:gradle:3.1.1' 14 | } 15 | } 16 | 17 | apply plugin: 'com.android.application' 18 | 19 | repositories { 20 | jcenter() 21 | maven { 22 | url 'https://maven.google.com/' 23 | name 'Google' 24 | } 25 | } 26 | 27 | dependencies { 28 | implementation "com.android.support:support-v4:27.1.1" 29 | implementation "com.android.support:support-v13:27.1.1" 30 | implementation "com.android.support:cardview-v7:27.1.1" 31 | implementation 'com.android.support:appcompat-v7:27.1.1' 32 | } 33 | 34 | // The sample build uses multiple directories to 35 | // keep boilerplate and common code separate from 36 | // the main sample code. 37 | List dirs = [ 38 | 'main', // main sample code; look here for the interesting stuff. 39 | 'common', // components that are reused by multiple samples 40 | 'template'] // boilerplate code that is generated by the sample template process 41 | 42 | android { 43 | compileSdkVersion 27 44 | buildToolsVersion '27.0.3' 45 | 46 | defaultConfig { 47 | minSdkVersion 21 48 | targetSdkVersion 27 49 | } 50 | 51 | compileOptions { 52 | sourceCompatibility JavaVersion.VERSION_1_7 53 | targetCompatibility JavaVersion.VERSION_1_7 54 | } 55 | 56 | sourceSets { 57 | main { 58 | dirs.each { dir -> 59 | java.srcDirs "src/${dir}/java" 60 | res.srcDirs "src/${dir}/res" 61 | } 62 | } 63 | androidTest.setRoot('tests') 64 | androidTest.java.srcDirs = ['tests/src'] 65 | 66 | } 67 | 68 | } 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | -------------------------------------------------------------------------------- /Application/src/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SundayLab/Camera2BasicStream/8a961bb5a2bbabf7c154ed25afbfb017c73f561d/Application/src/.DS_Store -------------------------------------------------------------------------------- /Application/src/main/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SundayLab/Camera2BasicStream/8a961bb5a2bbabf7c154ed25afbfb017c73f561d/Application/src/main/.DS_Store -------------------------------------------------------------------------------- /Application/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 39 | 40 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /Application/src/main/java/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SundayLab/Camera2BasicStream/8a961bb5a2bbabf7c154ed25afbfb017c73f561d/Application/src/main/java/.DS_Store -------------------------------------------------------------------------------- /Application/src/main/java/com/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SundayLab/Camera2BasicStream/8a961bb5a2bbabf7c154ed25afbfb017c73f561d/Application/src/main/java/com/.DS_Store -------------------------------------------------------------------------------- /Application/src/main/java/com/example/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SundayLab/Camera2BasicStream/8a961bb5a2bbabf7c154ed25afbfb017c73f561d/Application/src/main/java/com/example/.DS_Store -------------------------------------------------------------------------------- /Application/src/main/java/com/example/android/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SundayLab/Camera2BasicStream/8a961bb5a2bbabf7c154ed25afbfb017c73f561d/Application/src/main/java/com/example/android/.DS_Store -------------------------------------------------------------------------------- /Application/src/main/java/com/example/android/camera2basic/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SundayLab/Camera2BasicStream/8a961bb5a2bbabf7c154ed25afbfb017c73f561d/Application/src/main/java/com/example/android/camera2basic/.DS_Store -------------------------------------------------------------------------------- /Application/src/main/java/com/example/android/camera2basic/AutoFitTextureView.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.example.android.camera2basic; 18 | 19 | import android.content.Context; 20 | import android.util.AttributeSet; 21 | import android.view.TextureView; 22 | 23 | /** 24 | * A {@link TextureView} that can be adjusted to a specified aspect ratio. 25 | */ 26 | public class AutoFitTextureView extends TextureView { 27 | 28 | private int mRatioWidth = 0; 29 | private int mRatioHeight = 0; 30 | 31 | public AutoFitTextureView(Context context) { 32 | this(context, null); 33 | } 34 | 35 | public AutoFitTextureView(Context context, AttributeSet attrs) { 36 | this(context, attrs, 0); 37 | } 38 | 39 | public AutoFitTextureView(Context context, AttributeSet attrs, int defStyle) { 40 | super(context, attrs, defStyle); 41 | } 42 | 43 | /** 44 | * Sets the aspect ratio for this view. The size of the view will be measured based on the ratio 45 | * calculated from the parameters. Note that the actual sizes of parameters don't matter, that 46 | * is, calling setAspectRatio(2, 3) and setAspectRatio(4, 6) make the same result. 47 | * 48 | * @param width Relative horizontal size 49 | * @param height Relative vertical size 50 | */ 51 | public void setAspectRatio(int width, int height) { 52 | if (width < 0 || height < 0) { 53 | throw new IllegalArgumentException("Size cannot be negative."); 54 | } 55 | mRatioWidth = width; 56 | mRatioHeight = height; 57 | requestLayout(); 58 | } 59 | 60 | @Override 61 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 62 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); 63 | int width = MeasureSpec.getSize(widthMeasureSpec); 64 | int height = MeasureSpec.getSize(heightMeasureSpec); 65 | if (0 == mRatioWidth || 0 == mRatioHeight) { 66 | setMeasuredDimension(width, height); 67 | } else { 68 | if (width < height * mRatioWidth / mRatioHeight) { 69 | setMeasuredDimension(width, width * mRatioHeight / mRatioWidth); 70 | } else { 71 | setMeasuredDimension(height * mRatioWidth / mRatioHeight, height); 72 | } 73 | } 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /Application/src/main/java/com/example/android/camera2basic/Camera2BasicFragment.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.example.android.camera2basic; 18 | 19 | import android.Manifest; 20 | import android.Manifest.permission; 21 | import android.app.Activity; 22 | import android.app.AlertDialog; 23 | import android.app.Dialog; 24 | import android.content.Context; 25 | import android.content.DialogInterface; 26 | import android.content.pm.PackageManager; 27 | import android.content.res.Configuration; 28 | import android.graphics.Bitmap; 29 | import android.graphics.BitmapFactory; 30 | import android.graphics.ImageFormat; 31 | import android.graphics.Matrix; 32 | import android.graphics.RectF; 33 | import android.graphics.SurfaceTexture; 34 | import android.hardware.camera2.CameraAccessException; 35 | import android.hardware.camera2.CameraCaptureSession; 36 | import android.hardware.camera2.CameraCharacteristics; 37 | import android.hardware.camera2.CameraDevice; 38 | import android.hardware.camera2.CameraManager; 39 | import android.hardware.camera2.CameraMetadata; 40 | import android.hardware.camera2.CaptureRequest; 41 | import android.hardware.camera2.CaptureResult; 42 | import android.hardware.camera2.TotalCaptureResult; 43 | import android.hardware.camera2.params.StreamConfigurationMap; 44 | import android.media.Image; 45 | import android.media.ImageReader; 46 | 47 | import android.os.Bundle; 48 | import android.os.Handler; 49 | import android.os.HandlerThread; 50 | import android.support.annotation.NonNull; 51 | import android.support.v4.app.ActivityCompat; 52 | import android.support.v4.app.DialogFragment; 53 | import android.support.v4.app.Fragment; 54 | import android.util.Log; 55 | import android.util.Size; 56 | import android.util.SparseIntArray; 57 | import android.view.LayoutInflater; 58 | import android.view.Surface; 59 | import android.view.TextureView; 60 | import android.view.View; 61 | import android.view.ViewGroup; 62 | import android.widget.Toast; 63 | import java.io.ByteArrayOutputStream; 64 | import java.io.File; 65 | import java.io.FileOutputStream; 66 | import java.io.IOException; 67 | import java.net.DatagramPacket; 68 | import java.net.DatagramSocket; 69 | import java.net.InetAddress; 70 | import java.nio.ByteBuffer; 71 | import java.util.ArrayList; 72 | import java.util.Arrays; 73 | import java.util.Collections; 74 | import java.util.Comparator; 75 | import java.util.List; 76 | import java.util.concurrent.Semaphore; 77 | import java.util.concurrent.TimeUnit; 78 | 79 | public class Camera2BasicFragment extends Fragment 80 | implements View.OnClickListener { 81 | 82 | /** 83 | * Conversion from screen rotation to JPEG orientation. 84 | */ 85 | private static final SparseIntArray ORIENTATIONS = new SparseIntArray(); 86 | private static final int REQUEST_CAMERA_PERMISSION = 1; 87 | private static final String FRAGMENT_DIALOG = "dialog"; 88 | 89 | static { 90 | ORIENTATIONS.append(Surface.ROTATION_0, 90); 91 | ORIENTATIONS.append(Surface.ROTATION_90, 0); 92 | ORIENTATIONS.append(Surface.ROTATION_180, 270); 93 | ORIENTATIONS.append(Surface.ROTATION_270, 180); 94 | } 95 | 96 | /** 97 | * Tag for the {@link Log}. 98 | */ 99 | private static final String TAG = "Camera2BasicFragment"; 100 | 101 | /** 102 | * Camera state: Showing camera preview. 103 | */ 104 | private static final int STATE_PREVIEW = 0; 105 | 106 | /** 107 | * Camera state: Waiting for the focus to be locked. 108 | */ 109 | private static final int STATE_WAITING_LOCK = 1; 110 | 111 | /** 112 | * Camera state: Waiting for the exposure to be precapture state. 113 | */ 114 | private static final int STATE_WAITING_PRECAPTURE = 2; 115 | 116 | /** 117 | * Camera state: Waiting for the exposure state to be something other than precapture. 118 | */ 119 | private static final int STATE_WAITING_NON_PRECAPTURE = 3; 120 | 121 | /** 122 | * Camera state: Picture was taken. 123 | */ 124 | private static final int STATE_PICTURE_TAKEN = 4; 125 | 126 | /** 127 | * {@link TextureView.SurfaceTextureListener} handles several lifecycle events on a 128 | * {@link TextureView}. 129 | */ 130 | private final TextureView.SurfaceTextureListener mSurfaceTextureListener 131 | = new TextureView.SurfaceTextureListener() { 132 | 133 | @Override 134 | public void onSurfaceTextureAvailable(SurfaceTexture texture, int width, int height) { 135 | openCameraOrRequestPermission(width, height); 136 | } 137 | 138 | @Override 139 | public void onSurfaceTextureSizeChanged(SurfaceTexture texture, int width, int height) { 140 | configureTransform(width, height); 141 | } 142 | 143 | @Override 144 | public boolean onSurfaceTextureDestroyed(SurfaceTexture texture) { 145 | return true; 146 | } 147 | 148 | @Override 149 | public void onSurfaceTextureUpdated(SurfaceTexture texture) { 150 | 151 | } 152 | 153 | }; 154 | 155 | /** 156 | * ID of the current {@link CameraDevice}. 157 | */ 158 | private String mCameraId; 159 | 160 | /** 161 | * An {@link AutoFitTextureView} for camera preview. 162 | */ 163 | private AutoFitTextureView mTextureView; 164 | 165 | /** 166 | * A {@link CameraCaptureSession } for camera preview. 167 | */ 168 | private CameraCaptureSession mCaptureSession; 169 | 170 | /** 171 | * A reference to the opened {@link CameraDevice}. 172 | */ 173 | private CameraDevice mCameraDevice; 174 | 175 | /** 176 | * The {@link android.util.Size} of camera preview. 177 | */ 178 | private Size mPreviewSize; 179 | 180 | /** 181 | * {@link CameraDevice.StateCallback} is called when {@link CameraDevice} changes its state. 182 | */ 183 | private final CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() { 184 | 185 | @Override 186 | public void onOpened(@NonNull CameraDevice cameraDevice) { 187 | // This method is called when the camera is opened. We start camera preview here. 188 | mCameraOpenCloseLock.release(); 189 | mCameraDevice = cameraDevice; 190 | createCameraPreviewSession(); 191 | 192 | 193 | } 194 | 195 | @Override 196 | public void onDisconnected(@NonNull CameraDevice cameraDevice) { 197 | /* mCameraOpenCloseLock.release(); 198 | cameraDevice.close(); 199 | mCameraDevice = null; */ 200 | } 201 | 202 | @Override 203 | public void onError(@NonNull CameraDevice cameraDevice, int error) { 204 | /* (mCameraOpenCloseLock.release(); 205 | cameraDevice.close(); 206 | mCameraDevice = null; 207 | Activity activity = getActivity(); 208 | if (null != activity) { 209 | activity.finish(); 210 | }*/ 211 | } 212 | 213 | }; 214 | 215 | /** 216 | * An additional thread for running tasks that shouldn't block the UI. 217 | */ 218 | private HandlerThread mBackgroundThread; 219 | 220 | /** 221 | * A {@link Handler} for running tasks in the background. 222 | */ 223 | private Handler mBackgroundHandler; 224 | 225 | /** 226 | * An {@link ImageReader} that handles still image capture. 227 | */ 228 | private ImageReader mImageReader; 229 | 230 | /** 231 | * This is the output file for our picture. 232 | */ 233 | private File mFile; 234 | 235 | /** 236 | * This a callback object for the {@link ImageReader}. "onImageAvailable" will be called when a 237 | * still image is ready to be saved. 238 | */ 239 | private final ImageReader.OnImageAvailableListener mOnImageAvailableListener 240 | = new ImageReader.OnImageAvailableListener() { 241 | 242 | @Override 243 | public void onImageAvailable(ImageReader reader) { 244 | 245 | Image test = null; 246 | 247 | try { 248 | if (reader != null) { 249 | test = reader.acquireLatestImage(); 250 | } 251 | 252 | } catch (IllegalStateException e) { 253 | System.out.println("whoops"); 254 | } 255 | if (test != null) { 256 | 257 | if (mBitmap == null) //create Bitmap image first time 258 | { 259 | width_ima = smallest.getWidth(); 260 | height_ima = smallest.getHeight(); 261 | mBitmap = Bitmap.createBitmap(width_ima, height_ima, Bitmap.Config.RGB_565); 262 | } 263 | 264 | ByteBuffer buffer = test.getPlanes()[0].getBuffer(); 265 | resultRGB = getActiveArray(buffer); 266 | 267 | BitmapFactory.Options options = new BitmapFactory.Options(); 268 | options.inMutable = true; 269 | mBitmap = BitmapFactory.decodeByteArray(resultRGB, 0, resultRGB.length, options); 270 | 271 | mBackgroundHandler.post(new SendImageData(mBitmap)); 272 | test.close(); 273 | } 274 | 275 | } 276 | 277 | }; 278 | 279 | private byte[] resultRGB = new byte[0]; 280 | 281 | public byte[] getActiveArray(ByteBuffer buffer) { 282 | byte[] ret = new byte[buffer.remaining()]; 283 | if (buffer.hasArray()) { 284 | byte[] array = buffer.array(); 285 | System.arraycopy(array, buffer.arrayOffset() + buffer.position(), ret, 0, ret.length); 286 | } else { 287 | buffer.slice().get(ret); 288 | } 289 | return ret; 290 | } 291 | 292 | /** 293 | * {@link CaptureRequest.Builder} for the camera preview 294 | */ 295 | private CaptureRequest.Builder mPreviewRequestBuilder; 296 | 297 | /** 298 | * {@link CaptureRequest} generated by {@link #mPreviewRequestBuilder} 299 | */ 300 | private CaptureRequest mPreviewRequest; 301 | 302 | /** 303 | * The current state of camera state for taking pictures. 304 | * 305 | */ 306 | private int mState = STATE_PREVIEW; 307 | 308 | /** 309 | * A {@link Semaphore} to prevent the app from exiting before closing the camera. 310 | */ 311 | private Semaphore mCameraOpenCloseLock = new Semaphore(1); 312 | 313 | private Bitmap mBitmap; 314 | private int width_ima, height_ima; 315 | 316 | public Bitmap getBitmap() { 317 | return mBitmap; 318 | } 319 | 320 | /** 321 | * A {@link CameraCaptureSession.CaptureCallback} that handles events related to JPEG capture. 322 | */ 323 | private CameraCaptureSession.CaptureCallback mCaptureCallback 324 | = new CameraCaptureSession.CaptureCallback() { 325 | 326 | private void process(CaptureResult result) { 327 | switch (mState) { 328 | case STATE_PREVIEW: { 329 | 330 | 331 | break; 332 | } 333 | case STATE_WAITING_LOCK: { 334 | Integer afState = result.get(CaptureResult.CONTROL_AF_STATE); 335 | if (afState == null) { 336 | // captureStillPicture(); 337 | } else if (CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED == afState || 338 | CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED == afState) { 339 | // CONTROL_AE_STATE can be null on some devices 340 | Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE); 341 | if (aeState == null || 342 | aeState == CaptureResult.CONTROL_AE_STATE_CONVERGED) { 343 | mState = STATE_PICTURE_TAKEN; 344 | // captureStillPicture(); 345 | } else { 346 | runPrecaptureSequence(); 347 | } 348 | } 349 | break; 350 | } 351 | case STATE_WAITING_PRECAPTURE: { 352 | // CONTROL_AE_STATE can be null on some devices 353 | Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE); 354 | if (aeState == null || 355 | aeState == CaptureResult.CONTROL_AE_STATE_PRECAPTURE || 356 | aeState == CaptureRequest.CONTROL_AE_STATE_FLASH_REQUIRED) { 357 | mState = STATE_WAITING_NON_PRECAPTURE; 358 | } 359 | break; 360 | } 361 | case STATE_WAITING_NON_PRECAPTURE: { 362 | // CONTROL_AE_STATE can be null on some devices 363 | Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE); 364 | if (aeState == null || aeState != CaptureResult.CONTROL_AE_STATE_PRECAPTURE) { 365 | mState = STATE_PICTURE_TAKEN; 366 | // captureStillPicture(); 367 | } 368 | break; 369 | } 370 | } 371 | } 372 | 373 | @Override 374 | public void onCaptureProgressed(@NonNull CameraCaptureSession session, 375 | @NonNull CaptureRequest request, 376 | @NonNull CaptureResult partialResult) { 377 | process(partialResult); 378 | } 379 | 380 | @Override 381 | public void onCaptureCompleted(@NonNull CameraCaptureSession session, 382 | @NonNull CaptureRequest request, 383 | @NonNull TotalCaptureResult result) { 384 | process(result); 385 | } 386 | 387 | }; 388 | 389 | /** 390 | * Shows a {@link Toast} on the UI thread. 391 | * 392 | * @param text The message to show 393 | */ 394 | private void showToast(final String text) { 395 | final Activity activity = getActivity(); 396 | if (activity != null) { 397 | activity.runOnUiThread(new Runnable() { 398 | @Override 399 | public void run() { 400 | Toast.makeText(activity, text, Toast.LENGTH_SHORT).show(); 401 | } 402 | }); 403 | } 404 | } 405 | 406 | /** 407 | * Given {@code choices} of {@code Size}s supported by a camera, chooses the smallest one whose 408 | * width and height are at least as large as the respective requested values, and whose aspect 409 | * ratio matches with the specified value. 410 | * 411 | * @param choices The list of sizes that the camera supports for the intended output class 412 | * @param width The minimum desired width 413 | * @param height The minimum desired height 414 | * @param aspectRatio The aspect ratio 415 | * @return The optimal {@code Size}, or an arbitrary one if none were big enough 416 | */ 417 | private static Size chooseOptimalSize(Size[] choices, int width, int height, Size aspectRatio) { 418 | // Collect the supported resolutions that are at least as big as the preview Surface 419 | List bigEnough = new ArrayList<>(); 420 | int w = aspectRatio.getWidth(); 421 | int h = aspectRatio.getHeight(); 422 | for (Size option : choices) { 423 | if (option.getHeight() == option.getWidth() * h / w && 424 | option.getWidth() >= width && option.getHeight() >= height) { 425 | bigEnough.add(option); 426 | } 427 | } 428 | 429 | // Pick the smallest of those, assuming we found any 430 | if (bigEnough.size() > 0) { 431 | return Collections.min(bigEnough, new CompareSizesByArea()); 432 | } else { 433 | Log.e(TAG, "Couldn't find any suitable preview size"); 434 | return choices[0]; 435 | } 436 | } 437 | 438 | public static Camera2BasicFragment newInstance() { 439 | return new Camera2BasicFragment(); 440 | } 441 | 442 | @Override 443 | public View onCreateView(LayoutInflater inflater, ViewGroup container, 444 | Bundle savedInstanceState) { 445 | 446 | return inflater.inflate(R.layout.fragment_camera2_basic, container, false); 447 | 448 | } 449 | 450 | @Override 451 | public void onViewCreated(final View view, Bundle savedInstanceState) { 452 | view.findViewById(R.id.picture).setOnClickListener(this); 453 | view.findViewById(R.id.info).setOnClickListener(this); 454 | mTextureView = (AutoFitTextureView) view.findViewById(R.id.texture); 455 | } 456 | 457 | @Override 458 | public void onActivityCreated(Bundle savedInstanceState) { 459 | super.onActivityCreated(savedInstanceState); 460 | mFile = new File(getActivity().getExternalFilesDir(null), "pic.jpg"); 461 | } 462 | 463 | @Override 464 | public void onResume() { 465 | super.onResume(); 466 | startBackgroundThread(); 467 | 468 | // When the screen is turned off and turned back on, the SurfaceTexture is already 469 | // available, and "onSurfaceTextureAvailable" will not be called. In that case, we can open 470 | // a camera and start preview from here (otherwise, we wait until the surface is ready in 471 | // the SurfaceTextureListener). 472 | if (mTextureView.isAvailable()) { 473 | openCameraOrRequestPermission(mTextureView.getWidth(), mTextureView.getHeight()); 474 | } else { 475 | mTextureView.setSurfaceTextureListener(mSurfaceTextureListener); 476 | } 477 | } 478 | 479 | private void openCameraOrRequestPermission(int width, int height) { 480 | if (ActivityCompat.checkSelfPermission(getActivity(), permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { 481 | if (ActivityCompat 482 | .shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.CAMERA)) { 483 | new ConfirmationDialog().show(getChildFragmentManager(), FRAGMENT_DIALOG); 484 | } else { 485 | ActivityCompat 486 | .requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA}, 487 | REQUEST_CAMERA_PERMISSION); 488 | } 489 | } else { 490 | openCamera(width, height); 491 | } 492 | } 493 | 494 | @Override 495 | public void onPause() { 496 | closeCamera(); 497 | stopBackgroundThread(); 498 | super.onPause(); 499 | } 500 | 501 | @Override 502 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, 503 | @NonNull int[] grantResults) { 504 | if (requestCode == REQUEST_CAMERA_PERMISSION) { 505 | if (grantResults.length != 1 || grantResults[0] != PackageManager.PERMISSION_GRANTED) { 506 | ErrorDialog.newInstance(getString(R.string.request_permission)) 507 | .show(getChildFragmentManager(), FRAGMENT_DIALOG); 508 | } 509 | } else { 510 | openCameraOrRequestPermission(mTextureView.getWidth(), mTextureView.getHeight()); 511 | } 512 | } 513 | 514 | Size smallest; 515 | 516 | /** 517 | * Sets up member variables related to camera. 518 | * 519 | * @param width The width of available size for camera preview 520 | * @param height The height of available size for camera preview 521 | */ 522 | private void setUpCameraOutputs(int width, int height) { 523 | Activity activity = getActivity(); 524 | CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE); 525 | try { 526 | for (String cameraId : manager.getCameraIdList()) { 527 | CameraCharacteristics characteristics 528 | = manager.getCameraCharacteristics(cameraId); 529 | 530 | // We don't use a front facing camera in this sample. 531 | Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING); 532 | if (facing != null && facing == CameraCharacteristics.LENS_FACING_FRONT) { 533 | continue; 534 | } 535 | 536 | StreamConfigurationMap map = characteristics.get( 537 | CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); 538 | if (map == null) { 539 | continue; 540 | } 541 | 542 | 543 | // For still image captures, we use the largest available size. 544 | Size largest = Collections.max( 545 | Arrays.asList(map.getOutputSizes(ImageFormat.JPEG)), 546 | new CompareSizesByArea()); 547 | 548 | // For still image captures, we use the largest available size. 549 | smallest = Collections.min( 550 | Arrays.asList(map.getOutputSizes(ImageFormat.JPEG)), 551 | new CompareSizesByArea()); 552 | 553 | mImageReader = ImageReader.newInstance(smallest.getWidth(), smallest.getHeight(), 554 | ImageFormat.JPEG, /*maxImages*/2); 555 | mImageReader.setOnImageAvailableListener( 556 | mOnImageAvailableListener, mBackgroundHandler); 557 | 558 | // Danger, W.R.! Attempting to use too large a preview size could exceed the camera 559 | // bus' bandwidth limitation, resulting in gorgeous previews but the storage of 560 | // garbage capture data. 561 | mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), 562 | width, height, smallest); 563 | 564 | // We fit the aspect ratio of TextureView to the size of preview we picked. 565 | int orientation = getResources().getConfiguration().orientation; 566 | if (orientation == Configuration.ORIENTATION_LANDSCAPE) { 567 | mTextureView.setAspectRatio( 568 | mPreviewSize.getWidth(), mPreviewSize.getHeight()); 569 | } else { 570 | mTextureView.setAspectRatio( 571 | mPreviewSize.getHeight(), mPreviewSize.getWidth()); 572 | } 573 | 574 | mCameraId = cameraId; 575 | return; 576 | } 577 | } catch (CameraAccessException e) { 578 | e.printStackTrace(); 579 | } catch (NullPointerException e) { 580 | // Currently an NPE is thrown when the Camera2API is used but not supported on the 581 | // device this code runs. 582 | ErrorDialog.newInstance(getString(R.string.camera_error)) 583 | .show(getChildFragmentManager(), FRAGMENT_DIALOG); 584 | } 585 | } 586 | 587 | /** 588 | * Opens the camera specified by {@link Camera2BasicFragment#mCameraId}. 589 | */ 590 | private void openCamera(int width, int height) throws SecurityException { 591 | 592 | 593 | setUpCameraOutputs(width, height); 594 | configureTransform(width, height); 595 | Activity activity = getActivity(); 596 | CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE); 597 | try { 598 | if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) { 599 | throw new RuntimeException("Time out waiting to lock camera opening."); 600 | } 601 | manager.openCamera(mCameraId, mStateCallback, mBackgroundHandler); 602 | } catch (CameraAccessException e) { 603 | e.printStackTrace(); 604 | } catch (InterruptedException e) { 605 | throw new RuntimeException("Interrupted while trying to lock camera opening.", e); 606 | } 607 | } 608 | 609 | /** 610 | * Closes the current {@link CameraDevice}. 611 | */ 612 | private void closeCamera() { 613 | try { 614 | mCameraOpenCloseLock.acquire(); 615 | if (null != mCaptureSession) { 616 | mCaptureSession.close(); 617 | mCaptureSession = null; 618 | } 619 | if (null != mCameraDevice) { 620 | mCameraDevice.close(); 621 | mCameraDevice = null; 622 | } 623 | if (null != mImageReader) { 624 | mImageReader.close(); 625 | mImageReader = null; 626 | } 627 | } catch (InterruptedException e) { 628 | throw new RuntimeException("Interrupted while trying to lock camera closing.", e); 629 | } finally { 630 | mCameraOpenCloseLock.release(); 631 | } 632 | } 633 | 634 | /** 635 | * Starts a background thread and its {@link Handler}. 636 | */ 637 | private void startBackgroundThread() { 638 | mBackgroundThread = new HandlerThread("CameraBackground"); 639 | mBackgroundThread.start(); 640 | mBackgroundHandler = new Handler(mBackgroundThread.getLooper()); 641 | } 642 | 643 | /** 644 | * Stops the background thread and its {@link Handler}. 645 | */ 646 | private void stopBackgroundThread() { 647 | mBackgroundThread.quitSafely(); 648 | try { 649 | mBackgroundThread.join(); 650 | mBackgroundThread = null; 651 | mBackgroundHandler = null; 652 | } catch (InterruptedException e) { 653 | e.printStackTrace(); 654 | } 655 | } 656 | 657 | /** 658 | * Creates a new {@link CameraCaptureSession} for camera preview. 659 | */ 660 | private void createCameraPreviewSession() { 661 | try { 662 | SurfaceTexture texture = mTextureView.getSurfaceTexture(); 663 | assert texture != null; 664 | 665 | // We configure the size of default buffer to be the size of camera preview we want. 666 | texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight()); 667 | 668 | // This is the output Surface we need to start preview. 669 | Surface surface = new Surface(texture); 670 | Surface mImageSurface = mImageReader.getSurface(); 671 | 672 | 673 | // We set up a CaptureRequest.Builder with the output Surface. 674 | mPreviewRequestBuilder 675 | = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); 676 | 677 | 678 | mPreviewRequestBuilder.addTarget(surface); 679 | mPreviewRequestBuilder.addTarget(mImageSurface); 680 | 681 | 682 | // Here, we create a CameraCaptureSession for camera preview. 683 | mCameraDevice.createCaptureSession(Arrays.asList(surface, mImageReader.getSurface()), 684 | new CameraCaptureSession.StateCallback() { 685 | 686 | @Override 687 | public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) { 688 | // The camera is already closed 689 | if (null == mCameraDevice) { 690 | return; 691 | } 692 | // When the session is ready, we start displaying the preview. 693 | mCaptureSession = cameraCaptureSession; 694 | try { 695 | // Auto focus should be continuous for camera preview. 696 | mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, 697 | CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE); 698 | mPreviewRequest = mPreviewRequestBuilder.build(); 699 | mCaptureSession.setRepeatingRequest(mPreviewRequest, 700 | mCaptureCallback, mBackgroundHandler); 701 | 702 | 703 | } catch (CameraAccessException e) { 704 | e.printStackTrace(); 705 | } 706 | } 707 | 708 | @Override 709 | public void onConfigureFailed( 710 | @NonNull CameraCaptureSession cameraCaptureSession) { 711 | showToast("Failed"); 712 | } 713 | }, null 714 | ); 715 | } catch (CameraAccessException e) { 716 | e.printStackTrace(); 717 | } 718 | } 719 | 720 | /** 721 | * Configures the necessary {@link android.graphics.Matrix} transformation to `mTextureView`. 722 | * This method should be called after the camera preview size is determined in 723 | * setUpCameraOutputs and also the size of `mTextureView` is fixed. 724 | * 725 | * @param viewWidth The width of `mTextureView` 726 | * @param viewHeight The height of `mTextureView` 727 | */ 728 | private void configureTransform(int viewWidth, int viewHeight) { 729 | Activity activity = getActivity(); 730 | if (null == mTextureView || null == mPreviewSize || null == activity) { 731 | return; 732 | } 733 | int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); 734 | Matrix matrix = new Matrix(); 735 | RectF viewRect = new RectF(0, 0, viewWidth, viewHeight); 736 | RectF bufferRect = new RectF(0, 0, mPreviewSize.getHeight(), mPreviewSize.getWidth()); 737 | float centerX = viewRect.centerX(); 738 | float centerY = viewRect.centerY(); 739 | if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) { 740 | bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY()); 741 | matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL); 742 | float scale = Math.max( 743 | (float) viewHeight / mPreviewSize.getHeight(), 744 | (float) viewWidth / mPreviewSize.getWidth()); 745 | matrix.postScale(scale, scale, centerX, centerY); 746 | matrix.postRotate(90 * (rotation - 2), centerX, centerY); 747 | } else if (Surface.ROTATION_180 == rotation) { 748 | matrix.postRotate(180, centerX, centerY); 749 | } 750 | mTextureView.setTransform(matrix); 751 | } 752 | 753 | /** 754 | * Initiate a still image capture. 755 | */ 756 | private void takePicture() { 757 | lockFocus(); 758 | } 759 | 760 | /** 761 | * Lock the focus as the first step for a still image capture. 762 | */ 763 | private void lockFocus() { 764 | try { 765 | // This is how to tell the camera to lock focus. 766 | mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, 767 | CameraMetadata.CONTROL_AF_TRIGGER_START); 768 | // Tell #mCaptureCallback to wait for the lock. 769 | mState = STATE_WAITING_LOCK; 770 | mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, 771 | mBackgroundHandler); 772 | } catch (CameraAccessException e) { 773 | e.printStackTrace(); 774 | } 775 | } 776 | 777 | /** 778 | * Run the precapture sequence for capturing a still image. This method should be called when 779 | * we get a response in {@link #mCaptureCallback} from {@link #lockFocus()}. 780 | */ 781 | private void runPrecaptureSequence() { 782 | try { 783 | // This is how to tell the camera to trigger. 784 | mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER, 785 | CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER_START); 786 | // Tell #mCaptureCallback to wait for the precapture sequence to be set. 787 | mState = STATE_WAITING_PRECAPTURE; 788 | mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, 789 | mBackgroundHandler); 790 | } catch (CameraAccessException e) { 791 | e.printStackTrace(); 792 | } 793 | } 794 | 795 | /** 796 | * Capture a still picture. This method should be called when we get a response in 797 | * {@link #mCaptureCallback} from both {@link #lockFocus()}. 798 | */ 799 | private void captureStillPicture() { 800 | try { 801 | final Activity activity = getActivity(); 802 | if (null == activity || null == mCameraDevice) { 803 | return; 804 | } 805 | // This is the CaptureRequest.Builder that we use to take a picture. 806 | final CaptureRequest.Builder captureBuilder = 807 | mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE); 808 | captureBuilder.addTarget(mImageReader.getSurface()); 809 | 810 | // Use the same AE and AF modes as the preview. 811 | captureBuilder.set(CaptureRequest.CONTROL_AF_MODE, 812 | CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE); 813 | captureBuilder.set(CaptureRequest.CONTROL_AE_MODE, 814 | CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH); 815 | 816 | // Orientation 817 | int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); 818 | captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, ORIENTATIONS.get(rotation)); 819 | 820 | CameraCaptureSession.CaptureCallback CaptureCallback 821 | = new CameraCaptureSession.CaptureCallback() { 822 | 823 | @Override 824 | public void onCaptureCompleted(@NonNull CameraCaptureSession session, 825 | @NonNull CaptureRequest request, 826 | @NonNull TotalCaptureResult result) { 827 | showToast("Saved: " + mFile); 828 | Log.d(TAG, mFile.toString()); 829 | unlockFocus(); 830 | } 831 | }; 832 | 833 | mCaptureSession.stopRepeating(); 834 | mCaptureSession.capture(captureBuilder.build(), CaptureCallback, null); 835 | } catch (CameraAccessException e) { 836 | e.printStackTrace(); 837 | } 838 | } 839 | 840 | /** 841 | * Unlock the focus. This method should be called when still image capture sequence is 842 | * finished. 843 | */ 844 | private void unlockFocus() { 845 | try { 846 | // Reset the auto-focus trigger 847 | mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, 848 | CameraMetadata.CONTROL_AF_TRIGGER_CANCEL); 849 | mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, 850 | CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH); 851 | mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, 852 | mBackgroundHandler); 853 | // After this, the camera will go back to the normal state of preview. 854 | mState = STATE_PREVIEW; 855 | mCaptureSession.setRepeatingRequest(mPreviewRequest, mCaptureCallback, 856 | mBackgroundHandler); 857 | } catch (CameraAccessException e) { 858 | e.printStackTrace(); 859 | } 860 | } 861 | 862 | @Override 863 | public void onClick(View view) { 864 | switch (view.getId()) { 865 | case R.id.picture: { 866 | takePicture(); 867 | break; 868 | } 869 | case R.id.info: { 870 | Activity activity = getActivity(); 871 | if (null != activity) { 872 | new AlertDialog.Builder(activity) 873 | .setMessage(R.string.intro_message) 874 | .setPositiveButton(android.R.string.ok, null) 875 | .show(); 876 | } 877 | break; 878 | } 879 | } 880 | } 881 | 882 | private static class SendImageData implements Runnable { 883 | 884 | private Bitmap mBitmap; 885 | private InetAddress serverAddr; 886 | private DatagramSocket socket; 887 | 888 | public SendImageData(Bitmap bitmap) { 889 | mBitmap = bitmap; 890 | } 891 | 892 | @Override 893 | public void run() { 894 | 895 | try { 896 | 897 | if (serverAddr == null) { 898 | serverAddr = InetAddress.getByName("192.168.178.29"); 899 | } 900 | if (socket == null) { 901 | socket = new DatagramSocket(); 902 | } 903 | 904 | Matrix matrix = new Matrix(); 905 | matrix.postRotate(90); 906 | Bitmap rotated = Bitmap.createBitmap(mBitmap, 0, 0, 907 | mBitmap.getWidth(), mBitmap.getHeight(), 908 | matrix, true); 909 | 910 | int size_p = 0, i; 911 | ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); 912 | rotated.compress(Bitmap.CompressFormat.JPEG, 50, byteStream); 913 | 914 | byte data[] = byteStream.toByteArray(); 915 | Log.e(TAG, "SIZE: " + data.length); 916 | 917 | try { 918 | size_p = data.length; 919 | DatagramPacket packet = new DatagramPacket(data, size_p, serverAddr, 7788); 920 | socket.send(packet); 921 | mBitmap.recycle(); 922 | } catch (Exception e) { 923 | System.out.println(e.toString()); 924 | } 925 | } catch (Exception e) { 926 | System.out.println(e.toString()); 927 | 928 | } 929 | 930 | } 931 | } 932 | 933 | 934 | /** 935 | * Saves a JPEG {@link Image} into the specified {@link File}. 936 | */ 937 | private static class ImageSaver implements Runnable { 938 | 939 | /** 940 | * The JPEG image 941 | */ 942 | private final Image mImage; 943 | /** 944 | * The file we save the image into. 945 | */ 946 | private final File mFile; 947 | 948 | public ImageSaver(Image image, File file) { 949 | mImage = image; 950 | mFile = file; 951 | } 952 | 953 | @Override 954 | public void run() { 955 | ByteBuffer buffer = mImage.getPlanes()[0].getBuffer(); 956 | byte[] bytes = new byte[buffer.remaining()]; 957 | buffer.get(bytes); 958 | FileOutputStream output = null; 959 | try { 960 | output = new FileOutputStream(mFile); 961 | output.write(bytes); 962 | } catch (IOException e) { 963 | e.printStackTrace(); 964 | } finally { 965 | mImage.close(); 966 | if (null != output) { 967 | try { 968 | output.close(); 969 | } catch (IOException e) { 970 | e.printStackTrace(); 971 | } 972 | } 973 | } 974 | } 975 | 976 | } 977 | 978 | /** 979 | * Compares two {@code Size}s based on their areas. 980 | */ 981 | static class CompareSizesByArea implements Comparator { 982 | 983 | @Override 984 | public int compare(Size lhs, Size rhs) { 985 | // We cast here to ensure the multiplications won't overflow 986 | return Long.signum((long) lhs.getWidth() * lhs.getHeight() - 987 | (long) rhs.getWidth() * rhs.getHeight()); 988 | } 989 | 990 | } 991 | 992 | /** 993 | * Shows an error message dialog. 994 | */ 995 | public static class ErrorDialog extends DialogFragment { 996 | 997 | private static final String ARG_MESSAGE = "message"; 998 | 999 | public static ErrorDialog newInstance(String message) { 1000 | ErrorDialog dialog = new ErrorDialog(); 1001 | Bundle args = new Bundle(); 1002 | args.putString(ARG_MESSAGE, message); 1003 | dialog.setArguments(args); 1004 | return dialog; 1005 | } 1006 | 1007 | @Override 1008 | public Dialog onCreateDialog(Bundle savedInstanceState) { 1009 | final Activity activity = getActivity(); 1010 | return new AlertDialog.Builder(activity) 1011 | .setMessage(getArguments().getString(ARG_MESSAGE)) 1012 | .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { 1013 | @Override 1014 | public void onClick(DialogInterface dialogInterface, int i) { 1015 | activity.finish(); 1016 | } 1017 | }) 1018 | .create(); 1019 | } 1020 | 1021 | } 1022 | 1023 | /** 1024 | * Shows OK/Cancel confirmation dialog about camera permission. 1025 | */ 1026 | public static class ConfirmationDialog extends DialogFragment { 1027 | 1028 | @NonNull 1029 | @Override 1030 | public Dialog onCreateDialog(Bundle savedInstanceState) { 1031 | final Fragment parent = getParentFragment(); 1032 | return new AlertDialog.Builder(getActivity()) 1033 | .setMessage(R.string.request_permission) 1034 | .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { 1035 | @Override 1036 | public void onClick(DialogInterface dialog, int which) { 1037 | ActivityCompat.requestPermissions(parent.getActivity(), 1038 | new String[]{Manifest.permission.CAMERA}, 1039 | REQUEST_CAMERA_PERMISSION); 1040 | } 1041 | }) 1042 | .setNegativeButton(android.R.string.cancel, 1043 | new DialogInterface.OnClickListener() { 1044 | @Override 1045 | public void onClick(DialogInterface dialog, int which) { 1046 | Activity activity = parent.getActivity(); 1047 | if (activity != null) { 1048 | activity.finish(); 1049 | } 1050 | } 1051 | }) 1052 | .create(); 1053 | } 1054 | } 1055 | 1056 | } 1057 | -------------------------------------------------------------------------------- /Application/src/main/java/com/example/android/camera2basic/CameraActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.example.android.camera2basic; 18 | 19 | import android.app.Activity; 20 | import android.os.Bundle; 21 | import android.support.v7.app.AppCompatActivity; 22 | 23 | public class CameraActivity extends AppCompatActivity { 24 | 25 | @Override 26 | protected void onCreate(Bundle savedInstanceState) { 27 | super.onCreate(savedInstanceState); 28 | setContentView(R.layout.activity_camera); 29 | if (null == savedInstanceState) { 30 | getSupportFragmentManager().beginTransaction() 31 | .replace(R.id.container, Camera2BasicFragment.newInstance()) 32 | .commit(); 33 | } 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /Application/src/main/res/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SundayLab/Camera2BasicStream/8a961bb5a2bbabf7c154ed25afbfb017c73f561d/Application/src/main/res/.DS_Store -------------------------------------------------------------------------------- /Application/src/main/res/drawable-hdpi/ic_action_info.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SundayLab/Camera2BasicStream/8a961bb5a2bbabf7c154ed25afbfb017c73f561d/Application/src/main/res/drawable-hdpi/ic_action_info.png -------------------------------------------------------------------------------- /Application/src/main/res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SundayLab/Camera2BasicStream/8a961bb5a2bbabf7c154ed25afbfb017c73f561d/Application/src/main/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /Application/src/main/res/drawable-hdpi/tile.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SundayLab/Camera2BasicStream/8a961bb5a2bbabf7c154ed25afbfb017c73f561d/Application/src/main/res/drawable-hdpi/tile.9.png -------------------------------------------------------------------------------- /Application/src/main/res/drawable-mdpi/ic_action_info.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SundayLab/Camera2BasicStream/8a961bb5a2bbabf7c154ed25afbfb017c73f561d/Application/src/main/res/drawable-mdpi/ic_action_info.png -------------------------------------------------------------------------------- /Application/src/main/res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SundayLab/Camera2BasicStream/8a961bb5a2bbabf7c154ed25afbfb017c73f561d/Application/src/main/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /Application/src/main/res/drawable-xhdpi/ic_action_info.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SundayLab/Camera2BasicStream/8a961bb5a2bbabf7c154ed25afbfb017c73f561d/Application/src/main/res/drawable-xhdpi/ic_action_info.png -------------------------------------------------------------------------------- /Application/src/main/res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SundayLab/Camera2BasicStream/8a961bb5a2bbabf7c154ed25afbfb017c73f561d/Application/src/main/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /Application/src/main/res/drawable-xxhdpi/ic_action_info.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SundayLab/Camera2BasicStream/8a961bb5a2bbabf7c154ed25afbfb017c73f561d/Application/src/main/res/drawable-xxhdpi/ic_action_info.png -------------------------------------------------------------------------------- /Application/src/main/res/drawable-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SundayLab/Camera2BasicStream/8a961bb5a2bbabf7c154ed25afbfb017c73f561d/Application/src/main/res/drawable-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /Application/src/main/res/layout-land/fragment_camera2_basic.xml: -------------------------------------------------------------------------------- 1 | 16 | 19 | 20 | 27 | 28 | 38 | 39 | 45 | 46 | 55 | 56 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /Application/src/main/res/layout/activity_camera.xml: -------------------------------------------------------------------------------- 1 | 16 | 23 | -------------------------------------------------------------------------------- /Application/src/main/res/layout/fragment_camera2_basic.xml: -------------------------------------------------------------------------------- 1 | 16 | 19 | 20 | 26 | 27 | 34 | 35 | 41 | 42 | 51 | 52 | 53 | 59 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /Application/src/main/res/values-sw600dp/template-dimens.xml: -------------------------------------------------------------------------------- 1 | 16 | 17 | 18 | 19 | 20 | 21 | @dimen/margin_huge 22 | @dimen/margin_medium 23 | 24 | 25 | -------------------------------------------------------------------------------- /Application/src/main/res/values-sw600dp/template-styles.xml: -------------------------------------------------------------------------------- 1 | 16 | 17 | 18 | 19 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /Application/src/main/res/values-v11/template-styles.xml: -------------------------------------------------------------------------------- 1 | 16 | 17 | 18 | 19 | 20 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /Application/src/main/res/values/base-strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 19 | Camera2BasicStream 20 | 21 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /Application/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | #cc4285f4 19 | 20 | -------------------------------------------------------------------------------- /Application/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 16 | 17 | Picture 18 | Info 19 | This sample needs camera permission. 20 | This device doesn\'t support Camera2 API. 21 | 22 | -------------------------------------------------------------------------------- /Application/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 16 | 17 | 34 | 35 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /Application/tests/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 22 | 23 | 24 | 25 | 28 | 29 | 30 | 31 | 32 | 35 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /Application/tests/src/com/example/android/camera2basic/tests/SampleTests.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2013 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.example.android.camera2basic.tests; 17 | 18 | import com.example.android.camera2basic.*; 19 | 20 | import android.test.ActivityInstrumentationTestCase2; 21 | 22 | /** 23 | * Tests for Camera2Basic sample. 24 | */ 25 | public class SampleTests extends ActivityInstrumentationTestCase2 { 26 | 27 | private CameraActivity mTestActivity; 28 | 29 | public SampleTests() { 30 | super(CameraActivity.class); 31 | } 32 | 33 | @Override 34 | protected void setUp() throws Exception { 35 | super.setUp(); 36 | 37 | // Starts the activity under test using the default Intent with: 38 | // action = {@link Intent#ACTION_MAIN} 39 | // flags = {@link Intent#FLAG_ACTIVITY_NEW_TASK} 40 | // All other fields are null or empty. 41 | mTestActivity = getActivity(); 42 | } 43 | 44 | /** 45 | * Test if the test fixture has been set up correctly. 46 | */ 47 | public void testPreconditions() { 48 | //Try to add a message to add context to your assertions. These messages will be shown if 49 | //a tests fails and make it easy to understand why a test failed 50 | assertNotNull("mTestActivity is null", mTestActivity); 51 | } 52 | 53 | /** 54 | * Add more tests below. 55 | */ 56 | 57 | } 58 | -------------------------------------------------------------------------------- /CONTRIB.md: -------------------------------------------------------------------------------- 1 | # How to become a contributor and submit your own code 2 | 3 | ## Contributor License Agreements 4 | 5 | We'd love to accept your sample apps and patches! Before we can take them, we 6 | have to jump a couple of legal hurdles. 7 | 8 | Please fill out either the individual or corporate Contributor License Agreement (CLA). 9 | 10 | * If you are an individual writing original source code and you're sure you 11 | own the intellectual property, then you'll need to sign an [individual CLA] 12 | (https://developers.google.com/open-source/cla/individual). 13 | * If you work for a company that wants to allow you to contribute your work, 14 | then you'll need to sign a [corporate CLA] 15 | (https://developers.google.com/open-source/cla/corporate). 16 | 17 | Follow either of the two links above to access the appropriate CLA and 18 | instructions for how to sign and return it. Once we receive it, we'll be able to 19 | accept your pull requests. 20 | 21 | ## Contributing A Patch 22 | 23 | 1. Submit an issue describing your proposed change to the repo in question. 24 | 1. The repo owner will respond to your issue promptly. 25 | 1. If your proposed change is accepted, and you haven't already done so, sign a 26 | Contributor License Agreement (see details above). 27 | 1. Fork the desired repo, develop and test your code changes. 28 | 1. Ensure that your code adheres to the existing style in the sample to which 29 | you are contributing. Refer to the 30 | [Android Code Style Guide] 31 | (https://source.android.com/source/code-style.html) for the 32 | recommended coding standards for this organization. 33 | 1. Ensure that your code has an appropriate set of unit tests which all pass. 34 | 1. Submit a pull request. 35 | 36 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # How to become a contributor and submit your own code 2 | 3 | ## Contributor License Agreements 4 | 5 | We'd love to accept your sample apps and patches! Before we can take them, we 6 | have to jump a couple of legal hurdles. 7 | 8 | Please fill out either the individual or corporate Contributor License Agreement (CLA). 9 | 10 | * If you are an individual writing original source code and you're sure you 11 | own the intellectual property, then you'll need to sign an [individual CLA] 12 | (https://cla.developers.google.com). 13 | * If you work for a company that wants to allow you to contribute your work, 14 | then you'll need to sign a [corporate CLA] 15 | (https://cla.developers.google.com). 16 | 17 | Follow either of the two links above to access the appropriate CLA and 18 | instructions for how to sign and return it. Once we receive it, we'll be able to 19 | accept your pull requests. 20 | 21 | ## Contributing A Patch 22 | 23 | 1. Submit an issue describing your proposed change to the repo in question. 24 | 1. The repo owner will respond to your issue promptly. 25 | 1. If your proposed change is accepted, and you haven't already done so, sign a 26 | Contributor License Agreement (see details above). 27 | 1. Fork the desired repo, develop and test your code changes. 28 | 1. Ensure that your code adheres to the existing style in the sample to which 29 | you are contributing. Refer to the 30 | [Android Code Style Guide] 31 | (https://source.android.com/source/code-style.html) for the 32 | recommended coding standards for this organization. 33 | 1. Ensure that your code has an appropriate set of unit tests which all pass. 34 | 1. Submit a pull request. 35 | 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | -------------- 3 | 4 | Version 2.0, January 2004 5 | http://www.apache.org/licenses/ 6 | 7 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 8 | 9 | 1. Definitions. 10 | 11 | "License" shall mean the terms and conditions for use, reproduction, 12 | and distribution as defined by Sections 1 through 9 of this document. 13 | 14 | "Licensor" shall mean the copyright owner or entity authorized by 15 | the copyright owner that is granting the License. 16 | 17 | "Legal Entity" shall mean the union of the acting entity and all 18 | other entities that control, are controlled by, or are under common 19 | control with that entity. For the purposes of this definition, 20 | "control" means (i) the power, direct or indirect, to cause the 21 | direction or management of such entity, whether by contract or 22 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 23 | outstanding shares, or (iii) beneficial ownership of such entity. 24 | 25 | "You" (or "Your") shall mean an individual or Legal Entity 26 | exercising permissions granted by this License. 27 | 28 | "Source" form shall mean the preferred form for making modifications, 29 | including but not limited to software source code, documentation 30 | source, and configuration files. 31 | 32 | "Object" form shall mean any form resulting from mechanical 33 | transformation or translation of a Source form, including but 34 | not limited to compiled object code, generated documentation, 35 | and conversions to other media types. 36 | 37 | "Work" shall mean the work of authorship, whether in Source or 38 | Object form, made available under the License, as indicated by a 39 | copyright notice that is included in or attached to the work 40 | (an example is provided in the Appendix below). 41 | 42 | "Derivative Works" shall mean any work, whether in Source or Object 43 | form, that is based on (or derived from) the Work and for which the 44 | editorial revisions, annotations, elaborations, or other modifications 45 | represent, as a whole, an original work of authorship. For the purposes 46 | of this License, Derivative Works shall not include works that remain 47 | separable from, or merely link (or bind by name) to the interfaces of, 48 | the Work and Derivative Works thereof. 49 | 50 | "Contribution" shall mean any work of authorship, including 51 | the original version of the Work and any modifications or additions 52 | to that Work or Derivative Works thereof, that is intentionally 53 | submitted to Licensor for inclusion in the Work by the copyright owner 54 | or by an individual or Legal Entity authorized to submit on behalf of 55 | the copyright owner. For the purposes of this definition, "submitted" 56 | means any form of electronic, verbal, or written communication sent 57 | to the Licensor or its representatives, including but not limited to 58 | communication on electronic mailing lists, source code control systems, 59 | and issue tracking systems that are managed by, or on behalf of, the 60 | Licensor for the purpose of discussing and improving the Work, but 61 | excluding communication that is conspicuously marked or otherwise 62 | designated in writing by the copyright owner as "Not a Contribution." 63 | 64 | "Contributor" shall mean Licensor and any individual or Legal Entity 65 | on behalf of whom a Contribution has been received by Licensor and 66 | subsequently incorporated within the Work. 67 | 68 | 2. Grant of Copyright License. Subject to the terms and conditions of 69 | this License, each Contributor hereby grants to You a perpetual, 70 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 71 | copyright license to reproduce, prepare Derivative Works of, 72 | publicly display, publicly perform, sublicense, and distribute the 73 | Work and such Derivative Works in Source or Object form. 74 | 75 | 3. Grant of Patent License. Subject to the terms and conditions of 76 | this License, each Contributor hereby grants to You a perpetual, 77 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 78 | (except as stated in this section) patent license to make, have made, 79 | use, offer to sell, sell, import, and otherwise transfer the Work, 80 | where such license applies only to those patent claims licensable 81 | by such Contributor that are necessarily infringed by their 82 | Contribution(s) alone or by combination of their Contribution(s) 83 | with the Work to which such Contribution(s) was submitted. If You 84 | institute patent litigation against any entity (including a 85 | cross-claim or counterclaim in a lawsuit) alleging that the Work 86 | or a Contribution incorporated within the Work constitutes direct 87 | or contributory patent infringement, then any patent licenses 88 | granted to You under this License for that Work shall terminate 89 | as of the date such litigation is filed. 90 | 91 | 4. Redistribution. You may reproduce and distribute copies of the 92 | Work or Derivative Works thereof in any medium, with or without 93 | modifications, and in Source or Object form, provided that You 94 | meet the following conditions: 95 | 96 | (a) You must give any other recipients of the Work or 97 | Derivative Works a copy of this License; and 98 | 99 | (b) You must cause any modified files to carry prominent notices 100 | stating that You changed the files; and 101 | 102 | (c) You must retain, in the Source form of any Derivative Works 103 | that You distribute, all copyright, patent, trademark, and 104 | attribution notices from the Source form of the Work, 105 | excluding those notices that do not pertain to any part of 106 | the Derivative Works; and 107 | 108 | (d) If the Work includes a "NOTICE" text file as part of its 109 | distribution, then any Derivative Works that You distribute must 110 | include a readable copy of the attribution notices contained 111 | within such NOTICE file, excluding those notices that do not 112 | pertain to any part of the Derivative Works, in at least one 113 | of the following places: within a NOTICE text file distributed 114 | as part of the Derivative Works; within the Source form or 115 | documentation, if provided along with the Derivative Works; or, 116 | within a display generated by the Derivative Works, if and 117 | wherever such third-party notices normally appear. The contents 118 | of the NOTICE file are for informational purposes only and 119 | do not modify the License. You may add Your own attribution 120 | notices within Derivative Works that You distribute, alongside 121 | or as an addendum to the NOTICE text from the Work, provided 122 | that such additional attribution notices cannot be construed 123 | as modifying the License. 124 | 125 | You may add Your own copyright statement to Your modifications and 126 | may provide additional or different license terms and conditions 127 | for use, reproduction, or distribution of Your modifications, or 128 | for any such Derivative Works as a whole, provided Your use, 129 | reproduction, and distribution of the Work otherwise complies with 130 | the conditions stated in this License. 131 | 132 | 5. Submission of Contributions. Unless You explicitly state otherwise, 133 | any Contribution intentionally submitted for inclusion in the Work 134 | by You to the Licensor shall be under the terms and conditions of 135 | this License, without any additional terms or conditions. 136 | Notwithstanding the above, nothing herein shall supersede or modify 137 | the terms of any separate license agreement you may have executed 138 | with Licensor regarding such Contributions. 139 | 140 | 6. Trademarks. This License does not grant permission to use the trade 141 | names, trademarks, service marks, or product names of the Licensor, 142 | except as required for reasonable and customary use in describing the 143 | origin of the Work and reproducing the content of the NOTICE file. 144 | 145 | 7. Disclaimer of Warranty. Unless required by applicable law or 146 | agreed to in writing, Licensor provides the Work (and each 147 | Contributor provides its Contributions) on an "AS IS" BASIS, 148 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 149 | implied, including, without limitation, any warranties or conditions 150 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 151 | PARTICULAR PURPOSE. You are solely responsible for determining the 152 | appropriateness of using or redistributing the Work and assume any 153 | risks associated with Your exercise of permissions under this License. 154 | 155 | 8. Limitation of Liability. In no event and under no legal theory, 156 | whether in tort (including negligence), contract, or otherwise, 157 | unless required by applicable law (such as deliberate and grossly 158 | negligent acts) or agreed to in writing, shall any Contributor be 159 | liable to You for damages, including any direct, indirect, special, 160 | incidental, or consequential damages of any character arising as a 161 | result of this License or out of the use or inability to use the 162 | Work (including but not limited to damages for loss of goodwill, 163 | work stoppage, computer failure or malfunction, or any and all 164 | other commercial damages or losses), even if such Contributor 165 | has been advised of the possibility of such damages. 166 | 167 | 9. Accepting Warranty or Additional Liability. While redistributing 168 | the Work or Derivative Works thereof, You may choose to offer, 169 | and charge a fee for, acceptance of support, warranty, indemnity, 170 | or other liability obligations and/or rights consistent with this 171 | License. However, in accepting such obligations, You may act only 172 | on Your own behalf and on Your sole responsibility, not on behalf 173 | of any other Contributor, and only if You agree to indemnify, 174 | defend, and hold each Contributor harmless for any liability 175 | incurred by, or claims asserted against, such Contributor by reason 176 | of your accepting any such warranty or additional liability. 177 | 178 | END OF TERMS AND CONDITIONS 179 | 180 | APPENDIX: How to apply the Apache License to your work. 181 | 182 | To apply the Apache License to your work, attach the following 183 | boilerplate notice, with the fields enclosed by brackets "{}" 184 | replaced with your own identifying information. (Don't include 185 | the brackets!) The text should be enclosed in the appropriate 186 | comment syntax for the file format. We also recommend that a 187 | file or class name and description of purpose be included on the 188 | same "printed page" as the copyright notice for easier 189 | identification within third-party archives. 190 | 191 | Copyright {yyyy} {name of copyright owner} 192 | 193 | Licensed under the Apache License, Version 2.0 (the "License"); 194 | you may not use this file except in compliance with the License. 195 | You may obtain a copy of the License at 196 | 197 | http://www.apache.org/licenses/LICENSE-2.0 198 | 199 | Unless required by applicable law or agreed to in writing, software 200 | distributed under the License is distributed on an "AS IS" BASIS, 201 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 202 | See the License for the specific language governing permissions and 203 | limitations under the License. 204 | 205 | All image and audio files (including *.png, *.jpg, *.svg, *.mp3, *.wav 206 | and *.ogg) are licensed under the CC-BY-NC license. All other files are 207 | licensed under the Apache 2 license. 208 | 209 | CC-BY-NC License 210 | ---------------- 211 | 212 | Attribution-NonCommercial-ShareAlike 4.0 International 213 | 214 | ======================================================================= 215 | 216 | Creative Commons Corporation ("Creative Commons") is not a law firm and 217 | does not provide legal services or legal advice. Distribution of 218 | Creative Commons public licenses does not create a lawyer-client or 219 | other relationship. Creative Commons makes its licenses and related 220 | information available on an "as-is" basis. Creative Commons gives no 221 | warranties regarding its licenses, any material licensed under their 222 | terms and conditions, or any related information. Creative Commons 223 | disclaims all liability for damages resulting from their use to the 224 | fullest extent possible. 225 | 226 | Using Creative Commons Public Licenses 227 | 228 | Creative Commons public licenses provide a standard set of terms and 229 | conditions that creators and other rights holders may use to share 230 | original works of authorship and other material subject to copyright 231 | and certain other rights specified in the public license below. The 232 | following considerations are for informational purposes only, are not 233 | exhaustive, and do not form part of our licenses. 234 | 235 | Considerations for licensors: Our public licenses are 236 | intended for use by those authorized to give the public 237 | permission to use material in ways otherwise restricted by 238 | copyright and certain other rights. Our licenses are 239 | irrevocable. Licensors should read and understand the terms 240 | and conditions of the license they choose before applying it. 241 | Licensors should also secure all rights necessary before 242 | applying our licenses so that the public can reuse the 243 | material as expected. Licensors should clearly mark any 244 | material not subject to the license. This includes other CC- 245 | licensed material, or material used under an exception or 246 | limitation to copyright. More considerations for licensors: 247 | wiki.creativecommons.org/Considerations_for_licensors 248 | 249 | Considerations for the public: By using one of our public 250 | licenses, a licensor grants the public permission to use the 251 | licensed material under specified terms and conditions. If 252 | the licensor's permission is not necessary for any reason--for 253 | example, because of any applicable exception or limitation to 254 | copyright--then that use is not regulated by the license. Our 255 | licenses grant only permissions under copyright and certain 256 | other rights that a licensor has authority to grant. Use of 257 | the licensed material may still be restricted for other 258 | reasons, including because others have copyright or other 259 | rights in the material. A licensor may make special requests, 260 | such as asking that all changes be marked or described. 261 | Although not required by our licenses, you are encouraged to 262 | respect those requests where reasonable. More_considerations 263 | for the public: 264 | wiki.creativecommons.org/Considerations_for_licensees 265 | 266 | ======================================================================= 267 | 268 | Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International 269 | Public License 270 | 271 | By exercising the Licensed Rights (defined below), You accept and agree 272 | to be bound by the terms and conditions of this Creative Commons 273 | Attribution-NonCommercial-ShareAlike 4.0 International Public License 274 | ("Public License"). To the extent this Public License may be 275 | interpreted as a contract, You are granted the Licensed Rights in 276 | consideration of Your acceptance of these terms and conditions, and the 277 | Licensor grants You such rights in consideration of benefits the 278 | Licensor receives from making the Licensed Material available under 279 | these terms and conditions. 280 | 281 | 282 | Section 1 -- Definitions. 283 | 284 | a. Adapted Material means material subject to Copyright and Similar 285 | Rights that is derived from or based upon the Licensed Material 286 | and in which the Licensed Material is translated, altered, 287 | arranged, transformed, or otherwise modified in a manner requiring 288 | permission under the Copyright and Similar Rights held by the 289 | Licensor. For purposes of this Public License, where the Licensed 290 | Material is a musical work, performance, or sound recording, 291 | Adapted Material is always produced where the Licensed Material is 292 | synched in timed relation with a moving image. 293 | 294 | b. Adapter's License means the license You apply to Your Copyright 295 | and Similar Rights in Your contributions to Adapted Material in 296 | accordance with the terms and conditions of this Public License. 297 | 298 | c. BY-NC-SA Compatible License means a license listed at 299 | creativecommons.org/compatiblelicenses, approved by Creative 300 | Commons as essentially the equivalent of this Public License. 301 | 302 | d. Copyright and Similar Rights means copyright and/or similar rights 303 | closely related to copyright including, without limitation, 304 | performance, broadcast, sound recording, and Sui Generis Database 305 | Rights, without regard to how the rights are labeled or 306 | categorized. For purposes of this Public License, the rights 307 | specified in Section 2(b)(1)-(2) are not Copyright and Similar 308 | Rights. 309 | 310 | e. Effective Technological Measures means those measures that, in the 311 | absence of proper authority, may not be circumvented under laws 312 | fulfilling obligations under Article 11 of the WIPO Copyright 313 | Treaty adopted on December 20, 1996, and/or similar international 314 | agreements. 315 | 316 | f. Exceptions and Limitations means fair use, fair dealing, and/or 317 | any other exception or limitation to Copyright and Similar Rights 318 | that applies to Your use of the Licensed Material. 319 | 320 | g. License Elements means the license attributes listed in the name 321 | of a Creative Commons Public License. The License Elements of this 322 | Public License are Attribution, NonCommercial, and ShareAlike. 323 | 324 | h. Licensed Material means the artistic or literary work, database, 325 | or other material to which the Licensor applied this Public 326 | License. 327 | 328 | i. Licensed Rights means the rights granted to You subject to the 329 | terms and conditions of this Public License, which are limited to 330 | all Copyright and Similar Rights that apply to Your use of the 331 | Licensed Material and that the Licensor has authority to license. 332 | 333 | j. Licensor means the individual(s) or entity(ies) granting rights 334 | under this Public License. 335 | 336 | k. NonCommercial means not primarily intended for or directed towards 337 | commercial advantage or monetary compensation. For purposes of 338 | this Public License, the exchange of the Licensed Material for 339 | other material subject to Copyright and Similar Rights by digital 340 | file-sharing or similar means is NonCommercial provided there is 341 | no payment of monetary compensation in connection with the 342 | exchange. 343 | 344 | l. Share means to provide material to the public by any means or 345 | process that requires permission under the Licensed Rights, such 346 | as reproduction, public display, public performance, distribution, 347 | dissemination, communication, or importation, and to make material 348 | available to the public including in ways that members of the 349 | public may access the material from a place and at a time 350 | individually chosen by them. 351 | 352 | m. Sui Generis Database Rights means rights other than copyright 353 | resulting from Directive 96/9/EC of the European Parliament and of 354 | the Council of 11 March 1996 on the legal protection of databases, 355 | as amended and/or succeeded, as well as other essentially 356 | equivalent rights anywhere in the world. 357 | 358 | n. You means the individual or entity exercising the Licensed Rights 359 | under this Public License. Your has a corresponding meaning. 360 | 361 | 362 | Section 2 -- Scope. 363 | 364 | a. License grant. 365 | 366 | 1. Subject to the terms and conditions of this Public License, 367 | the Licensor hereby grants You a worldwide, royalty-free, 368 | non-sublicensable, non-exclusive, irrevocable license to 369 | exercise the Licensed Rights in the Licensed Material to: 370 | 371 | a. reproduce and Share the Licensed Material, in whole or 372 | in part, for NonCommercial purposes only; and 373 | 374 | b. produce, reproduce, and Share Adapted Material for 375 | NonCommercial purposes only. 376 | 377 | 2. Exceptions and Limitations. For the avoidance of doubt, where 378 | Exceptions and Limitations apply to Your use, this Public 379 | License does not apply, and You do not need to comply with 380 | its terms and conditions. 381 | 382 | 3. Term. The term of this Public License is specified in Section 383 | 6(a). 384 | 385 | 4. Media and formats; technical modifications allowed. The 386 | Licensor authorizes You to exercise the Licensed Rights in 387 | all media and formats whether now known or hereafter created, 388 | and to make technical modifications necessary to do so. The 389 | Licensor waives and/or agrees not to assert any right or 390 | authority to forbid You from making technical modifications 391 | necessary to exercise the Licensed Rights, including 392 | technical modifications necessary to circumvent Effective 393 | Technological Measures. For purposes of this Public License, 394 | simply making modifications authorized by this Section 2(a) 395 | (4) never produces Adapted Material. 396 | 397 | 5. Downstream recipients. 398 | 399 | a. Offer from the Licensor -- Licensed Material. Every 400 | recipient of the Licensed Material automatically 401 | receives an offer from the Licensor to exercise the 402 | Licensed Rights under the terms and conditions of this 403 | Public License. 404 | 405 | b. Additional offer from the Licensor -- Adapted Material. 406 | Every recipient of Adapted Material from You 407 | automatically receives an offer from the Licensor to 408 | exercise the Licensed Rights in the Adapted Material 409 | under the conditions of the Adapter's License You apply. 410 | 411 | c. No downstream restrictions. You may not offer or impose 412 | any additional or different terms or conditions on, or 413 | apply any Effective Technological Measures to, the 414 | Licensed Material if doing so restricts exercise of the 415 | Licensed Rights by any recipient of the Licensed 416 | Material. 417 | 418 | 6. No endorsement. Nothing in this Public License constitutes or 419 | may be construed as permission to assert or imply that You 420 | are, or that Your use of the Licensed Material is, connected 421 | with, or sponsored, endorsed, or granted official status by, 422 | the Licensor or others designated to receive attribution as 423 | provided in Section 3(a)(1)(A)(i). 424 | 425 | b. Other rights. 426 | 427 | 1. Moral rights, such as the right of integrity, are not 428 | licensed under this Public License, nor are publicity, 429 | privacy, and/or other similar personality rights; however, to 430 | the extent possible, the Licensor waives and/or agrees not to 431 | assert any such rights held by the Licensor to the limited 432 | extent necessary to allow You to exercise the Licensed 433 | Rights, but not otherwise. 434 | 435 | 2. Patent and trademark rights are not licensed under this 436 | Public License. 437 | 438 | 3. To the extent possible, the Licensor waives any right to 439 | collect royalties from You for the exercise of the Licensed 440 | Rights, whether directly or through a collecting society 441 | under any voluntary or waivable statutory or compulsory 442 | licensing scheme. In all other cases the Licensor expressly 443 | reserves any right to collect such royalties, including when 444 | the Licensed Material is used other than for NonCommercial 445 | purposes. 446 | 447 | 448 | Section 3 -- License Conditions. 449 | 450 | Your exercise of the Licensed Rights is expressly made subject to the 451 | following conditions. 452 | 453 | a. Attribution. 454 | 455 | 1. If You Share the Licensed Material (including in modified 456 | form), You must: 457 | 458 | a. retain the following if it is supplied by the Licensor 459 | with the Licensed Material: 460 | 461 | i. identification of the creator(s) of the Licensed 462 | Material and any others designated to receive 463 | attribution, in any reasonable manner requested by 464 | the Licensor (including by pseudonym if 465 | designated); 466 | 467 | ii. a copyright notice; 468 | 469 | iii. a notice that refers to this Public License; 470 | 471 | iv. a notice that refers to the disclaimer of 472 | warranties; 473 | 474 | v. a URI or hyperlink to the Licensed Material to the 475 | extent reasonably practicable; 476 | 477 | b. indicate if You modified the Licensed Material and 478 | retain an indication of any previous modifications; and 479 | 480 | c. indicate the Licensed Material is licensed under this 481 | Public License, and include the text of, or the URI or 482 | hyperlink to, this Public License. 483 | 484 | 2. You may satisfy the conditions in Section 3(a)(1) in any 485 | reasonable manner based on the medium, means, and context in 486 | which You Share the Licensed Material. For example, it may be 487 | reasonable to satisfy the conditions by providing a URI or 488 | hyperlink to a resource that includes the required 489 | information. 490 | 3. If requested by the Licensor, You must remove any of the 491 | information required by Section 3(a)(1)(A) to the extent 492 | reasonably practicable. 493 | 494 | b. ShareAlike. 495 | 496 | In addition to the conditions in Section 3(a), if You Share 497 | Adapted Material You produce, the following conditions also apply. 498 | 499 | 1. The Adapter's License You apply must be a Creative Commons 500 | license with the same License Elements, this version or 501 | later, or a BY-NC-SA Compatible License. 502 | 503 | 2. You must include the text of, or the URI or hyperlink to, the 504 | Adapter's License You apply. You may satisfy this condition 505 | in any reasonable manner based on the medium, means, and 506 | context in which You Share Adapted Material. 507 | 508 | 3. You may not offer or impose any additional or different terms 509 | or conditions on, or apply any Effective Technological 510 | Measures to, Adapted Material that restrict exercise of the 511 | rights granted under the Adapter's License You apply. 512 | 513 | 514 | Section 4 -- Sui Generis Database Rights. 515 | 516 | Where the Licensed Rights include Sui Generis Database Rights that 517 | apply to Your use of the Licensed Material: 518 | 519 | a. for the avoidance of doubt, Section 2(a)(1) grants You the right 520 | to extract, reuse, reproduce, and Share all or a substantial 521 | portion of the contents of the database for NonCommercial purposes 522 | only; 523 | 524 | b. if You include all or a substantial portion of the database 525 | contents in a database in which You have Sui Generis Database 526 | Rights, then the database in which You have Sui Generis Database 527 | Rights (but not its individual contents) is Adapted Material, 528 | including for purposes of Section 3(b); and 529 | 530 | c. You must comply with the conditions in Section 3(a) if You Share 531 | all or a substantial portion of the contents of the database. 532 | 533 | For the avoidance of doubt, this Section 4 supplements and does not 534 | replace Your obligations under this Public License where the Licensed 535 | Rights include other Copyright and Similar Rights. 536 | 537 | 538 | Section 5 -- Disclaimer of Warranties and Limitation of Liability. 539 | 540 | a. UNLESS OTHERWISE SEPARATELY UNDERTAKEN BY THE LICENSOR, TO THE 541 | EXTENT POSSIBLE, THE LICENSOR OFFERS THE LICENSED MATERIAL AS-IS 542 | AND AS-AVAILABLE, AND MAKES NO REPRESENTATIONS OR WARRANTIES OF 543 | ANY KIND CONCERNING THE LICENSED MATERIAL, WHETHER EXPRESS, 544 | IMPLIED, STATUTORY, OR OTHER. THIS INCLUDES, WITHOUT LIMITATION, 545 | WARRANTIES OF TITLE, MERCHANTABILITY, FITNESS FOR A PARTICULAR 546 | PURPOSE, NON-INFRINGEMENT, ABSENCE OF LATENT OR OTHER DEFECTS, 547 | ACCURACY, OR THE PRESENCE OR ABSENCE OF ERRORS, WHETHER OR NOT 548 | KNOWN OR DISCOVERABLE. WHERE DISCLAIMERS OF WARRANTIES ARE NOT 549 | ALLOWED IN FULL OR IN PART, THIS DISCLAIMER MAY NOT APPLY TO YOU. 550 | 551 | b. TO THE EXTENT POSSIBLE, IN NO EVENT WILL THE LICENSOR BE LIABLE 552 | TO YOU ON ANY LEGAL THEORY (INCLUDING, WITHOUT LIMITATION, 553 | NEGLIGENCE) OR OTHERWISE FOR ANY DIRECT, SPECIAL, INDIRECT, 554 | INCIDENTAL, CONSEQUENTIAL, PUNITIVE, EXEMPLARY, OR OTHER LOSSES, 555 | COSTS, EXPENSES, OR DAMAGES ARISING OUT OF THIS PUBLIC LICENSE OR 556 | USE OF THE LICENSED MATERIAL, EVEN IF THE LICENSOR HAS BEEN 557 | ADVISED OF THE POSSIBILITY OF SUCH LOSSES, COSTS, EXPENSES, OR 558 | DAMAGES. WHERE A LIMITATION OF LIABILITY IS NOT ALLOWED IN FULL OR 559 | IN PART, THIS LIMITATION MAY NOT APPLY TO YOU. 560 | 561 | c. The disclaimer of warranties and limitation of liability provided 562 | above shall be interpreted in a manner that, to the extent 563 | possible, most closely approximates an absolute disclaimer and 564 | waiver of all liability. 565 | 566 | 567 | Section 6 -- Term and Termination. 568 | 569 | a. This Public License applies for the term of the Copyright and 570 | Similar Rights licensed here. However, if You fail to comply with 571 | this Public License, then Your rights under this Public License 572 | terminate automatically. 573 | 574 | b. Where Your right to use the Licensed Material has terminated under 575 | Section 6(a), it reinstates: 576 | 577 | 1. automatically as of the date the violation is cured, provided 578 | it is cured within 30 days of Your discovery of the 579 | violation; or 580 | 581 | 2. upon express reinstatement by the Licensor. 582 | 583 | For the avoidance of doubt, this Section 6(b) does not affect any 584 | right the Licensor may have to seek remedies for Your violations 585 | of this Public License. 586 | 587 | c. For the avoidance of doubt, the Licensor may also offer the 588 | Licensed Material under separate terms or conditions or stop 589 | distributing the Licensed Material at any time; however, doing so 590 | will not terminate this Public License. 591 | 592 | d. Sections 1, 5, 6, 7, and 8 survive termination of this Public 593 | License. 594 | 595 | 596 | Section 7 -- Other Terms and Conditions. 597 | 598 | a. The Licensor shall not be bound by any additional or different 599 | terms or conditions communicated by You unless expressly agreed. 600 | 601 | b. Any arrangements, understandings, or agreements regarding the 602 | Licensed Material not stated herein are separate from and 603 | independent of the terms and conditions of this Public License. 604 | 605 | 606 | Section 8 -- Interpretation. 607 | 608 | a. For the avoidance of doubt, this Public License does not, and 609 | shall not be interpreted to, reduce, limit, restrict, or impose 610 | conditions on any use of the Licensed Material that could lawfully 611 | be made without permission under this Public License. 612 | 613 | b. To the extent possible, if any provision of this Public License is 614 | deemed unenforceable, it shall be automatically reformed to the 615 | minimum extent necessary to make it enforceable. If the provision 616 | cannot be reformed, it shall be severed from this Public License 617 | without affecting the enforceability of the remaining terms and 618 | conditions. 619 | 620 | c. No term or condition of this Public License will be waived and no 621 | failure to comply consented to unless expressly agreed to by the 622 | Licensor. 623 | 624 | d. Nothing in this Public License constitutes or may be interpreted 625 | as a limitation upon, or waiver of, any privileges and immunities 626 | that apply to the Licensor or You, including from the legal 627 | processes of any jurisdiction or authority. 628 | 629 | ======================================================================= 630 | 631 | Creative Commons is not a party to its public licenses. 632 | Notwithstanding, Creative Commons may elect to apply one of its public 633 | licenses to material it publishes and in those instances will be 634 | considered the "Licensor." Except for the limited purpose of indicating 635 | that material is shared under a Creative Commons public license or as 636 | otherwise permitted by the Creative Commons policies published at 637 | creativecommons.org/policies, Creative Commons does not authorize the 638 | use of the trademark "Creative Commons" or any other trademark or logo 639 | of Creative Commons without its prior written consent including, 640 | without limitation, in connection with any unauthorized modifications 641 | to any of its public licenses or any other arrangements, 642 | understandings, or agreements concerning use of licensed material. For 643 | the avoidance of doubt, this paragraph does not form part of the public 644 | licenses. 645 | 646 | Creative Commons may be contacted at creativecommons.org. 647 | 648 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | Android Camera2BasicStream 3 | =================================== 4 | 5 | This little demo is based on the Camera2Basics example. I really needed to pipe out every frame the 6 | camera provides to a specific endpoint e.g for streaming. The camera2 api is quite new and right now 7 | its a little bit more complicated to grab a frame and convert it to a bitmap for your processing needs. 8 | 9 | The app will work on the nexus4 and the moto x. Why that ? 10 | 11 | The imageformat YUV420SP is not used anymore now you have to deal with yuv_420_888. The problem is YUV_420_888 is still not implemented correctly that means you simply cant use it. It will be fixed in Android (5.1.1). The fallback is jpeg(HAL_PIXEL_FORMAT_RGBA_888) not every camera/mobile delievers that format so the implementation is kinda weak for now and not really fast but it works ! :) 12 | 13 | 14 | Pre-requisites 15 | -------------- 16 | 17 | - Android SDK v23 18 | - Android Build Tools v23.0.1 19 | - Android Support Repository 20 | 21 | Getting Started 22 | --------------- 23 | 24 | This sample uses the Gradle build system. To build this project, use the 25 | "gradlew build" command or use "Import Project" in Android Studio. 26 | -------------------------------------------------------------------------------- /READMEs.md: -------------------------------------------------------------------------------- 1 | 2 | Android Camera2BasicStream 3 | =================================== 4 | 5 | This little demo is based on the Camera2Basics example. I really needed to pipe out every frame the 6 | camera provides to a specific endpoint e.g for streaming. The camera2 api is quite new and right now 7 | its a little bit more complicated grab a frame and convert it to a bitmap for your processing needs. 8 | 9 | The app will work on the nexus4 and the moto x. Why that ? 10 | 11 | The imageformat YUV420SP is not used anymore now you have to deal with yuv_420_888. The problem is YUV_420_888 is still not implemented correcty that means you simply cant use it. It will be fixed in Android (5.1.1). The fallback is jpeg(HAL_PIXEL_FORMAT_RGBA_888) not every camera/mobile delievers that format so the implementation is kinda weak for now and not really fast but it works ! :) 12 | 13 | 14 | Pre-requisites 15 | -------------- 16 | 17 | - Android SDK v23 18 | - Android Build Tools v23.0.1 19 | - Android Support Repository 20 | 21 | Getting Started 22 | --------------- 23 | 24 | This sample uses the Gradle build system. To build this project, use the 25 | "gradlew build" command or use "Import Project" in Android Studio. 26 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | maven { 4 | url 'https://maven.google.com/' 5 | name 'Google' 6 | } 7 | } 8 | } -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SundayLab/Camera2BasicStream/8a961bb5a2bbabf7c154ed25afbfb017c73f561d/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Apr 11 11:28:35 PDT 2018 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /packaging.yaml: -------------------------------------------------------------------------------- 1 | # GOOGLE SAMPLE PACKAGING DATA 2 | # 3 | # This file is used by Google as part of our samples packaging process. 4 | # End users may safely ignore this file. It has no relevance to other systems. 5 | --- 6 | 7 | status: PUBLISHED 8 | technologies: [Android] 9 | categories: [Media] 10 | languages: [Java] 11 | solutions: [Mobile] 12 | github: googlesamples/android-Camera2Basic 13 | level: BEGINNER 14 | icon: Camera2BasicSample/src/main/res/drawable-xxhdpi/ic_launcher.png 15 | license: apache2 16 | -------------------------------------------------------------------------------- /screenshots/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SundayLab/Camera2BasicStream/8a961bb5a2bbabf7c154ed25afbfb017c73f561d/screenshots/.DS_Store -------------------------------------------------------------------------------- /screenshots/icon-web.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SundayLab/Camera2BasicStream/8a961bb5a2bbabf7c154ed25afbfb017c73f561d/screenshots/icon-web.png -------------------------------------------------------------------------------- /screenshots/main.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SundayLab/Camera2BasicStream/8a961bb5a2bbabf7c154ed25afbfb017c73f561d/screenshots/main.png -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include 'Application' 2 | --------------------------------------------------------------------------------