Java YUV图像四宫格拼接

这段代码主要实现了YUV格式的图像进行四宫格横向和纵向拼接,以及NV21到NV12的转换。`horizontalMontage`函数用于横屏拼接,`verticalMontage`函数用于竖屏拼接,`NV21ToNV12`则用于YUV格式的转换。
摘要由CSDN通过智能技术生成
//四宫格拼接
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];
        }
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值