必须再次重启手机才能使相机正常工作

发布于 2024-11-01 00:56:19 字数 6446 浏览 1 评论 0原文

嘿伙计们 我正在通过我的应用程序访问相机。 一切都很好,我什至可以将照片保存到我的 SD 卡上,但是

当我捕获图像时,应用程序似乎卡住了,而且如果我回家并再次返回应用程序,我会遇到强制关闭错误。以下是我在 logcat 中收到的错误

04-13 17:20:40.818: ERROR/AndroidRuntime(1149): java.lang.RuntimeException: Fail to connect to camera service
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.hardware.Camera.native_setup(Native Method)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.hardware.Camera.<init>(Camera.java:118)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.hardware.Camera.open(Camera.java:91)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at org.mycamera.mycamera.surfaceCreated(mycamera.java:70)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.view.SurfaceView.updateWindow(SurfaceView.java:540)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.view.SurfaceView.dispatchDraw(SurfaceView.java:339)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.view.View.draw(View.java:6742)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.view.ViewGroup.drawChild(ViewGroup.java:1648)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.view.View.draw(View.java:6742)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1872)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.view.ViewRoot.draw(ViewRoot.java:1422)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1167)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1744)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.os.Looper.loop(Looper.java:144)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.app.ActivityThread.main(ActivityThread.java:4937)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at java.lang.reflect.Method.invokeNative(Native Method)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at java.lang.reflect.Method.invoke(Method.java:521)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at dalvik.system.NativeStart.main(Native Method)

以下是代码:-

SurfaceHolder holder;
SurfaceView  surface;
Camera camera;
Boolean isPreviewRunning;
//Preview mpreview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    isPreviewRunning = false;
    surface = (SurfaceView)findViewById(R.id.surface);
    holder = surface.getHolder();
  holder.addCallback(this);
  holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

  Button btn = (Button)findViewById(R.id.click);
    btn.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            camera.takePicture(mShutterCallback, mPictureCallbackRaw, mPictureCallbackJpeg);
            //onCreate(null);
        }

    });

}


@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
/*  if (isPreviewRunning) {  
          camera.stopPreview();  
         }  */

    Camera.Parameters parameters = camera.getParameters();

    List<Size> sizes = parameters.getSupportedPreviewSizes();
    Size size = sizes.get(0);
    parameters.setPreviewSize(size.width, size.height);

    camera.setParameters(parameters);
     camera.startPreview();  
     isPreviewRunning=true;
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub
     camera = Camera.open();
     try {
        camera.setPreviewDisplay(holder);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    camera.stopPreview();
    camera.release();
    camera=null;
}

 protected void onRestoreInstanceState(Bundle savedInstanceState)
 {

 super.onRestoreInstanceState(savedInstanceState);
 }

 Camera.PictureCallback mPictureCallbackRaw = new Camera.PictureCallback() {
 public void onPictureTaken(byte[] data, Camera c) {
  Log.e(getClass().getSimpleName(), "PICTURE CALLBACK RAW: " + data);
  camera.startPreview();
 }
 };

 Camera.PictureCallback mPictureCallbackJpeg= new Camera.PictureCallback() {
 public void onPictureTaken(byte[] data, Camera c) {
  Log.e(getClass().getSimpleName(), "PICTURE CALLBACK JPEG: data.length = " + data);
 }
 };

 Camera.ShutterCallback mShutterCallback = new Camera.ShutterCallback() {
 public void onShutter() {
 Log.e(getClass().getSimpleName(), "SHUTTER CALLBACK");
 }
 };

 protected void onResume()
 {

 Log.e(getClass().getSimpleName(), "onResume");
 super.onResume();
 }

 protected void onSaveInstanceState(Bundle outState)
 {
 super.onSaveInstanceState(outState);
 }

 protected void onStop()
 {
 Log.e(getClass().getSimpleName(), "onStop");
 super.onStop();
 }

Hey guys
I am accessing camera through my application.
It is all well and i can even save a photo to my sd card but

when i capture the image the application seems to get stucked plus if i go to home and den return to the application again i have a force close error. The following is the error i get in logcat

04-13 17:20:40.818: ERROR/AndroidRuntime(1149): java.lang.RuntimeException: Fail to connect to camera service
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.hardware.Camera.native_setup(Native Method)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.hardware.Camera.<init>(Camera.java:118)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.hardware.Camera.open(Camera.java:91)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at org.mycamera.mycamera.surfaceCreated(mycamera.java:70)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.view.SurfaceView.updateWindow(SurfaceView.java:540)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.view.SurfaceView.dispatchDraw(SurfaceView.java:339)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.view.View.draw(View.java:6742)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.view.ViewGroup.drawChild(ViewGroup.java:1648)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1375)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.view.View.draw(View.java:6742)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1872)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.view.ViewRoot.draw(ViewRoot.java:1422)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1167)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1744)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.os.Looper.loop(Looper.java:144)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at android.app.ActivityThread.main(ActivityThread.java:4937)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at java.lang.reflect.Method.invokeNative(Native Method)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at java.lang.reflect.Method.invoke(Method.java:521)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-13 17:20:40.818: ERROR/AndroidRuntime(1149):     at dalvik.system.NativeStart.main(Native Method)

Following is the code :-

SurfaceHolder holder;
SurfaceView  surface;
Camera camera;
Boolean isPreviewRunning;
//Preview mpreview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    isPreviewRunning = false;
    surface = (SurfaceView)findViewById(R.id.surface);
    holder = surface.getHolder();
  holder.addCallback(this);
  holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

  Button btn = (Button)findViewById(R.id.click);
    btn.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            camera.takePicture(mShutterCallback, mPictureCallbackRaw, mPictureCallbackJpeg);
            //onCreate(null);
        }

    });

}


@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
/*  if (isPreviewRunning) {  
          camera.stopPreview();  
         }  */

    Camera.Parameters parameters = camera.getParameters();

    List<Size> sizes = parameters.getSupportedPreviewSizes();
    Size size = sizes.get(0);
    parameters.setPreviewSize(size.width, size.height);

    camera.setParameters(parameters);
     camera.startPreview();  
     isPreviewRunning=true;
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub
     camera = Camera.open();
     try {
        camera.setPreviewDisplay(holder);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    camera.stopPreview();
    camera.release();
    camera=null;
}

 protected void onRestoreInstanceState(Bundle savedInstanceState)
 {

 super.onRestoreInstanceState(savedInstanceState);
 }

 Camera.PictureCallback mPictureCallbackRaw = new Camera.PictureCallback() {
 public void onPictureTaken(byte[] data, Camera c) {
  Log.e(getClass().getSimpleName(), "PICTURE CALLBACK RAW: " + data);
  camera.startPreview();
 }
 };

 Camera.PictureCallback mPictureCallbackJpeg= new Camera.PictureCallback() {
 public void onPictureTaken(byte[] data, Camera c) {
  Log.e(getClass().getSimpleName(), "PICTURE CALLBACK JPEG: data.length = " + data);
 }
 };

 Camera.ShutterCallback mShutterCallback = new Camera.ShutterCallback() {
 public void onShutter() {
 Log.e(getClass().getSimpleName(), "SHUTTER CALLBACK");
 }
 };

 protected void onResume()
 {

 Log.e(getClass().getSimpleName(), "onResume");
 super.onResume();
 }

 protected void onSaveInstanceState(Bundle outState)
 {
 super.onSaveInstanceState(outState);
 }

 protected void onStop()
 {
 Log.e(getClass().getSimpleName(), "onStop");
 super.onStop();
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一人独醉 2024-11-08 00:56:19

最有可能的是,这是因为您太早再次调用 startPreview() 。来自 Camera.takePicture() 的 SDK 文档:

调用此方法后,在 JPEG 回调返回之前,不得调用 startPreview() 或拍摄另一张照片。

您正在调用 startPreview() 来重新启动 mPictureCallbackRaw 中的预览帧,而您应该等到 mPictureCallbackJpeg。这可能会扰乱相机服务并使其进入奇怪的状态。

希望有帮助!

Most likely, it's because your calling startPreview() again too early. From the SDK documentation on Camera.takePicture():

After calling this method, you must not call startPreview() or take another picture until the JPEG callback has returned.

You are calling startPreview() to restart the preview frames in your mPictureCallbackRaw, when you should wait until the mPictureCallbackJpeg. This is probably messing with the Camera service and putting it into a weird state.

Hope that Helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文