Android相机在捕获之间失去焦点

时间:2017-06-13 23:23:56

标签: java android camera

我正在使用折旧的Camera API。我的目标是每分钟拍一张照片10分钟,所以我有一个倒数计时器调用我的图像捕获。第一次图像捕捉在100%的时间内都能很好地工作。随后的照片在不同程度上失焦。有什么想法吗?在调用下一个图像捕获之前很久就会捕获并写入照片,所以我不相信它发生得太快。

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



}

1 个答案:

答案 0 :(得分:1)

我最终不得不关闭相机并在每次捕捉之间完全重新打开它。

所以,每次捕获后都会这样:

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

然后,在每次捕获之前:

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