我已经针对我的问题搜索了一些解决方案,并确定有很多相关问题,但没有解决我的问题。
我收到运行时异常:takepicture failed:native_autofocus
..等等。
我尝试使用自动对焦从相机拍照,我似乎无法理解可能导致错误的原因。
这是我的代码。
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());
}
}
我认为我的可运行代码中缺少某些东西,我试图省略runnable并执行一次自动聚焦,它删除了运行时错误。我错过了重新初始化这部分的内容吗?
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) {
}
}