繁体   English   中英

将图像文件从RGB转换为YUV的有效方法

[英]Effective method to convert an image file from RGB to YUV

我正在使用Java开发一个Android应用程序项目。 我有一个图像,其扩展名为RGB颜色空间中的JPG或BMP。

我想将它从RGB转换为YUV。 我用谷歌搜索,发现我们可以通过使用使用公式的强力方法来实现它。 但是,速度太慢,因为我的应用程序是移动应用程序。 有没有其他更有效的方法来转换它?

这是你如何从RGB转换为YUV

我没有测试它有多快,但它应该工作正常

...

Bitmap b = ...
int bytes = b.getByteCount();
ByteBuffer buffer = ByteBuffer.allocate(bytes);
b.copyPixelsToBuffer(buffer); //Move the byte data to the buffer

byte[] data = buffer.array(); //Get the bytes array of the bitmap
YuvImage yuv = new YuvImage(data, ImageFormat.NV21, size.width, size.height, null);

然后使用YuvImage yuv做你想做的事。

这是如何从YUV转换为RGB

ByteArrayOutputStream out = new ByteArrayOutputStream();
YuvImage yuv = new YuvImage(data, ImageFormat.NV2,size.width, size.height, null);
//data is the byte array of your YUV image, that you want to convert
yuv.compressToJpeg(new Rect(0, 0, size.width,size.height), 100, out);
byte[] bytes = out.toByteArray();

Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM