gpt4 book ai didi

java - Android 相机在两次拍摄之间失焦

转载 作者:行者123 更新时间:2023-11-30 00:30:17 25 4
gpt4 key购买 nike

我使用的是过时的 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) {
}
}



}

最佳答案

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

因此,在每次捕获之后:

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

关于java - Android 相机在两次拍摄之间失焦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44533072/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com