createCameraPreviewSession:Surface没有有效的本机窗口-第二次创建时出错

时间:2019-01-15 12:48:53

标签: java android ionic-framework android-camera2

我们从Ionic调用了一个自定义的Cordova插件,该插件使用Camera2 API捕获帧。在Ionic中此插件的第二次调用(随后进行)中,有时会有时(例如十分之一)出现以下错误:

2019-01-14 18:08:16.471 31738-31738/io.ionic.starter E/Legacy-CameraDevice-JNI: getNativeWindow: Surface had no valid native window.
2019-01-14 18:08:16.472 31738-31738/io.ionic.starter E/Legacy-CameraDevice-JNI: LegacyCameraDevice_nativeDetectSurfaceDimens: Could not retrieve native window from surface.
2019-01-14 18:08:16.472 31738-31738/io.ionic.starter D/AndroidRuntime: Shutting down VM
2019-01-14 18:08:16.472 31738-31738/io.ionic.starter E/AndroidRuntime: FATAL EXCEPTION: main
    Process: io.ionic.starter, PID: 31738
    java.lang.UnsupportedOperationException: Unknown error -22
        at android.hardware.camera2.legacy.LegacyExceptionUtils.throwOnError(LegacyExceptionUtils.java:77)
        at android.hardware.camera2.legacy.LegacyCameraDevice.getSurfaceSize(LegacyCameraDevice.java:583)
        at android.hardware.camera2.utils.SurfaceUtils.getSurfaceSize(SurfaceUtils.java:68)
        at android.hardware.camera2.params.OutputConfiguration.<init>(OutputConfiguration.java:237)
        at android.hardware.camera2.params.OutputConfiguration.<init>(OutputConfiguration.java:101)
        at android.hardware.camera2.impl.CameraDeviceImpl.createCaptureSession(CameraDeviceImpl.java:499)

        at dev.capture.Camera2Scanner.createCameraPreviewSession(Camera2Scanner.java:675)
        at dev.capture.Camera2Scanner.access$200(Camera2Scanner.java:47)
        at dev.capture.Camera2Scanner$2.onOpened(Camera2Scanner.java:191)

        at android.hardware.camera2.impl.CameraDeviceImpl$1.run(CameraDeviceImpl.java:139)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6938)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

所以我检查了Android JNI源是否有此错误,它说当 getNativeWindow()返回null时抛出此错误。但是这个功能到底是做什么的,这对我们的表面意味着什么?

1 个答案:

答案 0 :(得分:1)

您已将Surface传递给不再有效的CameraDevice.createCaptureSession;通常,这是因为从中获得的Surface要么被垃圾回收,要么是因为绘制到的UI元素不再有效。

因此,如果SurfaceView无法再进行有效绘制或已发布ImageReader等,则可能会出现此错误。您需要找出哪个Surface无效,然后找出其来源不再处于良好状态的原因。