简体   繁体   English

如何在Android Camera2 API中禁用所有自动更新

[英]How to Disable All Automatics in Android Camera2 API

I'm trying to disable auto-exposure, auto-focus, and auto-white-balance in Google's Camera2Basic sample. 我正在尝试在Google的Camera2Basic示例中禁用自动曝光,自动对焦和自动白平衡。 Here's my code: 这是我的代码:

private void disableAutomatics() {
    try {
        mPreviewRequestBuilder.set(CaptureRequest.CONTROL_MODE, CaptureRequest.CONTROL_MODE_OFF);
        mPreviewRequestBuilder.set(CaptureRequest.CONTROL_VIDEO_STABILIZATION_MODE, CaptureRequest.CONTROL_VIDEO_STABILIZATION_MODE_OFF);
        mPreviewRequestBuilder.set(CaptureRequest.LENS_OPTICAL_STABILIZATION_MODE, CaptureRequest.LENS_OPTICAL_STABILIZATION_MODE_OFF);
        mPreviewRequestBuilder.set(CaptureRequest.LENS_FOCUS_DISTANCE, .2f);
        mPreviewRequestBuilder.set(CaptureRequest.SENSOR_EXPOSURE_TIME, 1000000L);

        mPreviewRequest = mPreviewRequestBuilder.build();
        // Set new repeating request with our changed one
        mCaptureSession.setRepeatingRequest(mPreviewRequest, mCaptureCallback, mBackgroundHandler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}

The problem is I don't know where to place the method in Camera2BasicFragment.java. 问题是我不知道将该方法放在Camera2BasicFragment.java中的哪个位置。

Any help would be greatly appreciated. 任何帮助将不胜感激。

There are two places where you may want to do those settings: 您可以在两个地方进行这些设置:

  • If you want to do it before the preview starts, the better place would be inside of the overridden method onConfigured within the createCameraPreviewSession() void (line 696 in the Camera2BasicFragment file provided in the Google's Camera2Basic sample: 如果你想在预览开始之前做到这一点,那么更好的地方就是在createCameraPreviewSession() void中的被覆盖方法onConfigured createCameraPreviewSession()在Google的Camera2Basic示例中提供的Camera2BasicFragment文件中的第696行:

     private void createCameraPreviewSession() { try { SurfaceTexture texture = mTextureView.getSurfaceTexture(); assert texture != null; // We configure the size of default buffer to be the size of camera preview we want. texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight()); // This is the output Surface we need to start preview. Surface surface = new Surface(texture); // We set up a CaptureRequest.Builder with the output Surface. mPreviewRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); mPreviewRequestBuilder.addTarget(surface); // Here, we create a CameraCaptureSession for camera preview. mCameraDevice.createCaptureSession(Arrays.asList(surface, mImageReader.getSurface()), new CameraCaptureSession.StateCallback() { @Override public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) { // The camera is already closed if (null == mCameraDevice) { return; } // When the session is ready, we start displaying the preview. mCaptureSession = cameraCaptureSession; try { //Place here your custom camera settings // Start displaying the camera preview. mPreviewRequest = mPreviewRequestBuilder.build(); mCaptureSession.setRepeatingRequest(mPreviewRequest, mCaptureCallback, mBackgroundHandler); } catch (CameraAccessException e) { e.printStackTrace(); } } @Override public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) { showToast("Failed"); } }, null ); } catch (CameraAccessException e) { e.printStackTrace(); } } 
  • If you want to do the settings after the preview has started and in runtime just call your disableAutomatics() from the UI or anywhere else and it should work fine. 如果您想在预览开始后进行设置,并且在运行时只需从UI或其他任何地方调用disableAutomatics() ,它应该可以正常工作。

Note that you don't have to close the older CaptureSession by calling its CaptureSession.close() method as explained in an answer to this other question because the new replaces the older one. 请注意,您不必通过调用其CaptureSession.close()方法来关闭旧的CaptureSession ,如对此其他问题的回答中所述,因为new替换了旧版本。


On another hand, I am not sure about setting the exposure time value manually as you did in your question: 另一方面,我不确定如您在问题中那样手动设置曝光时间值:

mPreviewRequestBuilder.set(CaptureRequest.SENSOR_EXPOSURE_TIME, 1000000L); mPreviewRequestBuilder.set(CaptureRequest.SENSOR_EXPOSURE_TIME,1000000L);

because you may get unexpected results varying on different devices. 因为您可能会在不同设备上获得意外结果。 What I know is that doing so is usually discouraged and it's preferred instead to let the camera adjust by its own and then call AE (auto-exposure) lock: 我所知道的是,通常不鼓励这样做,而是首选让相机自行调整,然后调用AE(自动曝光)锁定:

mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_LOCK, true);

You can check the CONTROL_AE_LOCK reference here . 您可以在此处查看CONTROL_AE_LOCK参考。

But if your code needs a fixed exposure time then it should work. 但是如果您的代码需要一个固定的曝光时间,那么它应该可行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM