//四宫格拼接
byte[] dataH1 = YUVUtil.horizontalMontage(data, data, mWidth, mHeight);
byte[] dataH2 = YUVUtil.horizontalMontage(data, data, mWidth, mHeight);
byte[] data4 = YUVUtil.verticalMontage(dataH1, dataH2, mWidth * 2, mHeight);
//NV12 大小为源3/2 * 4
mRotatedYUVBuffer = new byte[this.mWidth * this.mHeight * 6];
YUVUtil.NV21ToNV12(data4, mRotatedYUVBuffer, mWidth, mHeight);
====================================================================================
/**
* YUV竖屏拼接
*
* @param data1
* @param data2
* @param width 输入的宽度
* @param height 输入的高度
* @return
*/
public static byte[] verticalMontage(byte[] data1, byte[] data2, int width, int height) {
if (data1 == null || data2 == null) {
return null;
}
byte[] data1_y = new byte[width * height];
byte[] data2_y = new byte[width * height];
byte[] data1_vu = new byte[width * height / 2];
byte[] data2_vu = new byte[width * height / 2];
byte[] mergeData = new byte[width * height * 3];
System.arraycopy(data1, 0, data1_y, 0, data1_y.length);
System.arraycopy(data2, 0, data2_y, 0, data2_y.length);
System.arraycopy(data1, data1_y.length, data1_vu, 0, data1_vu.length);
System.arraycopy(data2, data2_y.length, data2_vu, 0, data2_vu.length);
//y拼接
System.arraycopy(data1_y, 0, mergeData, 0, data1_y.length);//1
System.arraycopy(data2_y, 0, mergeData, data1_y.length, data2_y.length);//2
//vu拼接
System.arraycopy(data1_vu, 0, mergeData, (data1_y.length + data2_y.length), data1_vu.length);//2.5
System.arraycopy(data2_vu, 0, mergeData, (data1_y.length + data2_y.length + data1_vu.length), data2_vu.length);//3
return mergeData;
}
/**
* YUV横屏拼接
*
* @param data1
* @param data2
* @param width 输入的宽度
* @param height 输入的高度
* @return
*/
public static byte[] horizontalMontage(byte[] data1, byte[] data2, int width, int height) {
if (data1 == null || data2 == null) {
return null;
}
byte[] data1_y = new byte[width * height];
byte[] data2_y = new byte[width * height];
byte[] data1_vu = new byte[width * height / 2];
byte[] data2_vu = new byte[width * height / 2];
byte[] mergeData = new byte[width * height * 3];
System.arraycopy(data1, 0, data1_y, 0, data1_y.length);
System.arraycopy(data2, 0, data2_y, 0, data2_y.length);
System.arraycopy(data1, data1_y.length, data1_vu, 0, data1_vu.length);
System.arraycopy(data2, data2_y.length, data2_vu, 0, data2_vu.length);
//y分量一行一行拼接
int y = 0;
for (int i = 0; i < height; i++) {
System.arraycopy(data1_y, width * i, mergeData, width * y, width);
y++;
System.arraycopy(data2_y, width * i, mergeData, width * y, width);
y++;
}
//vu分量一行一行拼接
int z = 0;
for (int i = 0; i < height / 2; i++) {
System.arraycopy(data1_vu, width * i, mergeData, width * height * 2 + z * width, width);
z++;
System.arraycopy(data2_vu, width * i, mergeData, width * height * 2 + z * width, width);
z++;
}
return mergeData;
}
/**
* YUV转换
*
* @param nv21 源
* @param nv12 输出
* @param width
* @param height
*/
public static void NV21ToNV12(byte[] nv21, byte[] nv12, int width, int height) {
if (nv21 == null || nv12 == null) {
return;
}
int frameSize = width * height * 4;
int i = 0, j = 0;
System.arraycopy(nv21, 0, nv12, 0, width * height);
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];
}
}
Java YUV图像四宫格拼接
于 2023-05-08 16:48:37 首次发布
这段代码主要实现了YUV格式的图像进行四宫格横向和纵向拼接,以及NV21到NV12的转换。`horizontalMontage`函数用于横屏拼接,`verticalMontage`函数用于竖屏拼接,`NV21ToNV12`则用于YUV格式的转换。
摘要由CSDN通过智能技术生成