我正在使用Java开发一个Android应用程序项目。 我有一张图像,其扩展名为JPG或BMP,颜色空间为RGB。
我希望将其从RGB转换为YUV。 我已经搜索过并发现我们可以通过使用公式的暴力方法来实现它。 然而,由于我的应用程序是移动应用程序,速度太慢了。 有没有其他更有效的方法来转换它?
我正在使用Java开发一个Android应用程序项目。 我有一张图像,其扩展名为JPG或BMP,颜色空间为RGB。
我希望将其从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);