/** * does setup the repeating capture request for taking images for the preview */ @VisibleForTesting void setupCaptureRequestForPreview(@NonNull CameraCaptureSession previewSession, @NonNull CameraDevice camera, @NonNull List<Surface> surfaces) { try { CaptureRequest.Builder previewRequestBuilder = camera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); for (Surface surface : surfaces) { previewRequestBuilder.addTarget(surface); } previewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE); previewSession.setRepeatingRequest(previewRequestBuilder.build(), null, null); } catch (CameraAccessException | IllegalStateException e) { throw new CameraException(e); } }
private void startPreview() { try { CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraId); StreamConfigurationMap configMap = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); Size previewSize = Util.getPreferredPreviewSize( configMap.getOutputSizes(ImageFormat.JPEG),textureView.getWidth(), textureView.getHeight()); surfaceTexture.setDefaultBufferSize(previewSize.getWidth(),previewSize.getHeight()); Surface surface = new Surface(surfaceTexture); captureBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); captureBuilder.addTarget(surface); cameraDevice.createCaptureSession(Arrays.asList(surface),captureSessionCallback,backgroundHandler); } catch (CameraAccessException e) { e.printStackTrace(); } }
private void createCameraPreviewSession() { if (mCamera == null) { return; } SurfaceHolder holder = surfaceView.getHolder(); // if (config != null) { // holder.setFixedSize(640, 480); // } Surface surface = holder.getSurface(); List<Surface> surfaceList = Arrays.asList(surface, mImageReader.getSurface()); try { mPreviewBuilder = mCamera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); mPreviewBuilder.addTarget(surface); mCamera.createCaptureSession(surfaceList, sessionCallback, mHandler); } catch (CameraAccessException e) { e.printStackTrace(); } }
@Override public void onOpened(CameraDevice camera) { checkIsOnCameraThread(); Logging.d(TAG, "Camera opened."); cameraDevice = camera; final SurfaceTexture surfaceTexture = surfaceTextureHelper.getSurfaceTexture(); surfaceTexture.setDefaultBufferSize(captureFormat.width, captureFormat.height); surface = new Surface(surfaceTexture); List<Surface> surfaces = new ArrayList<Surface>(); surfaces.add(surface); if (mediaRecorderSurface != null) { Logging.d(TAG, "Add MediaRecorder surface to capture session."); surfaces.add(mediaRecorderSurface); } try { camera.createCaptureSession(surfaces, new CaptureSessionCallback(), cameraThreadHandler); } catch (CameraAccessException e) { reportError("Failed to create capture session. " + e); return; } }
private String getErrorDescription(int errorCode) { switch (errorCode) { case CameraDevice.StateCallback.ERROR_CAMERA_DEVICE: return "Camera device has encountered a fatal error."; case CameraDevice.StateCallback.ERROR_CAMERA_DISABLED: return "Camera device could not be opened due to a device policy."; case CameraDevice.StateCallback.ERROR_CAMERA_IN_USE: return "Camera device is in use already."; case CameraDevice.StateCallback.ERROR_CAMERA_SERVICE: return "Camera service has encountered a fatal error."; case CameraDevice.StateCallback.ERROR_MAX_CAMERAS_IN_USE: return "Camera device could not be opened because" + " there are too many other open camera devices."; default: return "Unknown camera error: " + errorCode; } }
@Override public void onClosed(CameraDevice cameraDevice) { Logging.d(TAG, "Simple camera2 closed."); synchronized (cameraDeviceLock) { SimpleCamera2.this.cameraDevice = null; } }
@Override public void onOpened(final CameraDevice cd) { // This method is called when the camera is opened. We start camera preview here. cameraOpenCloseLock.release(); cameraDevice = cd; createCameraPreviewSession(); }
@Override public void onOpened(@NonNull CameraDevice cameraDevice) { // This method is called when the camera is opened. We start camera preview here. mCameraOpenCloseLock.release(); mCameraDevice = cameraDevice; createCameraPreviewSession(); }
@Override public void onError(@NonNull CameraDevice cameraDevice, int error) { mCameraOpenCloseLock.release(); cameraDevice.close(); mCameraDevice = null; Activity activity = getActivity(); if (null != activity) { activity.finish(); } }
@Override public void onClosed(@NonNull CameraDevice camera) { cameraClosed = true; Log.d(TAG, "camera " + camera.getId() + " closed"); //once the current camera has been closed, start taking another picture if (!cameraIds.isEmpty()) { takeAnotherPicture(); } else { capturingListener.onDoneCapturingAllPhotos(picturesTaken); } }
/** * Execute a new capture request within the active session */ private void triggerImageCapture() { try { final CaptureRequest.Builder captureBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE); captureBuilder.addTarget(mImageReader.getSurface()); captureBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON); Log.d(TAG, "Capture request created."); mCaptureSession.capture(captureBuilder.build(), mCaptureCallback, null); } catch (CameraAccessException cae) { Log.d(TAG, "camera capture exception"); } catch (IllegalStateException e) { Log.d(TAG, "camera freaked out"); } }
/** * Capture a still picture. This method should be called when we get a response in * {@link #mCaptureCallback} from both {@link #lockFocus()}. */ private void captureStillPicture() { try { if (null == mCameraDevice) { return; } if(mShutterCallback != null) { mShutterCallback.onShutter(); } // This is the CaptureRequest.Builder that we use to take a picture. final CaptureRequest.Builder captureBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE); captureBuilder.addTarget(mImageReaderStill.getSurface()); // Use the same AE and AF modes as the preview. captureBuilder.set(CaptureRequest.CONTROL_AF_MODE, mFocusMode); if(mFlashSupported) { captureBuilder.set(CaptureRequest.CONTROL_AE_MODE, mFlashMode); } // Orientation captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getOrientation(mDisplayOrientation)); CameraCaptureSession.CaptureCallback CaptureCallback = new CameraCaptureSession.CaptureCallback() { @Override public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) { unlockFocus(); } }; mCaptureSession.stopRepeating(); mCaptureSession.capture(captureBuilder.build(), CaptureCallback, null); } catch (CameraAccessException e) { e.printStackTrace(); } }
@Override public void onOpened(CameraDevice cameraDevice) { Camera2Manager.this.cameraDevice = cameraDevice; if (cameraOpenListener != null) { uiHandler.post(new Runnable() { @Override public void run() { if (!TextUtils.isEmpty(currentCameraId) && previewSize != null) cameraOpenListener.onCameraOpened(currentCameraId, previewSize, Camera2Manager.this); } }); } }
@Override public void onDisconnected(CameraDevice cameraDevice) { cameraDevice.close(); Camera2Manager.this.cameraDevice = null; uiHandler.post(new Runnable() { @Override public void run() { cameraOpenListener.onCameraOpenError(); } }); }
private void captureStillPicture() { try { if (null == cameraDevice) { return; } final CaptureRequest.Builder captureBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE); captureBuilder.addTarget(imageReader.getSurface()); captureBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE); captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getPhotoOrientation(configurationProvider.getSensorPosition())); CameraCaptureSession.CaptureCallback CaptureCallback = new CameraCaptureSession.CaptureCallback() { @Override public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) { Log.d(TAG, "onCaptureCompleted: "); } }; captureSession.stopRepeating(); captureSession.capture(captureBuilder.build(), CaptureCallback, null); } catch (CameraAccessException e) { Log.e(TAG, "Error during capturing picture"); } }
/** * <p>Starts a capture session for camera preview.</p> * <p>This rewrites {@link #mPreviewRequestBuilder}.</p> * <p>The result will be continuously processed in {@link #mSessionCallback}.</p> */ void startCaptureSession() { if (!isCameraOpened() || !mPreview.isReady() || mImageReader == null) { return; } Size previewSize = chooseOptimalSize(); mPreview.setBufferSize(previewSize.getWidth(), previewSize.getHeight()); Surface surface = mPreview.getSurface(); try { mPreviewRequestBuilder = mCamera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); mPreviewRequestBuilder.addTarget(surface); mCamera.createCaptureSession(Arrays.asList(surface, mImageReader.getSurface()), mSessionCallback, null); } catch (CameraAccessException e) { throw new RuntimeException("Failed to start camera session"); } }
@Override public void takePhoto(boolean isFront, int degree, PhotoTakenCallback callback) { mIsFront = isFront; mDegree = degree; mPhotoTakenCallback = callback; CaptureRequest.Builder captureRequestBuilder; try { captureRequestBuilder = mCamera.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE); // 将ImageReader的surface作为CaptureRequest.Builder的目标 captureRequestBuilder.addTarget(mImageReader.getSurface()); // 自动对焦 captureRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE); // 自动曝光 captureRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH); // 设置照片方向 if (mIsFront) { captureRequestBuilder.set(CaptureRequest.JPEG_ORIENTATION, 270); } else { captureRequestBuilder.set(CaptureRequest.JPEG_ORIENTATION, 90); } // 拍照 CaptureRequest captureRequest = captureRequestBuilder.build(); mCameraCaptureSession.capture(captureRequest, null, mPreviewHandler); } catch (CameraAccessException e) { e.printStackTrace(); } }
/** * Execute a new capture request within the active session */ private void triggerImageCapture() { try { final CaptureRequest.Builder captureBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE); captureBuilder.addTarget(mImageReader.getSurface()); captureBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON); Log.d(TAG, "Capture request created."); mCaptureSession.capture(captureBuilder.build(), mCaptureCallback, null); } catch (CameraAccessException cae) { Log.d(TAG, "camera capture exception"); } }
@Override public void onOpened(@NonNull CameraDevice cameraDevice) { this.cameraDevice = cameraDevice; startPreview(cameraDevice); Log.i(TAG, "camera opened"); }
@Override public void onError(final CameraDevice cd, final int error) { cameraOpenCloseLock.release(); cd.close(); cameraDevice = null; final Activity activity = getActivity(); if (null != activity) { activity.finish(); } }
private boolean sendRepeatPreviewRequest() { try { CaptureRequest.Builder builder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); builder.addTarget(mPreviewSurface); builder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO); builder.set(CaptureRequest.CONTROL_AF_MODE, mControlAFMode.switchToCamera2FocusMode()); mCaptureSession.setRepeatingRequest(builder.build(), mPreCaptureCallback, mBackgroundHandler); return true; } catch (CameraAccessException e) { e.printStackTrace(); return false; } }
@Override public void onOpened(CameraDevice camera) { //This is called when the camera is open Log.e(TAG, "onOpened"); cameraDevice = camera; createCameraPreview(); }
/** * Capture a still picture. This method should be called when we get a response in * {@link #mCaptureCallback} from both {@link #lockFocus()}. */ private void captureStillPicture() { try { final Activity activity = getActivity(); if (null == activity || null == mCameraDevice) { return; } // This is the CaptureRequest.Builder that we use to take a picture. final CaptureRequest.Builder captureBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE); captureBuilder.addTarget(mImageReader.getSurface()); // Use the same AE and AF modes as the preview. captureBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE); setAutoFlash(captureBuilder); // Orientation int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getOrientation(rotation)); CameraCaptureSession.CaptureCallback CaptureCallback = new CameraCaptureSession.CaptureCallback() { @Override public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) { unlockFocus(); } }; mCaptureSession.stopRepeating(); mCaptureSession.capture(captureBuilder.build(), CaptureCallback, null); } catch (CameraAccessException e) { e.printStackTrace(); } }
@Override public void onClosed(CameraDevice camera) { checkIsOnCameraThread(); Logging.d(TAG, "Camera device closed."); events.onCameraClosed(Camera2Session.this); }
@Override public void onDisconnected(final CameraDevice cd) { cameraOpenCloseLock.release(); cd.close(); cameraDevice = null; if (mOnGetPreviewListener != null) { mOnGetPreviewListener.deInitialize(); } }
@Override public void onError(final CameraDevice cd, final int error) { cameraOpenCloseLock.release(); cd.close(); cameraDevice = null; final Activity activity = getActivity(); if (null != activity) { activity.finish(); } if (mOnGetPreviewListener != null) { mOnGetPreviewListener.deInitialize(); } }
@Override public void onOpened(@NonNull CameraDevice cameraDevice) { mCameraDevice = cameraDevice; startPreview(); mCameraOpenCloseLock.release(); if (null != mTextureView) { configureTransform(mTextureView.getWidth(), mTextureView.getHeight()); } }
/** * Get a request builder for the current camera. */ public CaptureRequest.Builder createCaptureRequest(int template) throws CameraAccessException { CameraDevice device = mCameraDevice; if (device == null) { throw new IllegalStateException("Can't get requests when no camera is open"); } return device.createCaptureRequest(template); }
private CaptureRequest drawPreview(Surface surface) { try { CaptureRequest.Builder captureRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); captureRequestBuilder.addTarget(surface); captureRequestBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO); return captureRequestBuilder.build(); } catch (CameraAccessException e) { e.printStackTrace(); return null; } }
@Override public void onDisconnected(CameraDevice cameraDevice) { cameraDevice.close(); mCameraDevice = null; mCameraOpenCloseLock.release(); }
@Override public void onOpened(CameraDevice cameraDevice) { mCameraDevice = cameraDevice; mCameraOpenCloseLock.release(); createCameraPreviewSession(); }
@Override public void onDisconnected(@NonNull CameraDevice cameraDevice) { mCameraOpenCloseLock.release(); cameraDevice.close(); mCameraDevice = null; }
@Override public void onError(CameraDevice camera, int error) { cameraDevice.close(); cameraDevice = null; }