从SurfaceView获取位图



我需要将SurfaceView的内容转换为位图。我尝试使用Surfaceview内部的以下代码段来实现此目的:

 public final Bitmap getScreenCopy() {
        Bitmap bitmap = Bitmap.createBitmap(
                getWidth(),
                getHeight(),
                Bitmap.Config.ARGB_8888
        );
        Canvas temporaryCanvas = new Canvas(bitmap);
        draw(temporaryCanvas); // Voodoo.
        return bitmap;
    }

我从那架无人机中收到的位图似乎是透明的,有人知道如何解决吗?

不可能使用纹理视图,因为我使用Parrot SDK,并且无人机需要SurfaceView来显示框架。

我一直在试图找到一种方法来实现这一目标,到目前为止,我还没有找到用于API级别低于24的设备的方法。绘制缓存,但除了透明度,我什么都没有。我没想到 drauctcache 无论如何都可以进行工作,因为视图本身没有吸引任何东西,并且一切都进入了Surface

对于API级别24和更新,您可以使用PixelCopy API将SurfaceView的全部或一部分复制到位图中。

这是我(Kotlin(代码的摘要:

override fun getPlotBitmap(view: PlotSurfaceView) = Single.create<Bitmap> {
    val plotBitmap = Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888)
    val listener = PixelCopy.OnPixelCopyFinishedListener { copyResult ->
        when (copyResult) {
            PixelCopy.SUCCESS -> {
                it.onSuccess(plotBitmap)
            }
            else -> {
                it.onError(RuntimeException("Pixel copy failed with result $copyResult"))
            }
        }
    }
    PixelCopy.request(view, plotBitmap, listener, view.handler)
}

最新更新