1

在使用相机实时预览作为背景的特定条件之后,我一直试图调用同一个类。但我遇到了一个错误。

日志猫:

02-03 19:52:42.274: E/AndroidRuntime(454): FATAL EXCEPTION: main
02-03 19:52:42.274: E/AndroidRuntime(454): java.lang.RuntimeException: Fail to connect to camera service
02-03 19:52:42.274: E/AndroidRuntime(454):  at android.hardware.Camera.native_setup(Native Method)
02-03 19:52:42.274: E/AndroidRuntime(454):  at android.hardware.Camera.<init>(Camera.java:258)
02-03 19:52:42.274: E/AndroidRuntime(454):  at android.hardware.Camera.open(Camera.java:235)
02-03 19:52:42.274: E/AndroidRuntime(454):  at com.example.gems.GamePlay.surfaceCreated(GamePlay.java:996)
02-03 19:52:42.274: E/AndroidRuntime(454):  at android.view.SurfaceView.updateWindow(SurfaceView.java:543)
02-03 19:52:42.274: E/AndroidRuntime(454):  at android.view.SurfaceView.dispatchDraw(SurfaceView.java:348)
02-03 19:52:42.274: E/AndroidRuntime(454):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
02-03 19:52:42.274: E/AndroidRuntime(454):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
02-03 19:52:42.274: E/AndroidRuntime(454):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
02-03 19:52:42.274: E/AndroidRuntime(454):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
02-03 19:52:42.274: E/AndroidRuntime(454):  at android.view.View.draw(View.java:6883)
02-03 19:52:42.274: E/AndroidRuntime(454):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
02-03 19:52:42.274: E/AndroidRuntime(454):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
02-03 19:52:42.274: E/AndroidRuntime(454):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
02-03 19:52:42.274: E/AndroidRuntime(454):  at android.view.View.draw(View.java:6883)
02-03 19:52:42.274: E/AndroidRuntime(454):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
02-03 19:52:42.274: E/AndroidRuntime(454):  at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)
02-03 19:52:42.274: E/AndroidRuntime(454):  at android.view.ViewRoot.draw(ViewRoot.java:1522)
02-03 19:52:42.274: E/AndroidRuntime(454):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
02-03 19:52:42.274: E/AndroidRuntime(454):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
02-03 19:52:42.274: E/AndroidRuntime(454):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-03 19:52:42.274: E/AndroidRuntime(454):  at android.os.Looper.loop(Looper.java:123)
02-03 19:52:42.274: E/AndroidRuntime(454):  at android.app.ActivityThread.main(ActivityThread.java:3683)
02-03 19:52:42.274: E/AndroidRuntime(454):  at java.lang.reflect.Method.invokeNative(Native Method)
02-03 19:52:42.274: E/AndroidRuntime(454):  at java.lang.reflect.Method.invoke(Method.java:507)
02-03 19:52:42.274: E/AndroidRuntime(454):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-03 19:52:42.274: E/AndroidRuntime(454):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-03 19:52:42.274: E/AndroidRuntime(454):  at dalvik.system.NativeStart.main(Native Method)

我正在像这样创建我的相机。

Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;


getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
surfaceHolder = surfaceView.getHolder();  
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);


public void surfaceChanged(SurfaceHolder holder, int format, int width,
         int height) 
{
        // TODO Auto-generated method stub
        if(previewing){
         camera.stopPreview();
         previewing = false;
        }

        if (camera != null){
            Parameters parameters = camera.getParameters();
            Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();

            if(display.getRotation() == Surface.ROTATION_0)
            {
                parameters.setPreviewSize(height, width);                           
                camera.setDisplayOrientation(90);
            }

            if(display.getRotation() == Surface.ROTATION_90)
            {
                parameters.setPreviewSize(width, height);                           
            }

            if(display.getRotation() == Surface.ROTATION_180)
            {
                parameters.setPreviewSize(height, width);               
            }

            if(display.getRotation() == Surface.ROTATION_270)
            {
                parameters.setPreviewSize(width, height);
                camera.setDisplayOrientation(180);
            }

            camera.setParameters(parameters);
            try 
            {           
                camera.setPreviewDisplay(surfaceHolder);          
                camera.startPreview();
                previewing = true;
            }
            catch(Exception e)
            {
                Log.d("Error", "Cannot start preview", e);    
            }
        }

}


        public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
            try {

                camera = Camera.open();  
                camera.setPreviewDisplay(surfaceHolder);

            } catch (IOException e) { }

        }

        public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        if (null == camera)
            return;
        camera.stopPreview();
        camera.release();
        camera = null;
        previewing = false;
        }

当我尝试调用完成然后再次调用同一类时,我在顶部收到错误。有任何想法吗?

4

1 回答 1

0

“无法连接到相机服务”通常发生在相机被其他应用程序使用或未正确释放时。

当您调用 Camera.open 时,您会为您保留相机,直到您调用 Camera.release()。这里奇怪的是你在surfaceDestroyed中调用camera.release。所以相机应该被释放。

我可以建议你做的是在调用finish()之前调用camera.release。您还可以在surfaceDestroyed 中的camera.stopPreview() 行上放置一个断点。

于 2013-04-03T16:28:02.327 回答