我正在使用 Java 开发一个 Android 应用程序项目。我有一个图像,其扩展名为 RGB 颜色空间中的 JPG 或 BMP。
我想将它从 RGB 转换为 YUV。我google了一下,发现我们可以通过使用公式的蛮力法来实现。但是,由于我的应用程序是移动应用程序,因此速度太慢了。有没有其他更有效的转换方法?
最佳答案
这就是您将 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);
关于java - 将图像文件从 RGB 转换为 YUV 的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24077140/