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

这篇博客介绍了如何解决在将Android的YUV_420_888格式图像转换为JPEG并保存时出现图像失真的问题。通过提供一个调整后的NV21toJPEG方法和YUV420toNV21方法,作者详细解释了转换过程,并考虑到了图像平面的行间距,以确保转换正确无误。
摘要由CSDN通过智能技术生成

@ 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];<

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值