如何为 android Camera x androidx.camera.core.Preview 实现自定义 SurfaceProvider

Min*_*per 5 android preview android-camera androidx android-camerax

我正在关注 Android 的原始 CameraX 示例:https : //developer.android.com/codelabs/camerax-getting-started#5 和这里:https : //github.com/android/camera-samples/blob/ main/CameraXBasic/app/src/main/java/com/android/example/cameraxbasic/fragments/CameraFragment.kt#L337

我想像示例中那样实例化我的预览:

       val preview = Preview.Builder()
           .build()
           .also {
               it.setSurfaceProvider(viewFinder.surfaceProvider)
           }
Run Code Online (Sandbox Code Playgroud)

但后来我意识到......好吧,多么令人惊喜-viewFinder / androidx.camera.view.PreviewView来自一个只是 alpha 版本的库

    implementation 'androidx.camera:camera-view:1.0.0-alpha24'
Run Code Online (Sandbox Code Playgroud)

(见这里) https://github.com/android/camera-samples/blob/main/CameraXBasic/app/build.gradle

我当然不想使用 alpha 版本!所以我想好吧也许我可以实现我自己的SurfaceProvider

文档表明我可以做这样的事情:

         * class MyGlSurfaceProvider implements Preview.SurfaceProvider {
         *     // This executor must have also been used with Preview.setSurfaceProvider() to
         *     // ensure onSurfaceRequested() is called on our GL thread.
         *     Executor mGlExecutor;
         *
         *     {@literal @}Override
         *     public void onSurfaceRequested(@NonNull SurfaceRequest request) {
         *         // If our GL thread/context is shutting down. Signal we will not fulfill
         *         // the request.
         *         if (isShuttingDown()) {
         *             request.willNotProvideSurface();
         *             return;
         *         }
         *
         *         // Create the surface and attempt to provide it to the camera.
         *         Surface surface = resetGlInputSurface(request.getResolution());
         *
         *         // Provide the surface and wait for the result to clean up the surface.
         *         request.provideSurface(surface, mGlExecutor, (result) -> {
         *             // In all cases (even errors), we can clean up the state. As an
         *             // optimization, we could also optionally check for REQUEST_CANCELLED
         *             // since we may be able to reuse the surface on subsequent surface requests.
         *             closeGlInputSurface(surface);
         *         });
         *     }
         * }
Run Code Online (Sandbox Code Playgroud)

是的,但这个例子似乎有些不完整。而且我不知道如何正确实施...所以有什么建议我可以如何实施我自己的 SurfaceProvider?