前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android Camera2 Focus Distance

Android Camera2 Focus Distance

作者头像
小驰随想录
发布2022-12-20 19:22:14
1.5K0
发布2022-12-20 19:22:14
举报
文章被收录于专栏:Android Camera开发

有一段时间,我一直在查找Camera Focus distance相关内容。网上也查找了不少资料,有时看别人提的问题以及回答,也能给自己带来不少的帮助,希望下面的内容也能帮助到有需要的小伙伴~

提问1

从android相机api2,我们尝试设置LENS_FOCUS_DISTANCE以在应用程序中实现手动对焦功能。为此,我们正在执行以下过程:

我们将聚焦距离作为用户输入的单位,以厘米为单位。

使用公式100f /(以厘米为单位的值)将厘米转换为屈光度,其中f表示将其转换为浮点。

我们使用LENS_FOCUS_DISTANCE传递的结果值 使用中的代码

final CaptureRequest.Builder captureBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);

captureBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_OFF);

captureBuilder.set(CaptureRequest.LENS_FOCUS_DISTANCE, mLensFocusDistance);

我们将一个对象放置在45厘米中,但是如果将LENS_FOCUS_DISTANCE设置为45厘米,它将失去焦点;如果对象在35厘米的距离处,并且将LENS_FOCUS_DISTANCE设置为45厘米,则焦点可以正常工作。

提问2

我想使用android camera2 API获取从对象到android设备的距离。在CaptureCallback ,我通过result.get(CaptureResult.LENS_FOCUS_DISTANCE)获取值,并同时检查LENS_STATE, CONTROL_AF_MODE, CONTROL_AF_STATE 。

它们是“ STATIONARY”, CONTINUOUS_PICTURE和PASSIVE_FOCUSED 。

我认为焦点距离应该是有效的,并且从对象到Android设备的距离应该是1/focus_distance 。

但是实际上,计算值与实际距离完全不同。它出什么问题了?LENS_FOCUS_DISTANCE是距离估算的正确值吗?有人知道吗?

代码语言:javascript
复制
final CameraCaptureSession.CaptureCallback captureCallbackListener = new CameraCaptureSession.CaptureCallback() {
    @Override
    public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
        super.onCaptureCompleted(session, request, result);
        Log.e(TAG, String.format("captureCallbackListener %s-%f", lensStateToString(result.get(CaptureResult.LENS_STATE)), result.get(CaptureResult.LENS_FOCUS_DISTANCE) ));
        Log.e(TAG, String.format("AF mode %s-%s", ctlAfModeToString(result.get(CaptureResult.CONTROL_AF_MODE)), ctlAfStateToString(result.get(CaptureResult.CONTROL_AF_STATE)) ));
    }
};
回复:

请检查CameraCharacteristics.LENS_INFO_FOCUS_DISTANCE_CALIBRATION的值。

如果它是UNCALIBRATED或APPROXIMATE,则LENS_FOCUS_DISTANCE的值可能不会接近实际值。

即使使用“校准”,物体越远,误差就越大-对于相机而言,超过超焦距(最多几米)的所有物体都开始无法精确地测量距离。

来源:https://stackoom.com/question/2l2uc/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8camera-API%E8%8E%B7%E5%8F%96%E4%BB%8E%E5%AF%B9%E8%B1%A1%E5%88%B0Android%E8%AE%BE%E5%A4%87%E7%9A%84%E8%B7%9D%E7%A6%BB

提问3

我想为自己开发一款Android相机应用程序(如果有兴趣的人可以共享它),在视频录制时有手动对焦。

我已经在谷歌样本Camera2应用程序中添加了一个SeekBar,但我找不到实现手动对焦的方法。

我在camera2,android中找到了手动对焦,但它在我的LG G4上无效。库存相机应用程序几乎是完美的,因为它不允许手动对焦在视频模式。

你们有谁有想法吗?

代码语言:javascript
复制
编辑:这是SeekBar监听器的代码:

@Override
public void onStopTrackingTouch(SeekBar seekBar) {}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
    mPreviewBuilder.set(CaptureRequest.CONTROL_AF_MODE, CameraMetadata.CONTROL_AF_MODE_OFF);
}

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    float minimumLens = characteristics.get(CameraCharacteristics.LENS_INFO_MINIMUM_FOCUS_DISTANCE);
    float num = (((float)progress) * minimumLens / 100);
    mPreviewBuilder.set(CaptureRequest.LENS_FOCUS_DISTANCE, num);
}
回复1:

您需要检查您运行的设备是否实际支持camera2中的手动控制。

关键是相机设备的可用功能是否列出MANUAL_SENSOR 。

如果是这样,则可以通过将自动对焦模式设置为OFF来控制镜头,然后将镜头焦距设置为所需的值。

如果未列出MANUAL_SENSOR,则设备很可能不支持手动对焦控制(不幸的是,有些制造商使用私有接口作为其默认相机应用来实现手动对焦控制)。

这些都应该进入你用来控制预览的重复请求。

回复2:

Eddy Talvala在他的回答中正确地描述了一切,但是如果你仍然觉得复杂,这里有一个代码片段,它将焦点设置为无穷大(其值为0f):

代码语言:javascript
复制
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
int[] capabilities = characteristics
    .get(CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES);

boolean isManualFocusSupported = IntStream.of(capabilities)
    .anyMatch(x -> x == CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES_MANUAL_SENSOR);

if (isManualFocusSupported) {
    previewBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_OFF);
    previewBuilder.set(CaptureRequest.LENS_FOCUS_DISTANCE, 0f);
}

来源:https://stackoom.com/question/2u0b0/%E4%BD%BF%E7%94%A8android-camera-API%E6%89%8B%E5%8A%A8%E5%AF%B9%E7%84%A6

提问4

Problem I've recognized that the native 'LG' Camera2 App in the Lollipop API can manually focus nearer than any other app on the market. Not even 'Open Camera', which seems to use really all functions of the Camera2 API can focus that near.

I've searched through StackOverflow, Android Forums, Google and the source code of the native Android Lollipop Camera2 App and I've found nothing which explains this difference.

Is somebody out there who can explain, what does the 'LG' Camera2 app differently?

The question is Is there another usable option beside 'LENS_INFO_MINIMUM_FOCUS_DISTANCE' which can be edit to adjust the focus manually?

More details Currently I'm writing a camera app for my 'LG G4', which has a 'LENS_INFO_MINIMUM_FOCUS_DISTANCE' of 10.0. 'Android 5.x' currently has no setting to get the step width between 0.0 and 10.0. Whatever I've used as step width, it doesn't changed anything to the focus distance.

The device has SUPPORTED_HARDWARE_LEVEL: HARDWARE_LEVEL_FULL. The REQUEST_AVAILABLE_CAPABILITIES are: BACKWARD_COMPATIBLE, MANUAL_SENSOR, MANUAL_POST_PROCESSING, READ_SENSOR_SETTINGS, BURST_CAPTURE and RAW.

Nevertheless the settings 'LENS_INFO_AVAILABLE_FILTER_DENSITIES' (0.0), 'LENS_INFO_AVAILABLE_FOCAL_LENGTHS' (4.42) and 'LENS_INFO_AVAILABLE_APERTURES' (1.8) are containing only one value and therefore they can't be changed.

The setting 'LENS_INFO_FOCUS_DISTANCE_CALIBRATION' reporting that the focus is 'CALIBRATED'.

There is even no difference, if I set the 'AF Mode' to 'Off' or to 'Macro'.

The setting 'LENS_INFO_HYPERFOCAL_DISTANCE' is a farce. It's only an info about a value, which can't be set or used for anything.

来源:https://stackoverflow.com/questions/32136697/android-camera2-manual-focus

回复:

Well, to use manual focus at all via camera2, your device needs to have MANUAL_SENSOR in the android.request.availableCapabilities field.

So you should check your CameraCharacteristics for this, and if it only has BACKWARDS_COMPATIBLE, you're out of luck. That's the first check, and then you just need to actually set the focus distance property with the desired value and turn off auto focus in your CaptureRequests. – rcsumner Aug 23 '15 at 19:12

提问5

我尝试了很多时间将焦点设置在camera2 API上,或者将自动焦点设置在活动开始时初始化的特定区域上。

但是它仍然不起作用...

请问你能帮帮我吗 ?:)

这是我的配置:

代码语言:javascript
复制
        captureRequestBuilder.set(CaptureRequest.CONTROL_MODE, CaptureRequest.CONTROL_MODE_AUTO);

        captureRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER,
                CaptureRequest.CONTROL_AF_TRIGGER_START);

        MeteringRectangle[] focusArea = new MeteringRectangle[1];
        focusArea[0] = new MeteringRectangle(new Rect(rectangle.getLeft(),rectangle.getTop(),
                rectangle.getRight(), rectangle.getBottom()), MeteringRectangle.METERING_WEIGHT_MAX);
        captureRequestBuilder.set(CaptureRequest.CONTROL_AF_REGIONS, focusArea);  
      //captureRequestBuilder.set(CaptureRequest.LENS_FOCUS_DISTANCE,10000000000.0f);
android focus distance area android-camera2
回复:

您不会在调用CameraCaptureSession.capture或setRepeatingRequest的地方包含代码。

但是您不应该在重复请求中设置TRIGGER_START,因为这会重新开始关注每一帧。仅在单个capture()调用中使用它。

另外,您是说要更改CONTROL_AF_MODE,而不是CONTROL_MODE吗?后者不影响所使用的自动对焦的类型,我假设您要对触摸到焦点的操作执行AF_MODE_AUTO。

如果要手动对焦,则需要将AF_MODE_OFF设置为禁用自动对焦。

来源:https://stackoom.com/question/3dIhT/camera-%E8%81%9A%E7%84%A6%E5%8C%BA%E5%9F%9F%E5%92%8C%E8%81%9A%E7%84%A6%E8%B7%9D%E7%A6%BB%E7%9A%84%E9%97%AE%E9%A2%98

提问6

我正在使用Camera2 API开发自定义Camera App,并尝试通过搜索栏启用手动对焦。我引用了几篇文章,但遇到了可能来自我的Java编码的错误。

我从“ CameraCharacteristics features = manager.getCameraCharacteristics(cameraDevice.getId());”行看到“错误:未报告的异常CameraAccessException;必须被捕获或声明为抛出”的消息。

你们中有人吗?

代码语言:javascript
复制
       public void onStartTrackingTouch(SeekBar seekBar) {

            previewBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_OFF);

        }


        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            progressChangedValue = progress;


            CameraManager manager = (CameraManager)getSystemService(CAMERA_SERVICE);
            CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraDevice.getId());

            float minimumLens = characteristics.get(CameraCharacteristics.LENS_INFO_MINIMUM_FOCUS_DISTANCE);
            float num = (((float)progress) * minimumLens / 100);
            previewBuilder.set(CaptureRequest.LENS_FOCUS_DISTANCE, num);

        }

来源:https://stackoom.com/question/3YzuI/%E4%BD%BF%E7%94%A8Seekbar-for-Camera-API%E6%89%8B%E5%8A%A8%E5%AF%B9%E7%84%A6

提问7

我的手机支持所有手动配置。测试( https://github.com/pinguo-yuyidong/Camera2 ),允许我们使用搜索栏进行调整。

我想在预览开始时在镜头焦距中设置一个固定值,直到我关闭应用程序。

将AF模式设置为关闭并将镜头聚焦距离设置为值后。预览不显示任何效果,但更改了焦点2值。

我怎样才能克服这个问题?

代码语言:javascript
复制
示例代码(Java)
    private CaptureRequest mPreviewRequest;
    private void startCameraPreview() throws CameraAccessException {
        SurfaceTexture texture = textureview.getSurfaceTexture();
        texture.setDefaultBufferSize(imageDimensions.getWidth(), imageDimensions.getHeight());
        Surface surface = new Surface(texture);
        mPreviewRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        mPreviewRequestBuilder.addTarget(surface);
        System.out.println("Focus 1 ====== "+mPreviewRequestBuilder.get(CaptureRequest.LENS_FOCUS_DISTANCE));
        cameraDevice.createCaptureSession(Arrays.asList(surface), new CameraCaptureSession.StateCallback() {
            @Override
            public void onConfigured(@NonNull CameraCaptureSession session) {
                if (cameraDevice == null) {
                    return;
                }
                cameraSession = session;

                try {
                    mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,CaptureRequest.CONTROL_AF_MODE_OFF);
                    mPreviewRequestBuilder.set(CaptureRequest.LENS_FOCUS_DISTANCE, 7.5f);
                    mPreviewRequest = mPreviewRequestBuilder.build();
                    cameraSession.setRepeatingRequest(mPreviewRequest, mCaptureCallback, backgroundHandler);
                    System.out.println("Focus 2 ====== "+mPreviewRequestBuilder.get(CaptureRequest.LENS_FOCUS_DISTANCE));
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onConfigureFailed(@NonNull CameraCaptureSession session) {
                Toast.makeText(getApplicationContext(),"Failed",Toast.LENGTH_SHORT).show();
            }
        }, null);
    }

    private static final int STATE_PREVIEW = 0;
    private int mState = STATE_PREVIEW;
    private CameraCaptureSession.CaptureCallback mCaptureCallback = new CameraCaptureSession.CaptureCallback() {
        private void process(CaptureResult result){
            switch(mState){
                case STATE_PREVIEW:{
                    break;
                }
            }
        }
    };

产量 焦点1 ====== 0.0

焦点2 ====== 7.5

当我改变值时,我看不出差异。

回复:

不知道为什么我需要mPreviewRequest和setRepeatingRequest 2次,但它的工作原理。

代码语言:javascript
复制
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,CaptureRequest.CONTROL_AF_MODE_OFF);
mPreviewRequest = mPreviewRequestBuilder.build();
cameraSession.setRepeatingRequest(mPreviewRequest, mCaptureCallback, backgroundHandler);
mPreviewRequestBuilder.set(CaptureRequest.LENS_FOCUS_DISTANCE, 7.5f);
mPreviewRequest = mPreviewRequestBuilder.build();
cameraSession.setRepeatingRequest(mPreviewRequest, mCaptureCallback, backgroundHandler);

来源:https://stackoom.com/question/3ojnj/%E5%A6%82%E4%BD%95%E6%89%8B%E5%8A%A8%E6%8E%A7%E5%88%B6Camera-%E6%89%8B%E5%8A%A8%E5%AF%B9%E7%84%A6

深圳上班,

从事Android Camera相关软件开发工作,

公众号记录生活和工作的点滴,

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-11-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 小驰成长圈 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 提问1
  • 提问2
    • 回复:
    • 提问3
      • 回复1:
        • 回复2:
        • 提问4
          • 回复:
          • 提问5
            • 回复:
            • 提问6
            • 提问7
              • 回复:
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档