我已經搜索了一些解決方案,並確定存在許多與此相關的問題,但沒有解決我的問題。無法使用自動對焦拍攝照片
我收到運行時異常: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());
}
}
我覺得有些事情是缺乏在我運行的代碼,我想省略可運行和執行一次自動對焦,它去掉了運行時錯誤。我錯過了重新初始化這個部分的東西嗎?
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) {
}
}
請在您看到的錯誤上發佈更多詳細信息。從您的應用程序開始到失敗的日誌片段將是最好的。 – 2013-02-26 22:50:30