mediapipe.components.ExternalTextureConverter.setSurfaceTextureAndAttachToGLContext returns 空。使应用程序崩溃 onResume

mediapipe.components.ExternalTextureConverter.setSurfaceTextureAndAttachToGLContext returns null. makes the app crash onResume

我正在研究 jiuqiant's mediapipe face detection app。 我克隆了 repo,应用程序运行良好,检测到我的脸,但是当我暂停应用程序并恢复时,它崩溃了。 此行有错误 permalink

converter.setSurfaceTextureAndAttachToGLContext(
previewFrameTexture,
displaySize.getWidth(), 
displaySize.getHeight());

用这个日志

D/CameraXPreviewHelper: viewSize or frameSize is null.
E/SurfaceView: Exception configuring surface
    java.lang.NullPointerException: Attempt to invoke virtual method 'int android.util.Size.getWidth()' on a null object reference
        at com.example.myfacedetectionapp.MainActivity.surfaceChanged(MainActivity.java:146)
        at android.view.SurfaceView.updateSurface(SurfaceView.java:802)
        at android.view.SurfaceView.onPreDraw(SurfaceView.java:163)
        at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:977)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3044)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1845)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8235)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:994)
        at android.view.Choreographer.doCallbacks(Choreographer.java:806)
        at android.view.Choreographer.doFrame(Choreographer.java:738)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:980)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:226)
        at android.app.ActivityThread.main(ActivityThread.java:7178)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:503)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:942)

很明显,setSurfaceTextureAndAttachToGLContext 正在返回空值。 查看源码(下面是com.google.mediapipe.components.ExternalTextureConverter中的setSurfaceTextureAndAttachToGLContext函数)

if (viewSize != null && this.frameSize != null) {
    Size optimalSize = this.getOptimalViewSize(viewSize);
    return optimalSize != null ? optimalSize : this.frameSize;
} else {
    Log.d("CameraXPreviewHelper", "viewSize or frameSize is null.");
    return null;
}

似乎 this.frameSize 为空。但我不知道如何解决这个问题。

我也尝试硬编码这些值

converter.setSurfaceTextureAndAttachToGLContext(previewFrameTexture,200,200);

但它也会报错

I/GLConsumer: [SurfaceTexture-0-13076-0] attachToContext
E/GLConsumer: [SurfaceTexture-0-13076-0] attachToContext: GLConsumer is already attached to a context
E/BpSurfaceComposerClient: Failed to transact (-1)
E/BpSurfaceComposerClient: Failed to transact (-1)
D/GlThread: Stopping GL thread ExternalTextureConverter
D/CaptureSession: Opening capture session.
E/AndroidRuntime: FATAL EXCEPTION: ExternalTextureConverter
    Process: com.example.myfacedetectionapp, PID: 13076
    java.lang.RuntimeException: Error during attachToGLContext (see logcat for details)
        at android.graphics.SurfaceTexture.attachToGLContext(SurfaceTexture.java:289)
        at com.google.mediapipe.components.ExternalTextureConverter$RenderThread.setSurfaceTextureAndAttachToGLContext(ExternalTextureConverter.java:247)
        at com.google.mediapipe.components.ExternalTextureConverter.lambda$setSurfaceTextureAndAttachToGLContext$ExternalTextureConverter(ExternalTextureConverter.java:166)
        at com.google.mediapipe.components.-$$Lambda$ExternalTextureConverter$VQm8xcL00VolxXr4rMIgGUKVKnw.run(Unknown Source:8)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:226)
        at com.google.mediapipe.glutil.GlThread.run(GlThread.java:141)

是不是版本问题。看不懂。

重新创建的步骤 -

  1. 克隆 repo 和 运行.

  2. 就是这样。现在尝试暂停和恢复应用程序。 如果您没有遇到错误,请告诉我。

好的,使用 类 给定 here 有效。 只需将 com directory 放入您的 app/src/main.

请记住从 libs 目录中的 .aar 文件中删除重复项 类。