以下代码解决了您的问题,并且由于YuvImage类是Android-SDK随附的,因此可能需要较少的时间处理Yuv格式数据。
你可以试试看
ByteArrayOutputStream out = new ByteArrayOutputStream();
YuvImage yuvImage = new YuvImage(data, ImageFormat.NV21, width, height, null);
yuvImage.compressToJpeg(new Rect(0, 0, width, height), 50, out);
byte[] imageBytes = out.toByteArray();
Bitmap image = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
iv.setImageBitmap(image);
要么
void yourFunction(byte[] data, int mWidth, int mHeight)
{
int[] mIntArray = new int[mWidth*mHeight];
// Decode Yuv data to integer array
decodeYUV420SP(mIntArray, data, mWidth, mHeight);
//Initialize the bitmap, with the replaced color
Bitmap bmp = Bitmap.createBitmap(mIntArray, mWidth, mHeight, Bitmap.Config.ARGB_8888);
// Draw the bitmap with the replaced color
iv.setImageBitmap(bmp);
}
static public void decodeYUV420SP(int[] rgba, byte[] yuv420sp, int width,
int height) {
final int frameSize = width * height;
for (int j = 0, yp = 0; j < height; j++) {
int uvp = frameSize + (j >> 1) * width, u = 0, v = 0;
for (int i = 0; i < width; i++, yp++) {
int y = (0xff & ((int) yuv420sp[yp])) - 16;
if (y < 0)
y = 0;
if ((i & 1) == 0) {
v = (0xff & yuv420sp[uvp++]) - 128;
u = (0xff & yuv420sp[uvp++]) - 128;
}
int y1192 = 1192 * y;
int r = (y1192 + 1634 * v);
int g = (y1192 - 833 * v - 400 * u);
int b = (y1192 + 2066 * u);
if (r < 0)
r = 0;
else if (r > 262143)
r = 262143;
if (g < 0)
g = 0;
else if (g > 262143)
g = 262143;
if (b < 0)
b = 0;
else if (b > 262143)
b = 262143;
// rgb[yp] = 0xff000000 | ((r << 6) & 0xff0000) | ((g >> 2) &
// 0xff00) | ((b >> 10) & 0xff);
// rgba, divide 2^10 ( >> 10)
rgba[yp] = ((r << 14) & 0xff000000) | ((g << 6) & 0xff0000)
| ((b >> 2) | 0xff00);
}
}
}
0
在我的应用程序中,我们需要显示从服务器接收到的视频帧到我们的android应用程序,
服务器已以每秒50帧的速度发送视频数据,并已在WebM中进行了编码,即使用libvpx编码和解码图像,
现在,从libvpx解码得到的YUV数据后,我们可以将其显示在图像布局上,
当前的实现是这样的,
在JNI /本机C ++代码中,我们正在将YUV数据转换为RGB数据。
要创建位图图像,
使用以下代码在imageView上显示图像,
我的查询是,总体来说,此功能大约需要40毫秒,是否有任何方法可以对其进行优化,
1-是否可以将YUV数据显示到imageView?
2-还有其他方法可以根据RGB数据创建Image(位图图像),
3-我相信我一直在创建图像,但是我想我应该只创建一次位图,并且总是/在收到消息时总是提供/提供新的缓冲区。
请分享您的看法。