emhomm4
emhomm4

Reputation: 212

Android Camera gets out of focus between captures

I'm using the depreciated Camera API. My goal is to take a photo once per minute for 10 mins, so I've got a countdown timer calling my image capture. The first image capture works beautifully 100% of the time. The subsequent photos are out of focus to varying degrees. Any ideas why? The photo is captured and written long before the next image capture is called, so I don't believe it's happening too quickly.

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_apicTimer);

    imageView = (ImageView) findViewById(R.id.ViewImg);

    SurfaceView surfaceView;
    surfaceView = (SurfaceView) findViewById(R.id.SurfaceView);
    surfaceView.setEnabled(false);

    surfaceHolder = surfaceView.getHolder();
    surfaceHolder.addCallback(this);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    //Calls the timer
    picLoop();
}

public void picLoop(){

    timer = new CountDownTimer(totalTime,60000){ //(time in ms, tick length)

        public void onFinish(){
        }

        public void onTick(long millisUntilFinished){

                captureImage();
        }
    }.start();

public void captureImage() {

    //Need to wait a sec for the surface to form before we ask for autofocus
    new CountDownTimer(3000,500){ //1000 ms, 500ms tick
        public void onFinish(){
            camera.autoFocus(autoFocusCB);
        }
        public void onTick(long millisUntilFinished){

        }}.start();

    //When autofocused, take the picture
    autoFocusCB = new android.hardware.Camera.AutoFocusCallback() {
        @Override
        public void onAutoFocus(boolean success, android.hardware.Camera camera) {

            camera.takePicture(null, null, jpegCallback);

        }
    };

    //When the picture exists, save and display
    jpegCallback = new android.hardware.Camera.PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, android.hardware.Camera camera) {
            picData = data;
            displayPic(data); //call to a function to display to an imageview  
        }
    };

}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    try{
        camera = android.hardware.Camera.open();
    }
    catch (RuntimeException e){
        System.err.println(e);
        return;
    }

    List<Camera.Size> picSizes;
    android.hardware.Camera.Size largestSize;
    android.hardware.Camera.Parameters param;
    param = camera.getParameters();
    param.setPreviewSize(352, 288);
    param.setFlashMode(FLASH_MODE_AUTO);
    //param.setFocusMode(FOCUS_MODE_AUTO);
    param.setFocusMode(FOCUS_MODE_CONTINUOUS_PICTURE);       
    param.setPictureSize(1952,1952);
    param.setJpegQuality(100);
    param.setExposureCompensation(param.getMinExposureCompensation());
    param.setWhiteBalance(WHITE_BALANCE_AUTO);
    camera.setParameters(param);

    try {
        camera.setPreviewDisplay(surfaceHolder);
        camera.startPreview();
    }

    catch (Exception e) {
        System.err.println(e);
    }
}


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

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

private void refreshCamera() {
    if (surfaceHolder.getSurface() == null) {
        return;
    }

    try {
        camera.stopPreview();
    }

    catch (Exception e) {
        System.err.println(e);
        return;
    }

    try {
        camera.setPreviewDisplay(surfaceHolder);
        camera.startPreview();

    }
    catch (Exception e) {
    }
}



}

Upvotes: 1

Views: 501

Answers (1)

emhomm4
emhomm4

Reputation: 212

I ended up having to close the camera and completely reopen it between every capture.

So, this after every capture:

    camera.stopPreview();
    camera.release();
    camera = null;

Then, this before every capture:

try{
    camera = android.hardware.Camera.open();
}
catch (RuntimeException e){
    System.err.println(e);
    return;
}

List<Camera.Size> picSizes;
android.hardware.Camera.Size largestSize;
android.hardware.Camera.Parameters param;
param = camera.getParameters();
param.setPreviewSize(352, 288);
param.setFlashMode(FLASH_MODE_AUTO);
param.setFocusMode(FOCUS_MODE_AUTO);     
param.setPictureSize(1952,1952);
param.setJpegQuality(100);
param.setExposureCompensation(param.getMinExposureCompensation());
param.setWhiteBalance(WHITE_BALANCE_AUTO);
camera.setParameters(param);

try {
    camera.setPreviewDisplay(surfaceHolder);
    camera.startPreview();
}

catch (Exception e) {
    System.err.println(e);
}

Upvotes: 1

Related Questions