java – Android自动焦点在创建摄像机视图后不起作用

前端之家收集整理的这篇文章主要介绍了java – Android自动焦点在创建摄像机视图后不起作用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试创建自己的相机视图,除了自动对焦之外我还能正常工作,我似乎无法弄清楚为什么它不起作用.这是我的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;
    }
}

在我的清单中,我有以下内容

<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" />

解决方法

如果你添加< uses-feature android:name =“android.hardware.camera.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.

要将相机设置为焦点,可以将此方法添加到CameraView类:

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

然后在surfaceChanged()中调用方法,如下所示:

public void surfaceChanged(SurfaceHolder holder,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();

}

您可以选择以下焦点参数:

String FOCUS_MODE_AUTO Auto-focus mode.

String FOCUS_MODE_CONTINUOUS_PICTURE Continuous auto focus mode
intended for taking pictures.

String FOCUS_MODE_CONTINUOUS_VIDEO Continuous auto focus mode intended
for video recording.

String FOCUS_MODE_EDOF Extended depth of field (EDOF).

String FOCUS_MODE_FIXED Focus is fixed.

String FOCUS_MODE_INFINITY Focus is set at infinity.

String FOCUS_MODE_MACRO Macro (close-up) focus mode.

猜你在找的Android相关文章