【问题标题】:Convert android.media.Image (YUV_420_888) to Bitmap将 android.media.Image (YUV_420_888) 转换为位图
【发布时间】:2015-04-10 09:18:50
【问题描述】:
【问题讨论】:
标签:
android
bitmap
android-camera
yuv
【解决方案1】:
您可以使用内置的 Renderscript 内在函数 ScriptIntrinsicYuvToRGB 来执行此操作。代码取自Camera2 api Imageformat.yuv_420_888 results on rotated image:
@Override
public void onImageAvailable(ImageReader reader)
{
// Get the YUV data
final Image image = reader.acquireLatestImage();
final ByteBuffer yuvBytes = this.imageToByteBuffer(image);
// Convert YUV to RGB
final RenderScript rs = RenderScript.create(this.mContext);
final Bitmap bitmap = Bitmap.createBitmap(image.getWidth(), image.getHeight(), Bitmap.Config.ARGB_8888);
final Allocation allocationRgb = Allocation.createFromBitmap(rs, bitmap);
final Allocation allocationYuv = Allocation.createSized(rs, Element.U8(rs), yuvBytes.array().length);
allocationYuv.copyFrom(yuvBytes.array());
ScriptIntrinsicYuvToRGB scriptYuvToRgb = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));
scriptYuvToRgb.setInput(allocationYuv);
scriptYuvToRgb.forEach(allocationRgb);
allocationRgb.copyTo(bitmap);
// Release
bitmap.recycle();
allocationYuv.destroy();
allocationRgb.destroy();
rs.destroy();
image.close();
}
private ByteBuffer imageToByteBuffer(final Image image)
{
final Rect crop = image.getCropRect();
final int width = crop.width();
final int height = crop.height();
final Image.Plane[] planes = image.getPlanes();
final byte[] rowData = new byte[planes[0].getRowStride()];
final int bufferSize = width * height * ImageFormat.getBitsPerPixel(ImageFormat.YUV_420_888) / 8;
final ByteBuffer output = ByteBuffer.allocateDirect(bufferSize);
int channelOffset = 0;
int outputStride = 0;
for (int planeIndex = 0; planeIndex < 3; planeIndex++)
{
if (planeIndex == 0)
{
channelOffset = 0;
outputStride = 1;
}
else if (planeIndex == 1)
{
channelOffset = width * height + 1;
outputStride = 2;
}
else if (planeIndex == 2)
{
channelOffset = width * height;
outputStride = 2;
}
final ByteBuffer buffer = planes[planeIndex].getBuffer();
final int rowStride = planes[planeIndex].getRowStride();
final int pixelStride = planes[planeIndex].getPixelStride();
final int shift = (planeIndex == 0) ? 0 : 1;
final int widthShifted = width >> shift;
final int heightShifted = height >> shift;
buffer.position(rowStride * (crop.top >> shift) + pixelStride * (crop.left >> shift));
for (int row = 0; row < heightShifted; row++)
{
final int length;
if (pixelStride == 1 && outputStride == 1)
{
length = widthShifted;
buffer.get(output.array(), channelOffset, length);
channelOffset += length;
}
else
{
length = (widthShifted - 1) * pixelStride + 1;
buffer.get(rowData, 0, length);
for (int col = 0; col < widthShifted; col++)
{
output.array()[channelOffset] = rowData[col * pixelStride];
channelOffset += outputStride;
}
}
if (row < heightShifted - 1)
{
buffer.position(buffer.position() + rowStride - length);
}
}
}
return output;
}
【讨论】:
-
-
-
请注意 ScriptIntrinsicYuvToRGB 对于相机预览帧是错误的,因为它假定 video BT.610 颜色空间(其中 Y 在范围内[16…235])。
【解决方案2】:
对于更简单的解决方案,请参阅我的实现:
Conversion YUV 420_888 to Bitmap (full code)
该函数将 media.image 作为输入,并基于 y、u 和 v 平面创建三个 RenderScript 分配。它遵循 Wikipedia 插图中所示的 YUV_420_888 逻辑。
然而,这里我们为 Y、U 和 V 通道提供了三个独立的图像平面,因此我将它们视为三个字节 [],即 U8 分配。 y 分配的大小为宽度 * 高度字节,而 u 和 v 分配的大小为宽度 * 高度/4 个字节,这反映了每个 u 字节覆盖 4 个像素的事实(每个 v 字节也是如此)。