本文是针对NV21格式图片进行横/竖屏拼图
NV21:YVU排列,vu交错排列,vu高度是y的一半,y占得字节数是width*height,剩下的字节数都是vu
为了更形象的展示,以4*4图片为例子:
竖屏拼图后是
横屏拼图后是
竖屏拼图
首先进行全部Y分量的交错填充,然后全部VU分量交错填充
横屏拼图
先是Y分量一行一行拼接,然后再vu一行一行拼接
具体代码如下:
private int width=640;
private int height=480;
private byte[] dataIr_y=new byte[width*height];
private byte[] dataRgb_y=new byte[width*height];
private byte[] dataIr_vu=new byte[width*height/2];
private byte[] dataRgb_vu=new byte[width*height/2];
private byte[] mergeData=new byte[width*height*3];
//竖屏拼接
private byte[] concat(byte[] data1, byte[] data2) {
System.arraycopy(data1, 0, dataIr_y, 0, dataIr_y.length);
System.arraycopy(data2, 0, dataRgb_y, 0, dataRgb_y.length);
System.arraycopy(data1,dataIr_y.length,dataIr_vu,0,dataIr_vu.length);
System.arraycopy(data2,dataRgb_y.length,dataRgb_vu,0,dataRgb_vu.length);
//y拼接
System.arraycopy(dataIr_y, 0, mergeData, 0, dataIr_y.length);
System.arraycopy(dataRgb_y,0, mergeData, dataRgb_y.length, dataRgb_y.length);
//vu拼接
System.arraycopy(dataIr_vu, 0, mergeData, dataIr_y.length+dataRgb_y.length, dataIr_vu.length);
System.arraycopy(dataRgb_vu, 0, mergeData, dataIr_y.length+data1.length, dataRgb_vu.length);
return mergeData;
}
//横屏拼图代码
private byte[] concat(byte[] data1, byte[] data2) {
System.arraycopy(data1, 0, dataIr_y, 0, dataIr_y.length);
System.arraycopy(data2, 0, dataRgb_y, 0, dataRgb_y.length);
System.arraycopy(data1, dataIr_y.length, dataIr_vu, 0, data1.length - dataIr_y.length);
System.arraycopy(data2, dataRgb_y.length, dataRgb_vu, 0, data2.length - dataRgb_y.length);
//y分量一行一行拼接
int y = 0;
for (int i = 0; i < height; i++) {
System.arraycopy(dataIr_y, width * i, mergeData, width * y, width);
y++;
System.arraycopy(dataRgb_y, width * i, mergeData, width * y, width);
y++;
}
//vu分量一行一行拼接
int z = 0;
for (int i = 0; i < height / 2; i++) {
System.arraycopy(dataIr_vu, width * i, mergeData, width * height * 2 + z * width, width);
z++;
System.arraycopy(dataRgb_vu, width * i, mergeData, width * height * 2 + z * width, width);
z++;
}
return mergeData;
}