在 SurfaceView 上绘图时如何正确锁定/解锁相机?
我创建了一个具有增强现实功能的活动。其中一部分是让手机摄像头显示在后台(即 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需将这样的内容添加到 surfaceCreated 回调中:
然后在 onPause() 内部添加释放并调用回调:
现在它就像一个魅力!干杯!
Just add something like this to surfaceCreated callback:
And then inside onPause() add release and call to callback:
Now it works like a charm! Cheers!