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

use*_*221 3 java android opencv bmp image-conversion

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

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

小智 6

这是你如何从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);
Run Code Online (Sandbox Code Playgroud)

然后使用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);
Run Code Online (Sandbox Code Playgroud)