Android CameraX – 手动更改曝光补偿?

huangapple go评论64阅读模式
英文:

Android CameraX - manually change exposure compensation?

问题

我正在使用CameraX进行我的项目。我已经创建了预览用例和捕获用例。

final CameraSelector cameraSelector = new CameraSelector.Builder().requireLensFacing(lensFacing).build();

previewBuilder = new Preview.Builder()
    .setTargetResolution(targetOutputSize)
    .setTargetRotation(rotation);

preview = previewBuilder.build();

final ImageCapture.Builder imageCaptureBuilder = new ImageCapture.Builder()
    .setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY)
    .setTargetResolution(targetOutputSize)
    .setTargetRotation(rotation);

imageCapture = imageCaptureBuilder.build();

一切都运行正常。现在,我需要添加手动更改曝光补偿的功能,但我在官方文档或其他地方找不到如何执行此操作的参考资料。在CameraX中是否有可能实现这一功能,还是我需要切换到Camera2 API?

请问,任何帮助都将不胜感激。

英文:

I'm using CameraX for my project. I have created preview useCase and capture useCase.

final CameraSelector cameraSelector = new CameraSelector.Builder().requireLensFacing(lensFacing).build();

        previewBuilder = new Preview.Builder()
            .setTargetResolution(targetOutputSize)
            .setTargetRotation(rotation);

        preview = previewBuilder.build();

        final ImageCapture.Builder imageCaptureBuilder = new ImageCapture.Builder()
            .setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY)
            .setTargetResolution(targetOutputSize)
            .setTargetRotation(rotation);

        imageCapture = imageCaptureBuilder.build();

Everything works fine. Now, I need to add functionality to manually change exposure compensation, but I can't find any reference in official documentation or anywhere else how to do this. Is it possible with CameraX, or I need to switch to Camera2 API?

Please, any help is welcome.

答案1

得分: 5

有一个CameraX库的新版本

def camerax_version = '1.0.0-beta09';

首先将这些依赖项添加到gradle文件中

// CameraX核心库
implementation "androidx.camera:camera-core:$camerax_version"

// CameraX Camera2扩展
implementation "androidx.camera:camera-camera2:$camerax_version"

这个版本支持在运行时调整曝光补偿首先创建预览和拍照用例然后将这些用例绑定到cameraProvider

camera = cameraProvider.bindToLifecycle(this, cameraSelector, preview, imageCapture);

现在我们有了camera实例可以用它来获取CameraInfo然后从CameraInfo中获取ExposureState

ExposureState exposureState = camera.getCameraInfo().getExposureState();

我们可以使用exposureState来检查设备是否支持曝光补偿

if (!exposureState.isExposureCompensationSupported()) return;

接下来我们需要获取ExposureCompensation范围

Range<Integer> range = exposureState.getExposureCompensationRange();

最后我们可以按以下方式设置ExposureCompensation索引

if (range.contains(index))
    camera.getCameraControl().setExposureCompensationIndex(index);
英文:

There is new version of CameraX library.

def camerax_version = &#39;1.0.0-beta09&#39;

Firstly add those dependencies to gradle file.

// CameraX core library
implementation &quot;androidx.camera:camera-core:$camerax_version&quot;

// CameraX Camera2 extensions
implementation &quot;androidx.camera:camera-camera2:$camerax_version&quot;

This version supports Exposure compensation which can be adjusted in runtime.
First create preview and takePicure use cases, and then bind those use cases to cameraProvider.

 camera = cameraProvider.bindToLifecycle(this, cameraSelector, preview, imageCapture);

Now we have camera instance and it can be used to retrive CameraInfo, and from CameraInfo, we retrive ExposureState.

ExposureState exposureState = camera.getCameraInfo().getExposureState();

We can use exposureState to check if exposure compensation is supported on device

if (!exposureState.isExposureCompensationSupported()) return;

Next we need to get ExposureCompensation Range

Range&lt;Integer&gt; range = exposureState.getExposureCompensationRange();

Finally, we can set ExposureCompensation index in following way

 if (range.contains(index))
     camera.getCameraControl().setExposureCompensationIndex(index);

答案2

得分: 1

看起来在CameraX API下有一个名为Camera2Interop的类,它允许你在内部更改数值。这是官方文档链接:Camera2Interop.Extender

在构建ImageCapture之前,添加以下内容:

Camera2Interop.Extender(imageCaptureBuilder)
    .setCaptureRequestOption(CaptureRequest.CONTROL_AE_EXPOSURE_COMPENSATION, value);
英文:

Looks like there is a Camera2Interop class under the CameraX API which allows you to internally change the values. This is the official documentation: Camera2Interop.Extender

Before building your ImageCapture, add this:

Camera2Interop.Extender(imageCaptureBuilder)
    .setCaptureRequestOption(CaptureRequest.CONTROL_AE_EXPOSURE_COMPENSATION, value);

答案3

得分: 0

以下是翻译后的内容:

如果你想知道相机何时完成更改曝光设置,可以像这样进行操作(相机版本 = '1.0.0'):

lateinit var myCameraExposureListener: ListenableFuture<Int>

然后

myCameraExposureListener =
myCameraControl!!.setExposureCompensationIndex(exposureCompensationIndex);

myCameraExposureListener.addListener({
    try {
        Log.v(TAG, "更改曝光设置已完成")
    } catch (exception: Exception) {
        Log.v(TAG, "更改曝光设置失败")
    }
}, ContextCompat.getMainExecutor(context))
英文:

In case you want to know when the camera completed changing the exposure settings, do something like (camerax_version = '1.0.0'):

    lateinit var myCameraExposureListener: ListenableFuture&lt;Int&gt;

and then

        myCameraExposureListener =
        myCameraControl!!.setExposureCompensationIndex(exposureCompensationIndex);

    myCameraExposureListener.addListener({
        try {
            Log.v(TAG, &quot;Changing Exposure Settings Completed&quot;)
        } catch (exception: Exception) {
            Log.v(TAG, &quot;Changing Exposure Settings Failed&quot;)
        }
    }, ContextCompat.getMainExecutor(context) )

huangapple
  • 本文由 发表于 2020年9月3日 01:47:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63711004.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定