3

我已经针对我的问题搜索了一些解决方案,并且确定有很多相关问题,但没有解决我的问题。

我收到运行时异常:takepicture failed:native_autofocus..etc。

我尝试使用自动对焦从相机拍照,但我似乎无法理解可能导致错误的原因。

这是我的代码。

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder mholder;
    private Camera mcamera;
    private Handler handler = new Handler();
    public CameraPreview(Context context, Camera camera) {
        super(context);
        this.mcamera = camera;
        mholder = getHolder();
        mholder.addCallback(this);
        mholder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        if (mholder.getSurface() == null) {
            return;
        }
        try {
            mcamera.stopPreview();
        } catch (Exception e) {
            Log.d("surfaceChanged", e.toString());
        }
        try {
            mcamera.setPreviewDisplay(holder);
            mcamera.startPreview();           
        } catch (IOException e) {
            Log.d("surfaceChanged--->surfaceCreated", e.toString());
        }
    }

我认为我的可运行代码中缺少某些东西,我试图省略可运行并执行一次自动对焦,它消除了运行时错误。我是否错过了重新初始化这部分的某些内容?

    private void autoFocus(Camera mcamera){
        final  Camera cam=mcamera;
        handler.postDelayed(new Runnable() {            
            @Override
            public void run() {         
                cam.autoFocus(autoFocusCallback);
                handler.postDelayed(this, 1500L);            
            }
        }, 1500L);
    }
    AutoFocusCallback autoFocusCallback=new AutoFocusCallback() {

        @Override
        public void onAutoFocus(boolean success, Camera camera) {
            camera.takePicture(null, null, mPicture);
            }
    };
    private PictureCallback mPicture=new PictureCallback() {

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
          //do something
        }
    };

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        try {
            mcamera.setPreviewDisplay(holder);
            mcamera.startPreview();
            mcamera.autoFocus(autoFocusCallback);
        } catch (IOException e) {
            Log.d("surfaceCreated", e.toString());
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }
}
4

2 回答 2

0

看看这里

在这里

你在surfacecreated上调用autofocus,它应该在surface changed时调用

于 2013-11-16T06:52:33.533 回答
0

你没有提到它,但一定要在你的清单中包含权限。

< 使用功能 android:name="android.hardware.camera.autofocus" />

于 2013-03-14T05:46:00.737 回答