gpt4 book ai didi

java - Android Camera2 - 当 E/Camera 不可用时回调以拦截 E/Camera 错误(即用户自拍)

转载 作者:行者123 更新时间:2023-11-29 19:02:16 24 4
gpt4 key购买 nike

在我的应用程序中,我使用 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 Error 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() 不会触发?我怎样才能检测到这种情况?我认为能够“利用”以下任何事件都可以解决问题:

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

感谢您的任何反馈。

最佳答案

好的,所以进一步调查我已经找到了解决方案。问题是原生支持 Camera 1 API 的“传统”设备虽然可以“使用”Camera 2 API,但其行为不同于原生支持 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

现在,“受限”和“完整”设备将按预期运行,并且在相机变得不可用时,StateCallback 的 onDisconnect() 事件会触发,因此您可以使用react。对于“传统”设备,至少根据我的测试,这发生得太晚了(根据原始问题中的 LOGCAT)。

解决方案似乎是根据硬件级别在 Camera API 之间切换,然后使用 mCamera.setErrorCallback(errorCallback); 捕获 Camera 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.
}
}

关于java - Android Camera2 - 当 E/Camera 不可用时回调以拦截 E/Camera 错误(即用户自拍),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48569717/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com