在 SurfaceView 上绘图时如何正确锁定/解锁相机?

发布于 2024-11-11 15:48:44 字数 1064 浏览 2 评论 0原文

我创建了一个具有增强现实功能的活动。其中一部分是让手机摄像头显示在后台(即 Activity 布局中的 SurfaceView)。

在活动 onCreate() 方法中,我执行以下操作:

surfaceHolder = surfaceView.getHolder();
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceHolder.addCallback(this);

我的回调是:

@Override
public void surfaceCreated(SurfaceHolder holder) {

    try {
        camera = Camera.open();
        camera.setDisplayOrientation(90);
        camera.setPreviewDisplay(holder);
    } catch (Exception e) {
        // e.printStackTrace();
    }
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    camera.startPreview();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    camera.stopPreview();
    camera.release();
}

现在,当我关闭应用程序(同时将其保留在后台)时,相机应用程序无法正常工作,因为相机仍锁定在我的应用程序内。

我应该如何实现 Activity onResume() onPause() 方法才能锁定/解锁相机?

另外,将相机锁定在应用程序中会占用 Galaxy S 上大约 5% 的 CPU,当我回到我的应用程序后,当尝试camera.startPreview(); 时,它会在 surfaceChanged() 中崩溃。

帮助!

I created an Activity with Augmented Reality functionality. Part of that is to have phone camera display in the background (which is SurfaceView in Activity layout).

in activity onCreate() method I do:

surfaceHolder = surfaceView.getHolder();
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceHolder.addCallback(this);

My callbacks are:

@Override
public void surfaceCreated(SurfaceHolder holder) {

    try {
        camera = Camera.open();
        camera.setDisplayOrientation(90);
        camera.setPreviewDisplay(holder);
    } catch (Exception e) {
        // e.printStackTrace();
    }
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    camera.startPreview();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    camera.stopPreview();
    camera.release();
}

Right now when I close my application (while leaving it in the background) Camera app is not working because Camera is still locked inside my app.

How should I implement activity onResume() onPause() methods in order to lock/unlock camera?

Also leaving camera locked in the app takes up about 5% of CPU on Galaxy S and after I come back to my app - it crashes in surfaceChanged() when trying to camera.startPreview();

Help!

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

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

发布评论

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

评论(1

后知后觉 2024-11-18 15:48:44

只需将这样的内容添加到 surfaceCreated 回调中:

    if (camera != null) {
        camera.release();
    }
    camera = Camera.open();
    camera.setDisplayOrientation(90);
    try {
        camera.setPreviewDisplay(holder);
    } catch (Exception e) {
    }

然后在 onPause() 内部添加释放并调用回调:

    camera.release();
    surfaceCreated(surfaceHolder);

现在它就像一个魅力!干杯!

Just add something like this to surfaceCreated callback:

    if (camera != null) {
        camera.release();
    }
    camera = Camera.open();
    camera.setDisplayOrientation(90);
    try {
        camera.setPreviewDisplay(holder);
    } catch (Exception e) {
    }

And then inside onPause() add release and call to callback:

    camera.release();
    surfaceCreated(surfaceHolder);

Now it works like a charm! Cheers!

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