简体   繁体   English

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

[英]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.

相关问题 当我将rgb位图转换为yuv时,会产生红色图像。 我想以YUV格式从图库中读取图像或将RGB位图转换为YUV - when I convert my rgb bitmap to yuv it results in a red image. I want to read image from gallery in YUV format or convert the RGB bitmap to YUV 使用OpenCV从Android Camera2将YUV转换为RGB ImageReader时出现问题,输出图像为灰度 - Problem converting YUV to RGB ImageReader from android Camera2 using OpenCV, output image is in grayscale 在Java中将图像从CMYK转换为RGB - Convert an image from CMYK to RGB in Java 将灰度图像转换为RGB - Convert grayscale image to RGB 如何在Java中将YCbCr(YUV)4:4:4图像转换为4:2:2? - How to convert a YCbCr(YUV) 4:4:4 image to 4:2:2 in java? 将 YUV 网络摄像头图像转换为 java 字节数组 - Convert YUV webcam image to java byte array 如何从URL中读取图像并将其从CMYK转换为RGB中的RGB? - how to read an image from a url and convert it from CMYK to RGB in java? 使用平面4:2:0 YUV全比例像素格式将图像从字节数组转换为位图 - Convert image from byte array to bitmap with planar 4:2:0 YUV full scale pixel format 将RGB数据的byte []转换为图像 - Convert byte[] of RGB data to a image 从格式为 YUV 的图像中获取中心像素 - Get center pixel from image with format YUV
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM