Android Camera安全开启/关闭

时间:2014-01-17 14:42:33

标签: android camera android-camera

我已经在Android上阅读了关于安全开/关相机的Android指南和问题,但是我还没有找到答案。 这是我打开相机的代码片段(在CameraView类中扩展了SurfaceView):

@Override
    public void surfaceCreated(SurfaceHolder holder) {
        try {
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.FROYO) {
                this.camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);
            } else {
                this.camera = Camera.open();
            }
        } catch (IOException ioe) {
            ioe.printStackTrace(System.out);
        }
    }

我正在检查BUILD_SDK,因为例如在Nexus7上相机.open()无法正常工作 - 我必须使用camera.open(0) - 但是SDK 8中没有camera.open(int)。这里是我关闭的相机片段:

@Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        releaseCamera();
    }

    public void releaseCamera(){
        if (camera != null) {
            camera.stopPreview();
            camera.setPreviewCallback(null);
            camera.release();
            camera = null;
        }
    }

当我与CameraView连接的活动进入onPause状态时,我也在使用releaseCamera()函数。 这段代码在大多数设备上运行正常(我在Galaxy GIO,LG 4X HD和Galaxy S2上检查过)但是我发布了一个带有此代码片段的应用程序,用户仍然报告连接到摄像头的崩溃,这里是来自它们的logcat:

java.lang.RuntimeException: Fail to connect to camera service
at android.hardware.Camera.native_setup(Native Method)
at android.hardware.Camera.<init>(Camera.java:423)
at android.hardware.Camera.open(Camera.java:384)
at com.artostolab.xray.CameraView.surfaceCreated(CameraView.java:101)
at android.view.SurfaceView.updateWindow(SurfaceView.java:606)
at android.view.SurfaceView.access$000(SurfaceView.java:88)
at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:183)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:692)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2123)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1139)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4879)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
at android.view.Choreographer.doCallbacks(Choreographer.java:579)
at android.view.Choreographer.doFrame(Choreographer.java:548)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5297)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)

他们的设备: LG Optimus G(geehrc4g) LG Optimus L7(u0)
IRIS504Q

你知道为什么会这样吗?提前致谢

更新2014年1月21日

我现在使用了一个函数来找回CommonsWare提出的Camera并且工作正常,但是现在我和其他不那么受欢迎的设备(Htc EVo 3D,城市生活,华为U8815)的用户一样。你知道可能导致这个问题的原因吗?这是他们的logcat(我更新了主要问题) -

java.lang.RuntimeException: Fail to connect to camera service
at android.hardware.Camera.native_setup(Native Method)
at android.hardware.Camera.<init>(Camera.java:300)
at android.hardware.Camera.open(Camera.java:253)
at com.artostolab.xray.CameraView.surfaceCreated(CameraView.java:105)
at android.view.SurfaceView.updateWindow(SurfaceView.java:552)
at android.view.SurfaceView.dispatchDraw(SurfaceView.java:350)
at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
at android.view.View.draw(View.java:6892)
at android.widget.FrameLayout.draw(FrameLayout.java:357)
at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1884)
at android.view.ViewRoot.draw(ViewRoot.java:1524)
at android.view.ViewRoot.performTraversals(ViewRoot.java:1260)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1864)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
at dalvik.system.NativeStart.main(Native Method)

我的功能是找到合适的相机

public int findBackCamera() {
        int cameraId = -1;
        int numberOfCameras = Camera.getNumberOfCameras();
        for (int i = 0; i < numberOfCameras; i++) {
            Camera.CameraInfo info = new Camera.CameraInfo();
            Camera.getCameraInfo(i, info);
            if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
                cameraId = i;
                break;
            }
        }
        return cameraId;
    }

1 个答案:

答案 0 :(得分:1)

  

以下是我打开相机的代码段

该代码不正确。 open()获取相机的ID,而不是CAMERA_FACING_BACK