3

I have searched some solutions with my problem and sure there are many related issues regarding it but nothing solves my concern.

I am receiving a runtime exception : takepicture failed:native_autofocus..etc.

I tried to take picture from camera using autofocus and I can't seem to understand what could have caused the error.

Here is my code.

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());
        }
    }

I think somethings is lacking in my runnable code, I tried to omit the runnable and execute autofocus once, it removed the runtime error. Did I missed to reinitialize something in this part?

    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) {

    }
}
She Smile GM
  • 1,322
  • 1
  • 11
  • 33
  • 1
    Please post more detail on the errors you are seeing. A log snippet from the start of your app to the failure would be best. – Eddy Talvala Feb 26 '13 at 22:50

2 Answers2

0

You didn't mention it but be sure to include the permission in your manifest.

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

jimbo
  • 416
  • 1
  • 4
  • 14
0

Have a look here

or here

and you are calling autofocus on surfacecreated , It should be called on surface changed

Community
  • 1
  • 1
ROHIT PARMAR
  • 901
  • 15
  • 26