gpt4 book ai didi

java - 如何禁用 Android Camera2 API 中的所有自动功能

转载 作者:行者123 更新时间:2023-11-29 04:35:09 29 4
gpt4 key购买 nike

我试图在 Google 的 Camera2Basic 中禁用自动曝光、自动对焦和自动白平衡。样本。这是我的代码:

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();
}
}

问题是我不知道将方法放在 Camera2BasicFragment.java 中的什么位置。

如有任何帮助,我们将不胜感激。

最佳答案

您可能需要在两个地方进行这些设置:

  • 如果您想在预览开始之前执行此操作,最好的位置是在 createCameraPreviewSession() void(行Google 的 Camera2BasicFragment 示例中提供的 Camera2Basic 文件中的 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();
    }
    }
  • 如果您想在预览开始后和运行时进行设置,只需从 UI 或其他任何地方调用您的 disableAutomatics(),它应该可以正常工作。

请注意,您不必通过调用其 CaptureSession.close() 方法来关闭旧的 CaptureSession,如对 this other question 的回答中所述。因为新的取代了旧的。


另一方面,我不确定是否像您在问题中那样手动设置曝光时间值:

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

因为您可能会在不同的设备上获得不同的意外结果。我所知道的是通常不鼓励这样做,而是让相机自行调整然后调用 AE(自动曝光)锁定:

mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_LOCK, true);

您可以查看CONTROL_AE_LOCK引用here .

但是如果您的代码需要固定的曝光时间,那么它应该可以工作。

关于java - 如何禁用 Android Camera2 API 中的所有自动功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41926094/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com