NV21的byte数据用这个方法转成bitmap后显示正常:
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
但是用YuvImage的方法转换后就有问题不知道什么原因
效果图和代码如下:
YuvImage yuvImage = new YuvImage(data, ImageFormat.NV21, width, height, null);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
yuvImage.compressToJpeg(new Rect(0, 0, width, height), 100, outputStream);
byte[] jpeg = outputStream.toByteArray();
Bitmap bitmap = BitmapFactory.decodeByteArray(jpeg, 0, jpeg.length);
然后我又用了另外一种方法转成NV12格式后再转成bitmap后显示的图像只有一半,另一半是灰色,不知道到底是咋了
byte[] data12 = NV21ToNV12(data,1920, 1080);
bitmap = BitmapFactory.decodeByteArray(data12, 0, data12.length);
private byte[] NV21ToNV12(byte[] nv21, int width, int height) {
if (nv21 == null) {
return null;
}
;
int size = nv21.length;
byte[] nv12 = new byte[size];
int framesize = size * 2 / 3;
int i = 0, j = 0;
System.arraycopy(nv21, 0, nv12, 0, framesize);
for (i = 0; i < framesize; i++) {
nv12[i] = nv21[i];
}
for (j = 0; j < framesize / 2; j += 2) {
nv12[framesize + j - 1] = nv21[j + framesize];
}
for (j = 0; j < framesize / 2; j += 2) {
nv12[framesize + j] = nv21[j + framesize - 1];
}
return nv12;
}
请教各位大V,以上代码有什么问题,出现这些问题的原因是什么,该如何解决