简体   繁体   English

创建摄像机视图后,Android自动对焦功能无效

[英]Android Auto focus doesn't work after creating camera view

I am trying to create my own Camera View, I have everything working except the autofocus, I can't seem to figure out why it won't work. 我正在尝试创建自己的相机视图,除了自动对焦之外我还能正常工作,我似乎无法弄清楚为什么它不起作用。 Here is my code for CameraView.java 这是我的CameraView.java代码

public class CameraView extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder surface_Holder;
    private  Camera main_Camera;
    boolean on;

    public CameraView(Context context, Camera camera){
        super(context);
        main_Camera = camera;
        main_Camera.setDisplayOrientation(90);
        surface_Holder = getHolder();
        surface_Holder.addCallback(this);
        surface_Holder.setType(SurfaceHolder.SURFACE_TYPE_NORMAL);
    }

    public boolean isOn(){
        return on;
    }


    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        try{
            main_Camera.setPreviewDisplay(holder);
            main_Camera.startPreview();
        }catch (Exception e){
            Log.d("Error", "Canmera error on surfaceCreated" + e.getMessage());
            main_Camera.release();
            main_Camera = null;
        }

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

        if(holder.getSurface()==null){
            return;
        }
        try{
            main_Camera.stopPreview();
        }catch (Exception e){

        }
        try{

            main_Camera.setPreviewDisplay(surface_Holder);
            main_Camera.startPreview();
        }catch (IOException e){
            Log.d("Error", "Camera error on surfaceChanged " + e.getMessage());
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        main_Camera.setPreviewCallback(null);
        main_Camera.stopPreview();
        main_Camera.release();
        main_Camera= null;
    }
}

Inside my manifest I have the following: 在我的清单中,我有以下内容:

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

if you added <uses-feature android:name="android.hardware.camera.autofocus" /> to your manifest it doesn't mean the camera will make autofocus . 如果你在你的清单中添加了<uses-feature android:name="android.hardware.camera.autofocus" /> ,那并不代表相机会进行autofocus It means you give your app the permission to use camera hardware or software that take care of autofocus. 这意味着您允许您的应用程序使用相机硬件或处理自动对焦的软件。

The purpose of a declaration is to inform any external entity of the set of hardware and software features on which your application depends. 声明的目的是通知任何外部实体您的应用程序所依赖的硬件和软件功能集。

To set your camera to focus you can add this method to your CameraView class: 要将相机设置为焦点,可以将此方法添加到CameraView类:

private void setFocus(String mParameter) {
    Camera.Parameters mParameters = mCamera.getParameters();
    mParameters.setFocusMode(mParameter);
    mCamera.setParameters(mParameters);
}

And then call this method in surfaceChanged() like this: 然后在surfaceChanged()调用此方法,如下所示:

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

    ...//your code here

    // Set focus mode to continuous picture
    setFocus(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);

    // Start camera preview
    mCamera.startPreview();

}

You can choose between these focus parameters : 您可以选择以下focus parameters

String FOCUS_MODE_AUTO Auto-focus mode. String FOCUS_MODE_AUTO自动对焦模式。

String FOCUS_MODE_CONTINUOUS_PICTURE Continuous auto focus mode intended for taking pictures. String FOCUS_MODE_CONTINUOUS_PICTURE用于拍摄照片的连续自动对焦模式。

String FOCUS_MODE_CONTINUOUS_VIDEO Continuous auto focus mode intended for video recording. String FOCUS_MODE_CONTINUOUS_VIDEO用于视频录制的连续自动对焦模式。

String FOCUS_MODE_EDOF Extended depth of field (EDOF). String FOCUS_MODE_EDOF扩展景深(EDOF)。

String FOCUS_MODE_FIXED Focus is fixed. 字符串FOCUS_MODE_FIXED焦点是固定的。

String FOCUS_MODE_INFINITY Focus is set at infinity. String FOCUS_MODE_INFINITY焦点设置为无穷大。

String FOCUS_MODE_MACRO Macro (close-up) focus mode. String FOCUS_MODE_MACRO宏(特写)对焦模式。

//set camera to continually auto-focus
Camera.Parameters params = c.getParameters();
//*EDIT*//params.setFocusMode("continuous-picture");
//It is better to use defined constraints as opposed to String, thanks to AbdelHady
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
c.setParameters(params);

Here are some options: 以下是一些选项:

  1. User arsalank2 recommends using "continuous auto focus" as described in this answer . 用户arsalank2建议使用本答案中描述的“连续自动对焦”。 However, it seems that some HTC devices do not support this. 但是,似乎有些HTC设备不支持此功能。 Check with: parameters.getSupportedFocusModes().contains(Camera.Parameters.FOCUS_MODE_CONTIN‌​UOUS_VIDEO) 检查: parameters.getSupportedFocusModes().contains(Camera.Parameters.FOCUS_MODE_CONTIN‌​UOUS_VIDEO)

  2. You could implement an onSensorChanged listener and focus with a callback when certain criteria are met, see this answer by Juan Acevedo . 您可以实现onSensorChanged侦听器,并在满足某些条件时使用回调进行关注, 请参阅 Juan Acevedo的 回答

  3. Handle each case differently to support the widest range of devices possible. 以不同方式处理每种情况,以支持最广泛的设备。 Check what works with different models of different devices as you can't fully rely on what the API level says is implemented. 检查哪些适用于不同设备的不同型号,因为您无法完全依赖API级别所实现的内容。

I would recommend going with option 3 as there seems to be no method that works for every single device. 我建议使用选项3,因为似乎没有适用于每个设备的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM