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.app.AlertDialog; 21 import android.app.Dialog; 22 import android.app.DialogFragment; 23 import android.app.Fragment; 24 import android.content.Context; 25 import android.content.DialogInterface; 26 import android.content.res.Configuration; 27 import android.graphics.ImageFormat; 28 import android.graphics.Matrix; 29 import android.graphics.RectF; 30 import android.graphics.SurfaceTexture; 31 import android.hardware.camera2.CameraAccessException; 32 import android.hardware.camera2.CameraCaptureSession; 33 import android.hardware.camera2.CameraCharacteristics; 34 import android.hardware.camera2.CameraDevice; 35 import android.hardware.camera2.CameraManager; 36 import android.hardware.camera2.CameraMetadata; 37 import android.hardware.camera2.CaptureRequest; 38 import android.hardware.camera2.CaptureResult; 39 import android.hardware.camera2.TotalCaptureResult; 40 import android.hardware.camera2.params.StreamConfigurationMap; 41 import android.media.Image; 42 import android.media.ImageReader; 43 import android.os.Bundle; 44 import android.os.Handler; 45 import android.os.HandlerThread; 46 import android.util.Log; 47 import android.util.Size; 48 import android.util.SparseIntArray; 49 import android.view.LayoutInflater; 50 import android.view.Surface; 51 import android.view.TextureView; 52 import android.view.View; 53 import android.view.ViewGroup; 54 import android.widget.Toast; 55 56 import java.io.File; 57 import java.io.FileNotFoundException; 58 import java.io.FileOutputStream; 59 import java.io.IOException; 60 import java.nio.ByteBuffer; 61 import java.util.ArrayList; 62 import java.util.Arrays; 63 import java.util.Collections; 64 import java.util.Comparator; 65 import java.util.List; 66 import java.util.concurrent.Semaphore; 67 import java.util.concurrent.TimeUnit; 68 69 public class Camera2BasicFragment extends Fragment implements View.OnClickListener { 70 71 /** 72 * Conversion from screen rotation to JPEG orientation. 73 */ 74 private static final SparseIntArray ORIENTATIONS = new SparseIntArray(); 75 76 static { ORIENTATIONS.append(Surface.ROTATION_0, 90)77 ORIENTATIONS.append(Surface.ROTATION_0, 90); ORIENTATIONS.append(Surface.ROTATION_90, 0)78 ORIENTATIONS.append(Surface.ROTATION_90, 0); ORIENTATIONS.append(Surface.ROTATION_180, 270)79 ORIENTATIONS.append(Surface.ROTATION_180, 270); ORIENTATIONS.append(Surface.ROTATION_270, 180)80 ORIENTATIONS.append(Surface.ROTATION_270, 180); 81 } 82 83 /** 84 * Tag for the {@link Log}. 85 */ 86 private static final String TAG = "Camera2BasicFragment"; 87 88 /** 89 * Camera state: Showing camera preview. 90 */ 91 private static final int STATE_PREVIEW = 0; 92 93 /** 94 * Camera state: Waiting for the focus to be locked. 95 */ 96 private static final int STATE_WAITING_LOCK = 1; 97 /** 98 * Camera state: Waiting for the exposure to be precapture state. 99 */ 100 private static final int STATE_WAITING_PRECAPTURE = 2; 101 /** 102 * Camera state: Waiting for the exposure state to be something other than precapture. 103 */ 104 private static final int STATE_WAITING_NON_PRECAPTURE = 3; 105 /** 106 * Camera state: Picture was taken. 107 */ 108 private static final int STATE_PICTURE_TAKEN = 4; 109 110 /** 111 * {@link TextureView.SurfaceTextureListener} handles several lifecycle events on a 112 * {@link TextureView}. 113 */ 114 private final TextureView.SurfaceTextureListener mSurfaceTextureListener 115 = new TextureView.SurfaceTextureListener() { 116 117 @Override 118 public void onSurfaceTextureAvailable(SurfaceTexture texture, int width, int height) { 119 openCamera(width, height); 120 } 121 122 @Override 123 public void onSurfaceTextureSizeChanged(SurfaceTexture texture, int width, int height) { 124 configureTransform(width, height); 125 } 126 127 @Override 128 public boolean onSurfaceTextureDestroyed(SurfaceTexture texture) { 129 return true; 130 } 131 132 @Override 133 public void onSurfaceTextureUpdated(SurfaceTexture texture) { 134 } 135 136 }; 137 138 /** 139 * ID of the current {@link CameraDevice}. 140 */ 141 private String mCameraId; 142 143 /** 144 * An {@link AutoFitTextureView} for camera preview. 145 */ 146 private AutoFitTextureView mTextureView; 147 148 /** 149 * A {@link CameraCaptureSession } for camera preview. 150 */ 151 152 private CameraCaptureSession mCaptureSession; 153 /** 154 * A reference to the opened {@link CameraDevice}. 155 */ 156 157 private CameraDevice mCameraDevice; 158 /** 159 * The {@link android.util.Size} of camera preview. 160 */ 161 162 private Size mPreviewSize; 163 164 /** 165 * {@link CameraDevice.StateCallback} is called when {@link CameraDevice} changes its state. 166 */ 167 private final CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() { 168 169 @Override 170 public void onOpened(CameraDevice cameraDevice) { 171 // This method is called when the camera is opened. We start camera preview here. 172 mCameraOpenCloseLock.release(); 173 mCameraDevice = cameraDevice; 174 createCameraPreviewSession(); 175 } 176 177 @Override 178 public void onDisconnected(CameraDevice cameraDevice) { 179 mCameraOpenCloseLock.release(); 180 cameraDevice.close(); 181 mCameraDevice = null; 182 } 183 184 @Override 185 public void onError(CameraDevice cameraDevice, int error) { 186 mCameraOpenCloseLock.release(); 187 cameraDevice.close(); 188 mCameraDevice = null; 189 Activity activity = getActivity(); 190 if (null != activity) { 191 activity.finish(); 192 } 193 } 194 195 }; 196 197 /** 198 * An additional thread for running tasks that shouldn't block the UI. 199 */ 200 private HandlerThread mBackgroundThread; 201 202 /** 203 * A {@link Handler} for running tasks in the background. 204 */ 205 private Handler mBackgroundHandler; 206 207 /** 208 * An {@link ImageReader} that handles still image capture. 209 */ 210 private ImageReader mImageReader; 211 212 /** 213 * This is the output file for our picture. 214 */ 215 private File mFile; 216 217 /** 218 * This a callback object for the {@link ImageReader}. "onImageAvailable" will be called when a 219 * still image is ready to be saved. 220 */ 221 private final ImageReader.OnImageAvailableListener mOnImageAvailableListener 222 = new ImageReader.OnImageAvailableListener() { 223 224 @Override 225 public void onImageAvailable(ImageReader reader) { 226 mBackgroundHandler.post(new ImageSaver(reader.acquireNextImage(), mFile)); 227 } 228 229 }; 230 231 /** 232 * {@link CaptureRequest.Builder} for the camera preview 233 */ 234 private CaptureRequest.Builder mPreviewRequestBuilder; 235 236 /** 237 * {@link CaptureRequest} generated by {@link #mPreviewRequestBuilder} 238 */ 239 private CaptureRequest mPreviewRequest; 240 241 /** 242 * The current state of camera state for taking pictures. 243 * 244 * @see #mCaptureCallback 245 */ 246 private int mState = STATE_PREVIEW; 247 248 /** 249 * A {@link Semaphore} to prevent the app from exiting before closing the camera. 250 */ 251 private Semaphore mCameraOpenCloseLock = new Semaphore(1); 252 253 /** 254 * A {@link CameraCaptureSession.CaptureCallback} that handles events related to JPEG capture. 255 */ 256 private CameraCaptureSession.CaptureCallback mCaptureCallback 257 = new CameraCaptureSession.CaptureCallback() { 258 259 private void process(CaptureResult result) { 260 switch (mState) { 261 case STATE_PREVIEW: { 262 // We have nothing to do when the camera preview is working normally. 263 break; 264 } 265 case STATE_WAITING_LOCK: { 266 int afState = result.get(CaptureResult.CONTROL_AF_STATE); 267 if (CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED == afState || 268 CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED == afState) { 269 int aeState = result.get(CaptureResult.CONTROL_AE_STATE); 270 if (aeState == CaptureResult.CONTROL_AE_STATE_CONVERGED) { 271 mState = STATE_WAITING_NON_PRECAPTURE; 272 captureStillPicture(); 273 } else { 274 runPrecaptureSequence(); 275 } 276 } 277 break; 278 } 279 case STATE_WAITING_PRECAPTURE: { 280 int aeState = result.get(CaptureResult.CONTROL_AE_STATE); 281 if (CaptureResult.CONTROL_AE_STATE_PRECAPTURE == aeState) { 282 mState = STATE_WAITING_NON_PRECAPTURE; 283 } else if (CaptureRequest.CONTROL_AE_STATE_FLASH_REQUIRED == aeState) { 284 mState = STATE_WAITING_NON_PRECAPTURE; 285 } 286 break; 287 } 288 case STATE_WAITING_NON_PRECAPTURE: { 289 int aeState = result.get(CaptureResult.CONTROL_AE_STATE); 290 if (CaptureResult.CONTROL_AE_STATE_PRECAPTURE != aeState) { 291 mState = STATE_PICTURE_TAKEN; 292 captureStillPicture(); 293 } 294 break; 295 } 296 } 297 } 298 299 @Override 300 public void onCaptureProgressed(CameraCaptureSession session, CaptureRequest request, 301 CaptureResult partialResult) { 302 process(partialResult); 303 } 304 305 @Override 306 public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, 307 TotalCaptureResult result) { 308 process(result); 309 } 310 311 }; 312 313 /** 314 * Given {@code choices} of {@code Size}s supported by a camera, chooses the smallest one whose 315 * width and height are at least as large as the respective requested values, and whose aspect 316 * ratio matches with the specified value. 317 * 318 * @param choices The list of sizes that the camera supports for the intended output class 319 * @param width The minimum desired width 320 * @param height The minimum desired height 321 * @param aspectRatio The aspect ratio 322 * @return The optimal {@code Size}, or an arbitrary one if none were big enough 323 */ chooseOptimalSize(Size[] choices, int width, int height, Size aspectRatio)324 private static Size chooseOptimalSize(Size[] choices, int width, int height, Size aspectRatio) { 325 // Collect the supported resolutions that are at least as big as the preview Surface 326 List<Size> bigEnough = new ArrayList<Size>(); 327 int w = aspectRatio.getWidth(); 328 int h = aspectRatio.getHeight(); 329 for (Size option : choices) { 330 if (option.getHeight() == option.getWidth() * h / w && 331 option.getWidth() >= width && option.getHeight() >= height) { 332 bigEnough.add(option); 333 } 334 } 335 336 // Pick the smallest of those, assuming we found any 337 if (bigEnough.size() > 0) { 338 return Collections.min(bigEnough, new CompareSizesByArea()); 339 } else { 340 Log.e(TAG, "Couldn't find any suitable preview size"); 341 return choices[0]; 342 } 343 } 344 newInstance()345 public static Camera2BasicFragment newInstance() { 346 Camera2BasicFragment fragment = new Camera2BasicFragment(); 347 fragment.setRetainInstance(true); 348 return fragment; 349 } 350 351 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)352 public View onCreateView(LayoutInflater inflater, ViewGroup container, 353 Bundle savedInstanceState) { 354 return inflater.inflate(R.layout.fragment_camera2_basic, container, false); 355 } 356 357 @Override onViewCreated(final View view, Bundle savedInstanceState)358 public void onViewCreated(final View view, Bundle savedInstanceState) { 359 view.findViewById(R.id.picture).setOnClickListener(this); 360 view.findViewById(R.id.info).setOnClickListener(this); 361 mTextureView = (AutoFitTextureView) view.findViewById(R.id.texture); 362 } 363 364 @Override onActivityCreated(Bundle savedInstanceState)365 public void onActivityCreated(Bundle savedInstanceState) { 366 super.onActivityCreated(savedInstanceState); 367 mFile = new File(getActivity().getExternalFilesDir(null), "pic.jpg"); 368 } 369 370 @Override onResume()371 public void onResume() { 372 super.onResume(); 373 startBackgroundThread(); 374 375 // When the screen is turned off and turned back on, the SurfaceTexture is already 376 // available, and "onSurfaceTextureAvailable" will not be called. In that case, we can open 377 // a camera and start preview from here (otherwise, we wait until the surface is ready in 378 // the SurfaceTextureListener). 379 if (mTextureView.isAvailable()) { 380 openCamera(mTextureView.getWidth(), mTextureView.getHeight()); 381 } else { 382 mTextureView.setSurfaceTextureListener(mSurfaceTextureListener); 383 } 384 } 385 386 @Override onPause()387 public void onPause() { 388 closeCamera(); 389 stopBackgroundThread(); 390 super.onPause(); 391 } 392 393 /** 394 * Sets up member variables related to camera. 395 * 396 * @param width The width of available size for camera preview 397 * @param height The height of available size for camera preview 398 */ setUpCameraOutputs(int width, int height)399 private void setUpCameraOutputs(int width, int height) { 400 Activity activity = getActivity(); 401 CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE); 402 try { 403 for (String cameraId : manager.getCameraIdList()) { 404 CameraCharacteristics characteristics 405 = manager.getCameraCharacteristics(cameraId); 406 407 // We don't use a front facing camera in this sample. 408 if (characteristics.get(CameraCharacteristics.LENS_FACING) 409 == CameraCharacteristics.LENS_FACING_FRONT) { 410 continue; 411 } 412 413 StreamConfigurationMap map = characteristics.get( 414 CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); 415 416 // For still image captures, we use the largest available size. 417 Size largest = Collections.max( 418 Arrays.asList(map.getOutputSizes(ImageFormat.JPEG)), 419 new CompareSizesByArea()); 420 mImageReader = ImageReader.newInstance(largest.getWidth(), largest.getHeight(), 421 ImageFormat.JPEG, /*maxImages*/2); 422 mImageReader.setOnImageAvailableListener( 423 mOnImageAvailableListener, mBackgroundHandler); 424 425 // Danger, W.R.! Attempting to use too large a preview size could exceed the camera 426 // bus' bandwidth limitation, resulting in gorgeous previews but the storage of 427 // garbage capture data. 428 mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), 429 width, height, largest); 430 431 // We fit the aspect ratio of TextureView to the size of preview we picked. 432 int orientation = getResources().getConfiguration().orientation; 433 if (orientation == Configuration.ORIENTATION_LANDSCAPE) { 434 mTextureView.setAspectRatio( 435 mPreviewSize.getWidth(), mPreviewSize.getHeight()); 436 } else { 437 mTextureView.setAspectRatio( 438 mPreviewSize.getHeight(), mPreviewSize.getWidth()); 439 } 440 441 mCameraId = cameraId; 442 return; 443 } 444 } catch (CameraAccessException e) { 445 e.printStackTrace(); 446 } catch (NullPointerException e) { 447 // Currently an NPE is thrown when the Camera2API is used but not supported on the 448 // device this code runs. 449 new ErrorDialog().show(getFragmentManager(), "dialog"); 450 } 451 } 452 453 /** 454 * Opens the camera specified by {@link Camera2BasicFragment#mCameraId}. 455 */ openCamera(int width, int height)456 private void openCamera(int width, int height) { 457 setUpCameraOutputs(width, height); 458 configureTransform(width, height); 459 Activity activity = getActivity(); 460 CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE); 461 try { 462 if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) { 463 throw new RuntimeException("Time out waiting to lock camera opening."); 464 } 465 manager.openCamera(mCameraId, mStateCallback, mBackgroundHandler); 466 } catch (CameraAccessException e) { 467 e.printStackTrace(); 468 } catch (InterruptedException e) { 469 throw new RuntimeException("Interrupted while trying to lock camera opening.", e); 470 } 471 } 472 473 /** 474 * Closes the current {@link CameraDevice}. 475 */ closeCamera()476 private void closeCamera() { 477 try { 478 mCameraOpenCloseLock.acquire(); 479 if (null != mCaptureSession) { 480 mCaptureSession.close(); 481 mCaptureSession = null; 482 } 483 if (null != mCameraDevice) { 484 mCameraDevice.close(); 485 mCameraDevice = null; 486 } 487 if (null != mImageReader) { 488 mImageReader.close(); 489 mImageReader = null; 490 } 491 } catch (InterruptedException e) { 492 throw new RuntimeException("Interrupted while trying to lock camera closing.", e); 493 } finally { 494 mCameraOpenCloseLock.release(); 495 } 496 } 497 498 /** 499 * Starts a background thread and its {@link Handler}. 500 */ startBackgroundThread()501 private void startBackgroundThread() { 502 mBackgroundThread = new HandlerThread("CameraBackground"); 503 mBackgroundThread.start(); 504 mBackgroundHandler = new Handler(mBackgroundThread.getLooper()); 505 } 506 507 /** 508 * Stops the background thread and its {@link Handler}. 509 */ stopBackgroundThread()510 private void stopBackgroundThread() { 511 mBackgroundThread.quitSafely(); 512 try { 513 mBackgroundThread.join(); 514 mBackgroundThread = null; 515 mBackgroundHandler = null; 516 } catch (InterruptedException e) { 517 e.printStackTrace(); 518 } 519 } 520 521 /** 522 * Creates a new {@link CameraCaptureSession} for camera preview. 523 */ createCameraPreviewSession()524 private void createCameraPreviewSession() { 525 try { 526 SurfaceTexture texture = mTextureView.getSurfaceTexture(); 527 assert texture != null; 528 529 // We configure the size of default buffer to be the size of camera preview we want. 530 texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight()); 531 532 // This is the output Surface we need to start preview. 533 Surface surface = new Surface(texture); 534 535 // We set up a CaptureRequest.Builder with the output Surface. 536 mPreviewRequestBuilder 537 = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); 538 mPreviewRequestBuilder.addTarget(surface); 539 540 // Here, we create a CameraCaptureSession for camera preview. 541 mCameraDevice.createCaptureSession(Arrays.asList(surface, mImageReader.getSurface()), 542 new CameraCaptureSession.StateCallback() { 543 544 @Override 545 public void onConfigured(CameraCaptureSession cameraCaptureSession) { 546 // The camera is already closed 547 if (null == mCameraDevice) { 548 return; 549 } 550 551 // When the session is ready, we start displaying the preview. 552 mCaptureSession = cameraCaptureSession; 553 try { 554 // Auto focus should be continuous for camera preview. 555 mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, 556 CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE); 557 // Flash is automatically enabled when necessary. 558 mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, 559 CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH); 560 561 // Finally, we start displaying the camera preview. 562 mPreviewRequest = mPreviewRequestBuilder.build(); 563 mCaptureSession.setRepeatingRequest(mPreviewRequest, 564 mCaptureCallback, mBackgroundHandler); 565 } catch (CameraAccessException e) { 566 e.printStackTrace(); 567 } 568 } 569 570 @Override 571 public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) { 572 Activity activity = getActivity(); 573 if (null != activity) { 574 Toast.makeText(activity, "Failed", Toast.LENGTH_SHORT).show(); 575 } 576 } 577 }, null 578 ); 579 } catch (CameraAccessException e) { 580 e.printStackTrace(); 581 } 582 } 583 584 /** 585 * Configures the necessary {@link android.graphics.Matrix} transformation to `mTextureView`. 586 * This method should be called after the camera preview size is determined in 587 * setUpCameraOutputs and also the size of `mTextureView` is fixed. 588 * 589 * @param viewWidth The width of `mTextureView` 590 * @param viewHeight The height of `mTextureView` 591 */ configureTransform(int viewWidth, int viewHeight)592 private void configureTransform(int viewWidth, int viewHeight) { 593 Activity activity = getActivity(); 594 if (null == mTextureView || null == mPreviewSize || null == activity) { 595 return; 596 } 597 int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); 598 Matrix matrix = new Matrix(); 599 RectF viewRect = new RectF(0, 0, viewWidth, viewHeight); 600 RectF bufferRect = new RectF(0, 0, mPreviewSize.getHeight(), mPreviewSize.getWidth()); 601 float centerX = viewRect.centerX(); 602 float centerY = viewRect.centerY(); 603 if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) { 604 bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY()); 605 matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL); 606 float scale = Math.max( 607 (float) viewHeight / mPreviewSize.getHeight(), 608 (float) viewWidth / mPreviewSize.getWidth()); 609 matrix.postScale(scale, scale, centerX, centerY); 610 matrix.postRotate(90 * (rotation - 2), centerX, centerY); 611 } 612 mTextureView.setTransform(matrix); 613 } 614 615 /** 616 * Initiate a still image capture. 617 */ takePicture()618 private void takePicture() { 619 lockFocus(); 620 } 621 622 /** 623 * Lock the focus as the first step for a still image capture. 624 */ lockFocus()625 private void lockFocus() { 626 try { 627 // This is how to tell the camera to lock focus. 628 mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, 629 CameraMetadata.CONTROL_AF_TRIGGER_START); 630 // Tell #mCaptureCallback to wait for the lock. 631 mState = STATE_WAITING_LOCK; 632 mCaptureSession.setRepeatingRequest(mPreviewRequestBuilder.build(), mCaptureCallback, 633 mBackgroundHandler); 634 } catch (CameraAccessException e) { 635 e.printStackTrace(); 636 } 637 } 638 639 /** 640 * Run the precapture sequence for capturing a still image. This method should be called when we 641 * get a response in {@link #mCaptureCallback} from {@link #lockFocus()}. 642 */ runPrecaptureSequence()643 private void runPrecaptureSequence() { 644 try { 645 // This is how to tell the camera to trigger. 646 mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER, 647 CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER_START); 648 // Tell #mCaptureCallback to wait for the precapture sequence to be set. 649 mState = STATE_WAITING_PRECAPTURE; 650 mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, 651 mBackgroundHandler); 652 } catch (CameraAccessException e) { 653 e.printStackTrace(); 654 } 655 } 656 657 /** 658 * Capture a still picture. This method should be called when we get a response in 659 * {@link #mCaptureCallback} from both {@link #lockFocus()}. 660 */ captureStillPicture()661 private void captureStillPicture() { 662 try { 663 final Activity activity = getActivity(); 664 if (null == activity || null == mCameraDevice) { 665 return; 666 } 667 // This is the CaptureRequest.Builder that we use to take a picture. 668 final CaptureRequest.Builder captureBuilder = 669 mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE); 670 captureBuilder.addTarget(mImageReader.getSurface()); 671 672 // Use the same AE and AF modes as the preview. 673 captureBuilder.set(CaptureRequest.CONTROL_AF_MODE, 674 CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE); 675 captureBuilder.set(CaptureRequest.CONTROL_AE_MODE, 676 CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH); 677 678 // Orientation 679 int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); 680 captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, ORIENTATIONS.get(rotation)); 681 682 CameraCaptureSession.CaptureCallback CaptureCallback 683 = new CameraCaptureSession.CaptureCallback() { 684 685 @Override 686 public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, 687 TotalCaptureResult result) { 688 Toast.makeText(getActivity(), "Saved: " + mFile, Toast.LENGTH_SHORT).show(); 689 unlockFocus(); 690 } 691 }; 692 693 mCaptureSession.stopRepeating(); 694 mCaptureSession.capture(captureBuilder.build(), CaptureCallback, null); 695 } catch (CameraAccessException e) { 696 e.printStackTrace(); 697 } 698 } 699 700 /** 701 * Unlock the focus. This method should be called when still image capture sequence is finished. 702 */ unlockFocus()703 private void unlockFocus() { 704 try { 705 // Reset the autofucos trigger 706 mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, 707 CameraMetadata.CONTROL_AF_TRIGGER_CANCEL); 708 mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, 709 CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH); 710 mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, 711 mBackgroundHandler); 712 // After this, the camera will go back to the normal state of preview. 713 mState = STATE_PREVIEW; 714 mCaptureSession.setRepeatingRequest(mPreviewRequest, mCaptureCallback, 715 mBackgroundHandler); 716 } catch (CameraAccessException e) { 717 e.printStackTrace(); 718 } 719 } 720 721 @Override onClick(View view)722 public void onClick(View view) { 723 switch (view.getId()) { 724 case R.id.picture: { 725 takePicture(); 726 break; 727 } 728 case R.id.info: { 729 Activity activity = getActivity(); 730 if (null != activity) { 731 new AlertDialog.Builder(activity) 732 .setMessage(R.string.intro_message) 733 .setPositiveButton(android.R.string.ok, null) 734 .show(); 735 } 736 break; 737 } 738 } 739 } 740 741 /** 742 * Saves a JPEG {@link Image} into the specified {@link File}. 743 */ 744 private static class ImageSaver implements Runnable { 745 746 /** 747 * The JPEG image 748 */ 749 private final Image mImage; 750 /** 751 * The file we save the image into. 752 */ 753 private final File mFile; 754 ImageSaver(Image image, File file)755 public ImageSaver(Image image, File file) { 756 mImage = image; 757 mFile = file; 758 } 759 760 @Override run()761 public void run() { 762 ByteBuffer buffer = mImage.getPlanes()[0].getBuffer(); 763 byte[] bytes = new byte[buffer.remaining()]; 764 buffer.get(bytes); 765 FileOutputStream output = null; 766 try { 767 output = new FileOutputStream(mFile); 768 output.write(bytes); 769 } catch (FileNotFoundException e) { 770 e.printStackTrace(); 771 } catch (IOException e) { 772 e.printStackTrace(); 773 } finally { 774 mImage.close(); 775 if (null != output) { 776 try { 777 output.close(); 778 } catch (IOException e) { 779 e.printStackTrace(); 780 } 781 } 782 } 783 } 784 785 } 786 787 /** 788 * Compares two {@code Size}s based on their areas. 789 */ 790 static class CompareSizesByArea implements Comparator<Size> { 791 792 @Override compare(Size lhs, Size rhs)793 public int compare(Size lhs, Size rhs) { 794 // We cast here to ensure the multiplications won't overflow 795 return Long.signum((long) lhs.getWidth() * lhs.getHeight() - 796 (long) rhs.getWidth() * rhs.getHeight()); 797 } 798 799 } 800 801 public static class ErrorDialog extends DialogFragment { 802 803 @Override onCreateDialog(Bundle savedInstanceState)804 public Dialog onCreateDialog(Bundle savedInstanceState) { 805 final Activity activity = getActivity(); 806 return new AlertDialog.Builder(activity) 807 .setMessage("This device doesn't support Camera2 API.") 808 .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { 809 @Override 810 public void onClick(DialogInterface dialogInterface, int i) { 811 activity.finish(); 812 } 813 }) 814 .create(); 815 } 816 817 } 818 819 } 820