我在 Galaxy Nexus 上使用新的 API 14+ FaceDetectionListener来检测相机预览中的人脸。一切正常,我可以拿起人脸,但是当我尝试在 SurfaceHolder 被破坏时关闭相机时,我得到一个 RuntimeException ,上面写着“停止人脸检测失败”。我用来设置和停止人脸检测的代码如下所示:
public void surfaceChanged( SurfaceHolder holder, int format, int w, int h ) {
if( isPreviewRunning ) {
camera.stopPreview();
}
initPreview( w, h );
camera.startPreview();
if( enableFaceRecognition ) {
camera.startFaceDetection();
}
isPreviewRunning = true;
}
public void surfaceDestroyed( SurfaceHolder holder ) {
if( camera != null ) {
if( enableFaceRecognition ) {
try {
camera.stopFaceDetection();
} catch( Exception e ) {
Log.e( TAG, "Huh?", e );
}
}
cameraConfigured = false;
camera.stopPreview();
camera.release();
camera = null;
}
}
哦,还有:
enableFaceRecognition = camera.getParameters().getMaxNumDetectedFaces() > 0
编辑:
这是 logcat 输出:
07-31 18:59:47.809: E/Preview(9410): Huh?
07-31 18:59:47.809: E/Preview(9410): java.lang.RuntimeException: stop face detection failed
07-31 18:59:47.809: E/Preview(9410): at android.hardware.Camera._stopFaceDetection(Native Method)
07-31 18:59:47.809: E/Preview(9410): at android.hardware.Camera.stopFaceDetection(Camera.java:1138)
07-31 18:59:47.809: E/Preview(9410): at app.face.Preview.surfaceDestroyed(Preview.java:126)
07-31 18:59:47.809: E/Preview(9410): at android.view.SurfaceView.updateWindow(SurfaceView.java:517)
07-31 18:59:47.809: E/Preview(9410): at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:226)
07-31 18:59:47.809: E/Preview(9410): at android.view.View.dispatchWindowVisibilityChanged(View.java:5839)
07-31 18:59:47.809: E/Preview(9410): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:945)
07-31 18:59:47.809: E/Preview(9410): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:945)
07-31 18:59:47.809: E/Preview(9410): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:945)
07-31 18:59:47.809: E/Preview(9410): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:945)
07-31 18:59:47.809: E/Preview(9410): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:945)
07-31 18:59:47.809: E/Preview(9410): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:965)
07-31 18:59:47.809: E/Preview(9410): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
07-31 18:59:47.809: E/Preview(9410): at android.os.Handler.dispatchMessage(Handler.java:99)
07-31 18:59:47.809: E/Preview(9410): at android.os.Looper.loop(Looper.java:137)
07-31 18:59:47.809: E/Preview(9410): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-31 18:59:47.809: E/Preview(9410): at java.lang.reflect.Method.invokeNative(Native Method)
07-31 18:59:47.809: E/Preview(9410): at java.lang.reflect.Method.invoke(Method.java:511)
07-31 18:59:47.809: E/Preview(9410): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-31 18:59:47.809: E/Preview(9410): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-31 18:59:47.809: E/Preview(9410): at dalvik.system.NativeStart.main(Native Method)
任何想法是什么导致了这个异常?