Android Camera2 - 拦截回调 E/Camera 变得不可用时出错(即用户自拍)
Android Camera2 - Callback to Intercept E/Camera Error When It Becomes Unavailable (i.e. User Takes Selfie)
在我的应用程序中,我使用 Camera2 API 在后台进行一些处理。我正在使用众所周知的 Camera2Basic(尽管现在已进行了大量修改)。一切运行良好,但有一个明显的问题 - 当用户请求相机进行某些操作时(例如启动相机应用程序拍照),我的 Camera2 退出,请参阅 LOGCAT:
E/Camera: Error 2
I/RequestThread-1: Flushing all pending requests.
I/RequestQueue: Repeating capture request cancelled.
I/CameraDeviceState: Legacy camera service transitioning to state ERROR
E/RequestQueue: cancel failed: no repeating request exists for request id: 0
E/CameraDeviceState: Cannot receive result while in state: 0
W/CaptureCollector: previewProduced called with no preview request on queue!
W/MessageQueue: Handler (android.os.Handler) {32bb202} sending message to a Handler on a dead thread
java.lang.IllegalStateException: Handler (android.os.Handler) {32bb202} sending message to a Handler on a dead thread
... (irrelevant) ...
at android.os.Looper.loop(Looper.java:154)
at android.os.HandlerThread.run(HandlerThread.java:61)
E/MyApp: disconnected
D/gralloc: gralloc_lock_ycbcr success. format : 11, usage: 3, ycbcr.y: 0x66903000, .cb: 0x66afd401, .cr: 0x66afd400, .ystride: 1920 , .cstride: 1920, .chroma_step: 2
E/BufferItemConsumer: [ImageReader-1920x1080f23m1-19683-0] Failed to release buffer: Unknown error -1 (1)
E/MyApp: closed
现在...在 Camera1 API 中,我能够在报告 E/Camera 错误 2 的那一刻准确地拦截并采取行动,使用这个:
mCamera.setErrorCallback(errorCallback);
但是在 Camera2 API 中,情况很有趣,因为即使我已经配置了 StateCallback(请注意 Log.e() 对应于上面的 LOGCAT 输出),onError () 根本不触发并且 onClosed() 和 onDisconnected() 触发得太迟 (!):
private CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
@Override
public void onOpened(@NonNull CameraDevice cameraDevice) {
mCameraOpenCloseLock.release();
mCameraDevice = cameraDevice;
createCameraPreviewSession();
}
@Override
public void onDisconnected(@NonNull CameraDevice cameraDevice) {
mCameraOpenCloseLock.release();
cameraDevice.close();
mCameraDevice = null;
Log.e("MyApp", "disconnected");
}
@Override
public void onClosed(@NonNull CameraDevice cameraDevice) {
super.onClosed(cameraDevice);
Log.e("MyApp", "closed");
}
@Override
public void onError(@NonNull CameraDevice cameraDevice, int error) {
Log.e("MyApp", "Here we are "+error);
mCameraOpenCloseLock.release();
cameraDevice.close();
mCameraDevice = null;
}
};
为什么 onError() 在相机被用户请求接管的情况下不触发?我怎样才能检测到这种情况?我认为能够 "tap into" 以下任何事件都可以解决问题:
I/CameraDeviceState: Legacy camera service transitioning to state ERROR
E/RequestQueue: cancel failed: no repeating request exists for request id: 0
E/CameraDeviceState: Cannot receive result while in state: 0
感谢您的任何反馈。
M.
好的,所以进一步调查我已经找到了解决方案。问题是 "Legacy" 设备本身支持相机 1 API,尽管它们 "work" 与相机 2 API 的行为不同于 "Limited" / "Full" 原生支持 Camera 2 的设备 API.
要检测您可以使用此代码段的设备类型:
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
Integer deviceLevel = characteristics.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL); // 0 - limited, 1 - full, 2 - legacy, 3 - uber full
现在,"Limited" 和 "Full" 设备将按预期运行,并且在相机变得不可用时,StateCallback 的 onDisconnect() 事件会触发,因此您可以做出反应。对于 "Legacy" 设备,至少根据我的测试,这发生得太晚了(根据原始问题中的 LOGCAT)。
解决方案似乎是根据硬件级别在相机 API 之间切换,然后使用 mCamera.setErrorCallback(errorCallback);
捕获相机 1 API 设备上的错误。像这样:
CameraErrorCallback errorCallback = new CameraErrorCallback();
@SuppressWarnings("deprecation")
public class CameraErrorCallback implements android.hardware.Camera.ErrorCallback {
@Override
public void onError(int error, android.hardware.Camera camera) {
// Do something.
}
}
在我的应用程序中,我使用 Camera2 API 在后台进行一些处理。我正在使用众所周知的 Camera2Basic(尽管现在已进行了大量修改)。一切运行良好,但有一个明显的问题 - 当用户请求相机进行某些操作时(例如启动相机应用程序拍照),我的 Camera2 退出,请参阅 LOGCAT:
E/Camera: Error 2
I/RequestThread-1: Flushing all pending requests.
I/RequestQueue: Repeating capture request cancelled.
I/CameraDeviceState: Legacy camera service transitioning to state ERROR
E/RequestQueue: cancel failed: no repeating request exists for request id: 0
E/CameraDeviceState: Cannot receive result while in state: 0
W/CaptureCollector: previewProduced called with no preview request on queue!
W/MessageQueue: Handler (android.os.Handler) {32bb202} sending message to a Handler on a dead thread
java.lang.IllegalStateException: Handler (android.os.Handler) {32bb202} sending message to a Handler on a dead thread
... (irrelevant) ...
at android.os.Looper.loop(Looper.java:154)
at android.os.HandlerThread.run(HandlerThread.java:61)
E/MyApp: disconnected
D/gralloc: gralloc_lock_ycbcr success. format : 11, usage: 3, ycbcr.y: 0x66903000, .cb: 0x66afd401, .cr: 0x66afd400, .ystride: 1920 , .cstride: 1920, .chroma_step: 2
E/BufferItemConsumer: [ImageReader-1920x1080f23m1-19683-0] Failed to release buffer: Unknown error -1 (1)
E/MyApp: closed
现在...在 Camera1 API 中,我能够在报告 E/Camera 错误 2 的那一刻准确地拦截并采取行动,使用这个:
mCamera.setErrorCallback(errorCallback);
但是在 Camera2 API 中,情况很有趣,因为即使我已经配置了 StateCallback(请注意 Log.e() 对应于上面的 LOGCAT 输出),onError () 根本不触发并且 onClosed() 和 onDisconnected() 触发得太迟 (!):
private CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
@Override
public void onOpened(@NonNull CameraDevice cameraDevice) {
mCameraOpenCloseLock.release();
mCameraDevice = cameraDevice;
createCameraPreviewSession();
}
@Override
public void onDisconnected(@NonNull CameraDevice cameraDevice) {
mCameraOpenCloseLock.release();
cameraDevice.close();
mCameraDevice = null;
Log.e("MyApp", "disconnected");
}
@Override
public void onClosed(@NonNull CameraDevice cameraDevice) {
super.onClosed(cameraDevice);
Log.e("MyApp", "closed");
}
@Override
public void onError(@NonNull CameraDevice cameraDevice, int error) {
Log.e("MyApp", "Here we are "+error);
mCameraOpenCloseLock.release();
cameraDevice.close();
mCameraDevice = null;
}
};
为什么 onError() 在相机被用户请求接管的情况下不触发?我怎样才能检测到这种情况?我认为能够 "tap into" 以下任何事件都可以解决问题:
I/CameraDeviceState: Legacy camera service transitioning to state ERROR
E/RequestQueue: cancel failed: no repeating request exists for request id: 0
E/CameraDeviceState: Cannot receive result while in state: 0
感谢您的任何反馈。 M.
好的,所以进一步调查我已经找到了解决方案。问题是 "Legacy" 设备本身支持相机 1 API,尽管它们 "work" 与相机 2 API 的行为不同于 "Limited" / "Full" 原生支持 Camera 2 的设备 API.
要检测您可以使用此代码段的设备类型:
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
Integer deviceLevel = characteristics.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL); // 0 - limited, 1 - full, 2 - legacy, 3 - uber full
现在,"Limited" 和 "Full" 设备将按预期运行,并且在相机变得不可用时,StateCallback 的 onDisconnect() 事件会触发,因此您可以做出反应。对于 "Legacy" 设备,至少根据我的测试,这发生得太晚了(根据原始问题中的 LOGCAT)。
解决方案似乎是根据硬件级别在相机 API 之间切换,然后使用 mCamera.setErrorCallback(errorCallback);
捕获相机 1 API 设备上的错误。像这样:
CameraErrorCallback errorCallback = new CameraErrorCallback();
@SuppressWarnings("deprecation")
public class CameraErrorCallback implements android.hardware.Camera.ErrorCallback {
@Override
public void onError(int error, android.hardware.Camera camera) {
// Do something.
}
}