Android auto focus when continuous auto focus mode

2019-02-04 15:54发布

问题:

I have a camera in my app and I want to make it auto focus continuously in the same way that the phone's camera does it. I found the modes FOCUS_MODE_CONTINUOUS_VIDEO and FOCUS_MODE_CONTINUOUS_PICTURE, but they are not supported by some of the HTC Gingerbread phones I'm testing on.

This is what I'm doing to determine whether I can use these modes:

        Camera.Parameters parameters = mCamera.getParameters();
        List<String> supportedFocusModes = parameters.getSupportedFocusModes();

        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH &&
            supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
            parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
        }
        else if (supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) {
            parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
        }
        else if (supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
            // auto focus on request only
            parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
        }

Running on several different Gingerbread HTC phones I don't get the continuous modes back, but I get "auto". This lets me auto focus on demand (when I call mCamera.autoFocus(null), but the camera will not refocus if the user moves the camera.

I cannot set the focus mode to anything the camera does not support, and if I do it shows up blank.

One solution that I tried is to call mCamera.autoFocus(null) on a timer. This causes the camera to refocus continuously, even if it is already in focus.

Is there a way to implement a continuous auto focus experience on these phones? When I look at HTCs camera app on these phones it does have continuous auto focus - as you move around the camera refocuses and does not keep refocusing once the picture is in focus.

回答1:

We had a requirement to support a very wide range of phones. My solution in the end was to handle each case differently.

For the scenario of phones without continuous auto-focus support I implemented a utility to listen to the accelerometer using SensorManager and trigger mCamera.autoFocus(...) whenever the user moves the camera.

There were also some older tablets that only supported fixed focus (who would use such a thing!), in that case the picture needed to be taken immediately - not on the focus callback.

For most modern phones the code snippet above was fine to use FOCUS_MODE_CONTINUOUS_PICTURE.



回答2:

I got a similar pb on my samsung S4 and I solved it with:

camera.setParameters(parameters);
    camera.autoFocus(null);

This is suggest in the Google doc here.

To make the camera parameters take effect, your application has to call setParameters(Camera.Parameters).