当使用 Samsung S20 将 imageproxy 转换为位图时,cameraX 中分析的绿色位图

You*_*ton 5 android image bitmap android-camerax

我已经使用底部代码了。但是,当我仅使用最新智能手机的 imageproxy 进行位图时,它显示为绿色位图。比如三星s20

底部代码在过去的设备上运行良好谁有问题?

在此输入图像描述

fun Image.toBitmap(): Bitmap {
val yBuffer = planes[0].buffer // Y
val uBuffer = planes[1].buffer // U
val vBuffer = planes[2].buffer // V

val ySize = yBuffer.remaining()
val uSize = uBuffer.remaining()
val vSize = vBuffer.remaining()

val nv21 = ByteArray(ySize + uSize + vSize)

//U and V are swapped
yBuffer.get(nv21, 0, ySize)
vBuffer.get(nv21, ySize, vSize)
uBuffer.get(nv21, ySize + vSize, uSize)

val yuvImage = YuvImage(nv21, ImageFormat.NV21, this.width, this.height, null)
val out = ByteArrayOutputStream()
yuvImage.compressToJpeg(Rect(0, 0, yuvImage.width, yuvImage.height), 50, out)
val imageBytes = out.toByteArray()
return BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size)
}
Run Code Online (Sandbox Code Playgroud)

Hus*_*eem 0

val nv21 = ByteArray(ySize + uSize + vSize)
Run Code Online (Sandbox Code Playgroud)

并非每个YUV_420_888缓冲区实际上都是 NV21 格式。您还必须处理其他格式,例如 NV12、YU12 和 YV12。相机示例Github 存储库上有一个YUV 到位图转换器示例,请尝试一下,它应该可以正常工作。