【问题标题】:Android GPUImage setImage and getBitmapWithFilterApplied cause screen to flickerAndroid GPUImage setImage 和 getBitmapWithFilterApplied 导致屏幕闪烁
【发布时间】:2021-02-09 02:16:02
【问题描述】:

我一直使用这个 github 作为我代码的开始:https://github.com/xizhang/camerax-gpuimage

该代码是一种显示带有 GPUImage 过滤器的相机视图的方法。 我还希望能够分析应用了过滤器的位图以获得一些分析(图像中的红色/绿色/蓝色百分比)。

我已经成功地向用户展示了默认的相机视图以及我创建的过滤器。

通过注释掉 setImage 代码行,我已经能够获得过滤图像的分析,但是当我尝试同时执行这两个操作时,屏幕会闪烁。我更改了 StartCameraIfReady 函数以获取过滤后的图像,如下所示:

    @SuppressLint("UnsafeExperimentalUsageError")
    private fun startCameraIfReady() {
        if (!isPermissionsGranted() || cameraProvider == null) {
            return;
        }
        val imageAnalysis = ImageAnalysis.Builder().setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST).build()
        imageAnalysis.setAnalyzer(executor, ImageAnalysis.Analyzer {
            var bitmap = allocateBitmapIfNecessary(it.width, it.height)
            converter.yuvToRgb(it.image!!, bitmap)
            it.close()
            gpuImageView.post {
                // These two lines conflict causing the screen to flicker
                // I can comment out one or the other and it works great
                // But running both at the same time causes issues
                gpuImageView.gpuImage.setImage(bitmap)
                val filtered = gpuImageView.gpuImage.getBitmapWithFilterApplied(bitmap)

                /*
                Analyze the filtered image...
                Print details about image here
                 */
            }
        })
        cameraProvider!!.unbindAll()
        cameraProvider!!.bindToLifecycle(this, CameraSelector.DEFAULT_BACK_CAMERA, imageAnalysis)
    }

当我尝试获取过滤后的位图时,它似乎与 setImage 代码行冲突并导致屏幕闪烁,如下面的视频所示。我可以向用户显示预览,也可以分析图像,但不能同时进行。我尝试过在它们自己的后台线程上同步运行它们。我还尝试添加另一个图像分析器并将其绑定到相机生命周期(一个用于预览,另一个用于获取过滤后的位图),屏幕仍然闪烁但频率较低。

https://imgur.com/a/mXeuEhe

<blockquote class="imgur-embed-pub" lang="en" data-id="a/mXeuEhe"  ><a href="//imgur.com/a/mXeuEhe">screenRocord</a></blockquote><script async src="//s.imgur.com/min/embed.js" charset="utf-8"></script>

【问题讨论】:

    标签: android kotlin gpuimage android-camerax


    【解决方案1】:

    如果您要获得应用了过滤器的位图,您甚至不需要 GPUImageView。您可以只获取位图,然后将其设置在常规 ImageView 上。这就是您的分析仪的外观:

    ImageAnalysis.Analyzer {
       var bitmap = allocateBitmapIfNecessary(it.width, it.height)
       converter.yuvToRgb(it.image!!, bitmap)
       it.close()
       val filteredBitmap = gpuImage.getBitmapWithFilterApplied(bitmap)
       regularImageView.post {
          regularImageView.setImageBitmap(filteredBitmap)
       }
    }
    

    请注意,原始的 GitHub 示例效率低下,上面的示例更糟糕,因为它们将输出转换为位图,然后再将其反馈给 GPU。为获得最佳性能参数预览流,请参阅CameraX's core test app,了解如何通过 OpenGL 访问预览 Surface。

    【讨论】:

      猜你喜欢
      • 2012-08-27
      • 2013-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-23
      • 1970-01-01
      • 2011-09-29
      • 1970-01-01
      相关资源
      最近更新 更多