[英]Effective method to convert an image file from RGB to YUV
I am developing an Android application project using Java. 我正在使用Java开发一个Android应用程序项目。 I have an image whose extension is JPG or BMP in RGB color space. 我有一个图像,其扩展名为RGB颜色空间中的JPG或BMP。
I want to convert it from RGB to YUV. 我想将它从RGB转换为YUV。 I have googled and found that we can achieve it by using the brute force method of using a formula. 我用谷歌搜索,发现我们可以通过使用使用公式的强力方法来实现它。 However, speed is too slow as my application is a mobile application. 但是,速度太慢,因为我的应用程序是移动应用程序。 Is there any other more effective method to convert it? 有没有其他更有效的方法来转换它?
This is how you convert from RGB to YUV 这是你如何从RGB转换为YUV
I didn't test how quick this is, but it should work fine 我没有测试它有多快,但它应该工作正常
...
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);
Then do what you want with the YuvImage yuv. 然后使用YuvImage yuv做你想做的事。
And this is how to convert from YUV to RGB 这是如何从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.