/** * Unlock the focus. This method should be called when still image capture sequence is * finished. */ private void unlockFocus() { try { // Reset the auto-focus trigger mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_CANCEL); setAutoFlash(mPreviewRequestBuilder); mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, mBackgroundHandler); // After this, the camera will go back to the normal state of preview. mState = STATE_PREVIEW; mCaptureSession.setRepeatingRequest(mPreviewRequest, mCaptureCallback, mBackgroundHandler); } catch (CameraAccessException e) { e.printStackTrace(); } }
/** * Called after a RAW/JPEG capture has completed; resets the AF trigger state for the * pre-capture sequence. * <p/> * Call this only with {@link #mCameraStateLock} held. */ private void finishedCaptureLocked() { try { // Reset the auto-focus trigger in case AF didn't run quickly enough. if (!mNoAFRun) { mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_CANCEL); mCaptureSession.capture(mPreviewRequestBuilder.build(), mPreCaptureCallback, mBackgroundHandler); mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_IDLE); } } catch (CameraAccessException e) { e.printStackTrace(); } }
/** * Unlocks the auto-focus and restart camera preview. This is supposed to be called after * capturing a still picture. */ void unlockFocus() { mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CaptureRequest.CONTROL_AF_TRIGGER_CANCEL); try { mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, null); updateAutoFocus(); updateFlash(); mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CaptureRequest.CONTROL_AF_TRIGGER_IDLE); mCaptureSession.setRepeatingRequest(mPreviewRequestBuilder.build(), mCaptureCallback, null); mCaptureCallback.setState(PictureCaptureCallback.STATE_PREVIEW); } catch (CameraAccessException e) { Log.e(TAG, "Failed to restart camera preview.", e); } }
@Override public void onCaptureFailed(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull CaptureFailure failure) { super.onCaptureFailed(session, request, failure); this.session = null; switch (failure.getReason()) { case CaptureFailure.REASON_ERROR: Timber.e("Capture failed: REASON_ERROR"); break; case CaptureFailure.REASON_FLUSHED: Timber.e("Capture failed: REASON_FLUSHED"); break; default: Timber.e("Capture failed: UNKNOWN"); break; } }
@Override public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) { if(request.getTag() == ("FOCUS_TAG")) { //The focus trigger is complete! //Resume repeating request, clear AF trigger. mAutoFocusCallback.onAutoFocus(true); mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, null); mPreviewRequestBuilder.setTag(""); mPreviewRequest = mPreviewRequestBuilder.build(); try { mCaptureSession.setRepeatingRequest(mPreviewRequest, mCaptureCallback, mBackgroundHandler); } catch(CameraAccessException ex) { Log.d(TAG, "AUTO FOCUS FAILURE: "+ex); } } else { process(result); } }
/** * Lock the focus as the first step for a still image capture. */ private void lockFocus() { try { Log.d(TAG, "lockFocus: "); // This is how to tell the camera to lock focus. mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START); // Tell #mCaptureCallback to wait for the lock. // mState = STATE_WAITING_LOCK; mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, mBackgroundHandler); } catch (CameraAccessException e) { e.printStackTrace(); } }
/** * 运行preCapture序列来捕获静止图像 */ private void runPreCaptureSequence() { try { // 设置拍照参数请求 mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER, CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER_START); mState = STATE_WAITING_PRE_CAPTURE; mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, mBackgroundHandler); } catch (CameraAccessException e) { e.printStackTrace(); } }
/** * 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 { removeCaptureMessage(); 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); // updateAutoFocus(); updateFlash(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(); } }; mPreviewSession.stopRepeating(); mPreviewSession.capture(captureBuilder.build(), CaptureCallback, mBackgroundHandler); } catch (CameraAccessException e) { e.printStackTrace(); } }
private void runPreCaptureSequence() { try { previewRequestBuilder.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER, CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER_START); previewState = STATE_WAITING_PRE_CAPTURE; captureSession.capture(previewRequestBuilder.build(), captureCallback, backgroundHandler); } catch (CameraAccessException e) { } }
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"); } }
private void unlockFocus() { try { previewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_CANCEL); captureSession.capture(previewRequestBuilder.build(), captureCallback, backgroundHandler); previewState = STATE_PREVIEW; captureSession.setRepeatingRequest(previewRequest, captureCallback, backgroundHandler); } catch (Exception e) { Log.e(TAG, "Error during focus unlocking"); } }
private void setFlashModeAndBuildPreviewRequest(@Configuration.FlashMode int flashMode) { try { switch (flashMode) { case Configuration.FLASH_MODE_AUTO: previewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH); previewRequestBuilder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_SINGLE); break; case Configuration.FLASH_MODE_ON: previewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON); previewRequestBuilder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_SINGLE); break; case Configuration.FLASH_MODE_OFF: previewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON); previewRequestBuilder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_OFF); break; default: previewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH); previewRequestBuilder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_SINGLE); break; } previewRequest = previewRequestBuilder.build(); try { captureSession.setRepeatingRequest(previewRequest, captureCallback, backgroundHandler); } catch (Exception e) { Log.e(TAG, "Error updating preview: ", e); } } catch (Exception ignore) { Log.e(TAG, "Error setting flash: ", ignore); } }
/** * 解锁焦点 */ private void unlockFocus() { try { mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_CANCEL); setAutoFlash(mPreviewRequestBuilder); mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, mBackgroundHandler); mState = STATE_PREVIEW; mCaptureSession.setRepeatingRequest(mPreviewRequest, mCaptureCallback, mBackgroundHandler); } catch (CameraAccessException e) { e.printStackTrace(); } }
/** * Lock the focus as the first step for a still image capture. */ private void lockFocus() { try { // This is how to tell the camera to lock focus. mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START); // Tell #mCaptureCallback to wait for the lock. mState = STATE_WAITING_LOCK; mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, mBackgroundHandler); } catch (CameraAccessException e) { e.printStackTrace(); } }
/** * 锁定焦点 */ private void lockFocus() { try { // 如何通知相机锁定焦点 mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START); // 通知mCaptureCallback等待锁定 mState = STATE_WAITING_LOCK; mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, mBackgroundHandler); } catch (CameraAccessException e) { e.printStackTrace(); } }
/** * Run the precapture sequence for capturing a still image. This method should be called when * we get a response in {@link #mCaptureCallback} from {@link #lockFocus()}. */ private void runPrecaptureSequence() { try { // This is how to tell the camera to trigger. mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER, CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER_START); // Tell #mCaptureCallback to wait for the precapture sequence to be set. mState = STATE_WAITING_PRECAPTURE; mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, mBackgroundHandler); } catch (CameraAccessException e) { e.printStackTrace(); } }
private void reconfigureCamera() { if(mCamera != null) { try { mCaptureSession.stopRepeating(); mPreviewBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_CANCEL); mCaptureSession.capture(mPreviewBuilder.build(), mPreCaptureCallback, mHandler); doPreviewConfiguration(); } catch (CameraAccessException e) { e.printStackTrace(); } } }
@Override public void onCaptureProgressed(CameraCaptureSession session, CaptureRequest request, CaptureResult partialResult) { Log.d(TAG, "mSessionCaptureCallback, onCaptureProgressed"); mSession = session; checkState(partialResult); }
@Override public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) { super.onCaptureCompleted(session, request, result); if (picturesTaken.lastEntry() != null) { capturingListener.onCaptureDone(picturesTaken.lastEntry().getKey(), picturesTaken.lastEntry().getValue()); Log.i(TAG, "done taking picture from camera " + cameraDevice.getId()); } closeCamera(); }
@Override public void onPrecaptureRequired() { mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER, CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER_START); setState(STATE_PRECAPTURE); try { mCaptureSession.capture(mPreviewRequestBuilder.build(), this, null); mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER, CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER_IDLE); } catch (CameraAccessException e) { Log.e(TAG, "Failed to run precapture sequence.", e); } }
/** * Updates the internal state of flash to {@link #mFlash}. */ void updateFlash() { switch (mFlash) { case Constants.FLASH_OFF: mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON); mPreviewRequestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF); break; case Constants.FLASH_ON: mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_ALWAYS_FLASH); mPreviewRequestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF); break; case Constants.FLASH_TORCH: mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON); mPreviewRequestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_TORCH); break; case Constants.FLASH_AUTO: mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH); mPreviewRequestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF); break; case Constants.FLASH_RED_EYE: mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH_REDEYE); mPreviewRequestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF); break; } }
/** * Locks the focus as the first step for a still image capture. */ private void lockFocus() { mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CaptureRequest.CONTROL_AF_TRIGGER_START); try { mCaptureCallback.setState(PictureCaptureCallback.STATE_LOCKING); mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, null); } catch (CameraAccessException e) { Log.e(TAG, "Failed to lock focus.", e); } }
@Test public void testSetupCaptureRequestForPreview_captureRequestBuilderIsConfiguredCorrectly() throws Exception { cameraHelper.setupCaptureRequestForPreview(session, camera, surfaces); verify(captureRequestBuilder).addTarget(surface1); verify(captureRequestBuilder).addTarget(surface2); verify(captureRequestBuilder).set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE); }
/** * Build a request using the current target Surfaces and settings. * @return CaptureRequest * @throws ObjectNotInitializedException You should call init() before call this method otherwise * it will throw a ObjectNotInitializedException */ public CaptureRequest build() throws ObjectNotInitializedException { if (mBuilder != null) { return mBuilder.build(); } else { throw genException(); } }
@Override public void buildRequest(int type) { try { CaptureRequest.Builder builder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); } catch (CameraAccessException e) { e.printStackTrace(); } }
public void qq() { mPreviewRequestBuilder.set(CaptureRequest.CONTROL_EFFECT_MODE, CaptureResult.CONTROL_EFFECT_MODE_MONO); mPreviewRequest = mPreviewRequestBuilder.build(); try { mCaptureSession.setRepeatingRequest(mPreviewRequest, mCaptureCallback, null); } catch (CameraAccessException e) { e.printStackTrace(); } }
private void chooseFocusMode(CaptureRequest.Builder captureRequestBuilder) { final int[] availableFocusModes = cameraCharacteristics.get(CameraCharacteristics.CONTROL_AF_AVAILABLE_MODES); for (int mode : availableFocusModes) { if (mode == CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_VIDEO) { captureRequestBuilder.set( CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_VIDEO); Logging.d(TAG, "Using continuous video auto-focus."); return; } } Logging.d(TAG, "Auto-focus is not available."); }
@Override public void onCaptureStarted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, long timestamp, long frameNumber) { super.onCaptureStarted(session, request, timestamp, frameNumber); Timber.d("Capture started."); this.session = session; }
/** * Updates the internal state of flash to {@link #mFlash}. */ void updateFlash(CaptureRequest.Builder requestBuilder) { if (!mFlashSupported) { return; } switch (mFlash) { case CameraConstants.FLASH_OFF: requestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON); requestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF); break; case CameraConstants.FLASH_ON: requestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_ALWAYS_FLASH); requestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF); break; case CameraConstants.FLASH_TORCH: requestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON); requestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_TORCH); break; case CameraConstants.FLASH_AUTO: requestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH); requestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF); break; case CameraConstants.FLASH_RED_EYE: requestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH_REDEYE); requestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF); break; } }
private void unlockFocus() { MLog.d(TAG, "unlockFocus"); try { // Reset the auto-focus trigger mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_CANCEL); //setAutoFlash(mPreviewRequestBuilder); mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, null); // After this, the camera will go back to the normal state of preview. mState = STATE_PREVIEW; mCaptureSession.setRepeatingRequest(mPreviewRequest, mCaptureCallback, null); } catch (CameraAccessException e) { e.printStackTrace(); } }
/** * 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); }
/** * Set a repeating request. */ public void setRepeatingRequest(final CaptureRequest request, final CameraCaptureSession.CaptureCallback listener, final Handler handler) { mCameraHandler.post(() -> { try { mCameraSession.setRepeatingRequest(request, listener, handler); } catch (CameraAccessException e) { String errorMessage = mErrorDisplayer.getErrorString(e); mErrorDisplayer.showErrorDialog(errorMessage); } }); }
/** * Callbacks for CameraOps {@link CameraOps.CameraReadyListener}. */ @Override public void onCameraReady() { // Ready to send requests in, so set them up try { CaptureRequest.Builder previewBuilder = mCameraOps.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); previewBuilder.addTarget(mProcessingNormalSurface); mCameraOps.setRepeatingRequest(previewBuilder.build(), null, mUiHandler); } catch (CameraAccessException e) { String errorMessage = getErrorString(e); showErrorDialog(errorMessage); } }