如何为 android Camera x androidx.camera.core.Preview

问题描述

我正在关注这个适用于 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)
           }

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

    implementation 'androidx.camera:camera-view:1.0.0-alpha24'

(见这里) 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);
         *         });
         *     }
         * }

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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)