Droid X camera autofocus callback always returns f

2019-05-25 08:33发布

问题:

I'm using the Motorola Droid X and for some reason the success variable for success in the onAutoFocus(boolean success, Camera camera) always returns false. It seems to work with other devices including a Droid 2 so it's seems to be a device specific problem.

My code is basically this:

camera.autoFocus(new AutoFocusCallback() {
    @Override
    public void onAutoFocus(boolean success, Camera camera) {
        takePicture();
    }
});

回答1:

After hours of debugging, the problem was setting the preview size of the surface view to a large size. I don't understand why that would affect the autofocus but setting the preview size to a smaller size fixed the problem. It might be caused by the preview size being larger than the screen resolution because that was the only resolution that didn't work. Hope this helps anyone else who might run into this problem.

Update: I ran into this problem again after changing the layout of my preview page and it seems like the focus just fails for certain preview sizes. So far these sizes failed for me: 720x480, 1280x720



回答2:

I agree with dt0's answer. I'm using the Preview class from the Android sample, and to fix it I simply increased the aspect tolerance when determining the optimal preview size:

private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
    final double ASPECT_TOLERANCE = 0.2; // this was 0.1
    double targetRatio = (double) w / h;
    ...
}

With this change, rather than setting the optimal size to 720x480 (which causes auto focus to fail), it sets optimal size to 640x480, which passes. I still don't understand why this fixes the problem (the actual view is size 734x480), but I'm happy that it does.



回答3:

I had to go with 0.3 for mine to work.

final double ASPECT_TOLERANCE = 0.3; // this was 0.1

Also, I found that if I called startPreview() in the surfaceCreated(SurfaceHolder holder), Auto focus would work with a better looking preview aspect ratio. But, then the preview would not work at all on a Galaxy.