释放摄像头:"方法发布之后被称为()"例外 [英] Releasing the camera: "Method called after release()" exception
问题描述
我想纳入我的应用程序的一些相机相关的功能。我手动打开摄像头,并获得与集previewCallback
和启动preVIEW
。我做的不的使用面显示preVIEW,但我将它设置为符合照相机API文档。这是我打开摄像头:
I'm trying to incorporate some camera-related features in my app. I open the camera manually and get the preview stream with setPreviewCallback
and startPreview
. I do not use the surface for displaying preview, but I do set it to comply with the Camera API docs. This is how I open the camera:
public Camera openCamera(int id)
{
m_openedCamera = Camera.open(id);
m_surfaceHolder = new SurfaceView(MyApplication.instance().getApplicationContext()).getHolder();
Assert.assertNotNull(m_openedCamera);
m_openedCamera.setPreviewDisplay(m_surfaceHolder);
m_openedCameraFacing = facing;
if (m_openedCamera != null)
m_openedCamera.setPreviewCallback(this);
m_openedCamera.startPreview();
}
这是我释放它,没有什么花哨这里:
And this is how I release it, nothing fancy here:
public void releaseCamera()
{
if (m_openedCamera != null)
{
m_openedCamera.stopPreview();
m_openedCamera.release();
m_openedCamera = null;
}
}
这有点儿工作在第一,但我松开相机,我得到一个异常()方法释放后,被称为。
为了澄清:我做的不的调用任何相机的方法相机已被释放之后。在调试器,我没有双重和三重检查。我觉得这里有同步和异步调用引起该问题的混合。
It kinda works at first, but as I release the camera, I get an exception "Method called after release()". To clarify: I do not call any camera methods after the camera has been released. Double and triple-checked under debugger that I don't. I think there's a mix of synchronous and asynchronous calls here that causes the problem.
推荐答案
在preVIEW回调可能会导致此异常。 上previewFrame()
不是synchroneous,它收到的相机参数,它可能会变得无效时,相机。发布()
执行。这可能即使回调不使用一个单独的线程发生(强烈推荐)。
The preview callback could cause this exception. onPreviewFrame()
is not synchroneous, and it receives a Camera parameter that may become invalid when Camera.release()
is performed. This could happen even if callbacks are not using a separate thread (highly recommended).
在发布()
,电话集previewCallback(空)
,并保持一个全球性的标志丢弃回调相机发布之后。
Before release()
, call setPreviewCallback(null)
, and keep a global flag to discard callbacks after the camera is released.
这篇关于释放摄像头:"方法发布之后被称为()"例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!