在单独的线程中关闭CameraDevice [英] Close CameraDevice in a seperate thread
问题描述
我正在使用Android camera2创建自定义相机. cameraDevice.close()方法很慢,它会使UI冻结1秒钟.我把它放在另一个线程中,它似乎工作得很好.我想知道这是否会引起一些严重的问题,以及是否还有另一种方法可以解决此问题.这是我的closeCamera方法:
I am using Android camera2 to create a custom camera. The cameraDevice.close() method is slow and it makes UI freeze for 1 sec. I put it in another thread and it seems to work just fine. I want to know if this will cause some serious problem and whether there is another way to achieve this. Here is my closeCamera method:
private void closeCamera() {
boolean release = false;
try {
mCameraOpenCloseLock.acquire();
release = true;
} catch (InterruptedException e) {
release = false;
}
try {
preparing = true;
if (mCaptureSession != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && mCaptureSession.isReprocessable()
|| validCameraSession) {
mCaptureSession.close();
}
mCaptureSession = null;
validCameraSession = false;
}
} catch (IllegalStateException e) {
mCaptureSession = null;
} catch (Exception e) {
mCaptureSession = null;
}
try {
new Thread(new Runnable() {
@Override
public void run() {
if (mCameraDevice != null) {
if (openCamera) {
mCameraDevice.close();
mCameraDevice = null;
}
}
}
}).start();
} catch (IllegalStateException e) {
Log.e(TAG, "closeCamera: mCaptureSession - ", e);
} catch (Exception e) {
Log.e(TAG, "closeCamera: mCaptureSession - ", e);
}
if (release) {
if (mCameraOpenCloseLock != null) {
int lock = mCameraOpenCloseLock.availablePermits();
if (lock > 1) mCameraOpenCloseLock.release(lock - 1);
else if (lock == 0) mCameraOpenCloseLock.release();
}
}
}
我认为当mCameraDevice尚未关闭但用户再次打开相机时,可能会导致崩溃.但这是极少数情况,我想在再次打开相机之前再进行一次检查.我不希望UI冻结1秒以使其关闭,除了将其放在单独的线程中之外,还有其他方法可以实现吗?
I think it may cause crash when mCameraDevice has not been closed but user open camera again. But it is rare case, and I am thinking of putting another check before open camera again. I don't want my UI to freeze 1 sec for it to close, is there any other way I can achieve that except putting it in seperate thread?
推荐答案
据我所知,cameraDevice.close()的这种冻结发生在某些不幸的设备上,有时可以通过执行常规系统升级来解决.
As far as I know, such freeze with cameraDevice.close() happens on some unfortunate devices, and sometimes is cured by performing a normal system upgrade.
但是,如果这发生在您的设备上,这有点安慰.实际上,您很幸运可以为此准备一个修复程序.您的应用程序的最终用户将从您的不幸中受益.
But this is a little consolation if this happens to you, on your device. Actually, you are kind of lucky that you can prepare a fix for that. The end-users of your app will benefit from your misfortune.
如果您的代码可以为您提供所需的改进,则您的代码看起来不错.正如我所解释的,可能很难在另一个设备上重现此问题.
Your code looks OK, if it delivers desired improvements for you. As I explained, it may be hard to reproduce this problem on another device.
我宁愿将所有closeCamera()
逻辑放在同一后台线程上.如果您为 openCamera()提供了 Handler ,则为
I would rather put all closeCamera()
logic on the same background thread. If you provided a Handler to openCamera(), as in the official example,
manager.openCamera(mCameraId, mStateCallback, mBackgroundHandler);
然后我建议将所有closeCamera()
序列发布到此 mBackgroundHandler .
then I would suggest posting all closeCamera()
sequence to this mBackgroundHandler.
这篇关于在单独的线程中关闭CameraDevice的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!