簡體   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