android yuvimage 格式,android – 将YUV_420_888转换为JPEG并保存文件结果失真的图像

@ volodymyr-kulyk提供的解决方案没有考虑图像中平面的行间距.下面的代码诀窍(图像是android.media.Image类型):

data = NV21toJPEG(YUV420toNV21(image), image.getWidth(), image.getHeight(), 100);

实施:

private static byte[] NV21toJPEG(byte[] nv21, int width, int height, int quality) {

ByteArrayOutputStream out = new ByteArrayOutputStream();

YuvImage yuv = new YuvImage(nv21, ImageFormat.NV21, width, height, null);

yuv.compressToJpeg(new Rect(0, 0, width, height), quality, out);

return out.toByteArray();

}

private static byte[] YUV420toNV21(Image image) {

Rect crop = image.getCropRect();

int format = image.getFormat();

int width = crop.width();

int height = crop.height();

Image.Plane[] planes = image.getPlanes();

byte[] data = new byte[width * height * ImageFormat.getBitsPerPixel(format) / 8];<

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,针对您的问题,我可以提供以下的参考代码,可以将 Android camera2 api YUV_420_888 格式的数据转换为 RGB 格式的数据: ``` private void convertYUV420ToRGB(Image imgYUV, Bitmap imgRGB) { // Get image width and height int width = imgYUV.getWidth(); int height = imgYUV.getHeight(); // Get Y, U, and V image planes from image Image.Plane[] planes = imgYUV.getPlanes(); ByteBuffer bufferY = planes[0].getBuffer(); ByteBuffer bufferU = planes[1].getBuffer(); ByteBuffer bufferV = planes[2].getBuffer(); // Calculate pixel stride and row stride for Y, U, and V image planes int pixelStrideY = planes[0].getPixelStride(); int rowStrideY = planes[0].getRowStride(); int pixelStrideU = planes[1].getPixelStride(); int rowStrideU = planes[1].getRowStride(); int pixelStrideV = planes[2].getPixelStride(); int rowStrideV = planes[2].getRowStride(); // Create arrays for storing pixel data byte[] dataY = new byte[width * height * pixelStrideY]; byte[] dataU = new byte[width * height * pixelStrideU / 4]; byte[] dataV = new byte[width * height * pixelStrideV / 4]; // Copy pixel data from image planes into arrays bufferY.get(dataY, 0, width * height * pixelStrideY); bufferU.get(dataU, 0, width * height * pixelStrideU / 4); bufferV.get(dataV, 0, width * height * pixelStrideV / 4); // Initialize RGB pixel buffer int[] pixels = new int[width * height]; int offset = 0; // Loop through each row and column of pixels for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { // Get YUV pixel values for current pixel int Y = dataY[offset] & 0xff; int U = dataU[offset / 4] & 0xff; int V = dataV[offset / 4] & 0xff; // Calculate RGB pixel values for current pixel int R = (int)(Y + 1.370705f * (V - 128)); int G = (int)(Y - 0.698001f * (V - 128) - 0.337633f * (U - 128)); int B = (int)(Y + 1.732446f * (U - 128)); // Clip RGB pixel values to range [0, 255] R = Math.max(0, Math.min(255, R)); G = Math.max(0, Math.min(255, G)); B = Math.max(0, Math.min(255, B)); // Combine RGB pixel values into single pixel pixels[offset++] = (0xff << 24) | (R << 16) | (G << 8) | B; } // Move to next row of pixels offset += rowStrideY - width * pixelStrideY; } // Set RGB pixel data into Bitmap imgRGB.setPixels(pixels, 0, width, 0, 0, width, height); } ``` 这个方法将会把 Image 对象 imgYUV 中的 YUV 数据转换为 RGB 数据,并且将其存储到 Bitmap 对象 imgRGB 中。其中,YUV 数据的格式YUV_420_888,RGB 数据的格式为 ARGB_8888。 注意,这个方法中的转换公式是基于 YUV420格式,如果您的 YUV 数据格式不同,需要根据实际情况对转换公式进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值