我使用以下方法捕获图像:
final Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getPackageManager()) != null)
startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
onActivityResult,我将图像保留在SD卡上,然后我调用在ViewGroup中添加了ZxingScannerView的自定义对话框。
final ViewGroup barcodeScanningFrame = (ViewGroup)
findViewById(R.id.barcodeScanningFrame);
ZxingScannerView mScannerView = new ZXingScannerView(activityToDisplayDialogBox.getApplicationContext());
barcodeScanningFrame.addView(mScannerView);
mScannerView.setResultHandler(this);
mScannerView.startCamera();
在mScannerView.startCamera()之后;我收到以下错误
E/Camera: Error 2
E/CameraPreview: java.lang.RuntimeException: cancelAutoFocus failed
java.lang.RuntimeException: cancelAutoFocus failed
at android.hardware.Camera.native_cancelAutoFocus(Native Method)
at android.hardware.Camera.cancelAutoFocus(Camera.java:1275)
at me.dm7.barcodescanner.core.CameraPreview.stopCameraPreview(CameraPreview.java:109)
at me.dm7.barcodescanner.core.CameraPreview.surfaceChanged(CameraPreview.java:61)
at android.view.SurfaceView.updateWindow(SurfaceView.java:630)
at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:161)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2205)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6337)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
at android.view.Choreographer.doCallbacks(Choreographer.java:686)
at android.view.Choreographer.doFrame(Choreographer.java:621)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
E/CameraPreview: java.lang.RuntimeException: getParameters failed (empty parameters)
java.lang.RuntimeException: getParameters failed (empty parameters)
at android.hardware.Camera.native_getParameters(Native Method)
at android.hardware.Camera.getParameters(Camera.java:1896)
at me.dm7.barcodescanner.core.CameraPreview.getOptimalPreviewSize(CameraPreview.java:188)
at me.dm7.barcodescanner.core.CameraPreview.setupCameraParameters(CameraPreview.java:119)
at me.dm7.barcodescanner.core.CameraPreview.showCameraPreview(CameraPreview.java:76)
at me.dm7.barcodescanner.core.CameraPreview.surfaceChanged(CameraPreview.java:62)
at android.view.SurfaceView.updateWindow(SurfaceView.java:630)
at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:161)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2205)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6337)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
at android.view.Choreographer.doCallbacks(Choreographer.java:686)
at android.view.Choreographer.doFrame(Choreographer.java:621)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
看起来启动相机拍摄图像的意图并不是释放相机。因为如果不这样做,mScannerView.startCamera()就可以正常工作了:
final Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getPackageManager()) != null)
startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
并直接启动自定义对话框:
我拥有所需的权限,上面的代码在marshmallow中运行正常。只是牛轧糖设备在上面提到的场景中给我带来了这个错误。
请帮助。谢谢。