0
我正在使用折舊的攝像頭API。我的目標是每分鐘拍攝一張照片10分鐘,所以我有一個倒數計時器,叫我的圖像捕捉。第一個圖像捕捉100%的時間非常漂亮。隨後的照片不同程度地失焦。任何想法爲什麼?在下一次圖像捕捉被調用之前,照片就被捕獲並寫入,所以我不相信它發生得太快。Android攝像頭在拍攝之間失去焦點
@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) {
}
}
}
您是否嘗試過使用不同的對焦模式?例如,也許看到FOCUS_MODE_AUTO或FOCUS_MODE_CONTINUOUS_VIDEO的效果是什麼。這只是從快速瀏覽代碼,因此不是一個明確的答案。 –
是的,我有。其實FOCUS_MODE_AUTO可能是最好的(儘管它在我的例子中被註釋掉了)。 – emhomm4