运行时错误:无法连接到android中的相机服务

发布于 2024-12-07 14:42:59 字数 1754 浏览 0 评论 0原文

我想利用 zxing 库来检测我的应用程序中的二维码。但出于应用程序查看的目的,我必须将自定义显示方向更改为纵向。因此,我必须将整个 zxing 库集成到我的应用程序中,并将camera.setDisplayOrientation(90) 添加到 openDriver() 方法中。 执行此操作后,程序可以运行,但我随机收到“运行时异常:无法连接到相机服务”。

public void openDriver(SurfaceHolder holder) throws IOException {
  if (camera == null) {
      camera = Camera.open();
      camera.setDisplayOrientation(90);

      if (camera == null) {
          throw new IOException();
      }
  }
  camera.setPreviewDisplay(holder);
  if (!initialized) {
  initialized = true;
  configManager.initFromCameraParameters(camera);
}
configManager.setDesiredCameraParameters(camera);

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
reverseImage = prefs.getBoolean(PreferencesActivity.KEY_REVERSE_IMAGE, false);
if (prefs.getBoolean(PreferencesActivity.KEY_FRONT_LIGHT, false)) {
  FlashlightManager.enableFlashlight();
 }
}

public void closeDriver() {
    if (camera != null) {
        FlashlightManager.disableFlashlight();
        camera.release();
        camera = null;
        framingRect = null;
        framingRectInPreview = null;
    }
}

/**
 * Asks the camera hardware to begin drawing preview frames to the screen.
 */
public void startPreview() {
    if (camera != null && !previewing) {
        camera.startPreview();
        previewing = true;
    }
}

/**
 * Tells the camera to stop drawing preview frames.
 */
public void stopPreview() {
    if (camera != null && previewing) {
        if (!useOneShotPreviewCallback) {
            camera.setPreviewCallback(null);
        }
        camera.stopPreview();
        previewCallback.setHandler(null, 0);
        autoFocusCallback.setHandler(null, 0);
        previewing = false;
    }
}    

I wanted to make use of the zxing library to detect qrcodes in my app. But for the apps viewing purpose, i had to change the custom display orientation to portrait. Hence i had to integrate the whole zxing library into my app and addded camera.setDisplayOrientation(90) to the openDriver() method.
After doing this, the program works, but I get "Runtime exceptions : Fail to connect to camera service" randomly.

public void openDriver(SurfaceHolder holder) throws IOException {
  if (camera == null) {
      camera = Camera.open();
      camera.setDisplayOrientation(90);

      if (camera == null) {
          throw new IOException();
      }
  }
  camera.setPreviewDisplay(holder);
  if (!initialized) {
  initialized = true;
  configManager.initFromCameraParameters(camera);
}
configManager.setDesiredCameraParameters(camera);

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
reverseImage = prefs.getBoolean(PreferencesActivity.KEY_REVERSE_IMAGE, false);
if (prefs.getBoolean(PreferencesActivity.KEY_FRONT_LIGHT, false)) {
  FlashlightManager.enableFlashlight();
 }
}

public void closeDriver() {
    if (camera != null) {
        FlashlightManager.disableFlashlight();
        camera.release();
        camera = null;
        framingRect = null;
        framingRectInPreview = null;
    }
}

/**
 * Asks the camera hardware to begin drawing preview frames to the screen.
 */
public void startPreview() {
    if (camera != null && !previewing) {
        camera.startPreview();
        previewing = true;
    }
}

/**
 * Tells the camera to stop drawing preview frames.
 */
public void stopPreview() {
    if (camera != null && previewing) {
        if (!useOneShotPreviewCallback) {
            camera.setPreviewCallback(null);
        }
        camera.stopPreview();
        previewCallback.setHandler(null, 0);
        autoFocusCallback.setHandler(null, 0);
        previewing = false;
    }
}    

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

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

发布评论

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

评论(1

じее 2024-12-14 14:42:59

我怀疑是方向改变造成的。我发现每当活动停止但无法在 onPause 中调用 Camera.release 时,您都会收到该错误。结果是,下次您尝试执行 Camera.open 时,您会收到运行时错误,因为驱动程序仍然认为它已打开,无论打开它的应用程序/活动是否已消失。

当调试/测试某些东西抛出异常并导致活动中断时,您可以轻松地实现这种情况。您需要非常勤奋地捕获所有异常,并确保在完成活动之前释放相机。

顺便说一句,您是否发现需要重新启动设备才能再次打开相机?

I doubt that the orientation change is causing that. I have found you will get that error whenever an activity stops but fails to call Camera.release in their onPause. The result is that the next time you try to do Camera.open you get that runtime error since the driver still considers it open regardless of the app/activity that opened it being gone.

You can easily get this to happen while debugging/testing stuff when something throws an exception and brings the activity down. You need to be very diligent about catching all exceptions and being sure to release the camera before finishing the activity.

BTW, are you finding you need to power cycle the device in order to be able to open the camera again?

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