Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
103 views
in Technique[技术] by (71.8m points)

android - Unable to focus distance in Camera2 API

I am trying to set focus distance in Android camera2 API but get Attempt to invoke virtual method 'float java.lang.Float.floatValue()' on a null object reference. I tried this on Samsung android 10 and it works fine. When I try with redmi photo I get this error, do anyone knows how to solve this issue?

Here is my code:

 public void focus(float distance) {
        CameraManager cameraManager = (CameraManager) app.getSystemService(Context.CAMERA_SERVICE);
        CameraCharacteristics characteristics = null;
        try {
            characteristics = cameraManager.getCameraCharacteristics(cameraID);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }

        float minimumLens = characteristics.get(CameraCharacteristics.LENS_INFO_MINIMUM_FOCUS_DISTANCE);

        float num = (distance * minimumLens / 100);
        captureRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_OFF);
        captureRequestBuilder.set(CaptureRequest.LENS_FOCUS_DISTANCE, num);
        refreshCamera();
    }
question from:https://stackoverflow.com/questions/65896511/unable-to-focus-distance-in-camera2-api

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

well, thats Xiaomi... you can't be shure that camera module supports some features like focusing. one and only almost always properly working feature is taking photo/bitmap. all other may be not supported by camera module - even if there is an app on device which can use some feature (e.g. focus) there is no guarantee that manufacturer implemented this feature in Camera2 API (or any other API)

consider using CameraX lib, which tries to resolve problems with very different ways for accessing some features/resolutions/params. still it may also fail (but rarely) or return info that camera doesn't support such feature


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...