2

在我的应用程序中,我使用 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

感谢您的任何反馈。M。

4

1 回答 1

3

好的,所以进一步调查这个我已经找到了一个解决方案。问题是本机支持 Camera 1 API 的“Legacy”设备,尽管它们与 Camera 2 API “工作”,但其行为与本机支持 Camera 2 API 的“Limited”/“Full”设备不同。

要检测设备类型,您可以使用以下代码段:

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() 事件将触发,因此您可以做出反应。对于“传统”设备,至少根据我的测试,这发生得太晚了(根据原始问题中的 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.
    }
}
于 2018-02-10T05:08:35.570 回答