NV21数据处理——实现剪裁,叠图

关于NV21格式数据不了解的同学可自行搜索,网上资料很多,这里不做过多阐述。

NV21数据剪裁

剪裁的本质就是在原nv21数据上截取我们需要的进行二次封装即可。


剪裁.png
    /**
     * nv21数据剪裁
     *
     * @param src        原始nv21数据
     * @param srcWidth   原始nv21数据的宽
     * @param srcHeight  原始nv21数据的高
     * @param clipWidth  剪裁的宽度
     * @param clipHeight 剪裁的高度
     * @param left       剪裁的开始的左边位置,坐标相对于nv21原始数据
     * @param top        剪裁的开始的上边位置,坐标相对于nv21原始数据
     * @return 剪裁异常时返回null,成功时返回剪裁的nv21数据
     */
    @Nullable
    public static byte[] cropNV21(@NonNull byte[] src, int srcWidth, int srcHeight, int clipWidth, int clipHeight, int left, int top) {
        if (src.length != srcWidth * srcHeight * 3 / 2) {
            return null;
        }
        if (clipWidth + left > srcWidth || clipHeight + top > srcHeight) {
            return null;
        }
        if (clipWidth == srcWidth && clipHeight == srcHeight && left == 0 && top == 0) {
            return src;
        }
        //确保为偶数
        clipWidth &= ~1;
        clipHeight &= ~1;
        left &= ~1;
        top &= ~1;
        byte[] cropBytes = new byte[clipWidth * clipHeight * 3 / 2];
        int bottom = top + clipHeight;
        //先复制Y数据
        for (int i = top; i < bottom; i++) {
            System.arraycopy(src, left + i * srcWidth, cropBytes, (i - top) * clipWidth, clipWidth);
        }
        //复制UV数据
        int startH = srcHeight + top / 2;
        int endH = srcHeight + bottom / 2;
        for (int i = startH; i < endH; i++) {
            System.arraycopy(src,
                    left + i * srcWidth,
                    cropBytes,
                    (i - startH + clipHeight) * clipWidth,
                    clipWidth);
        }
        return cropBytes;
    }

NV21数据叠图

叠图的思路就是把原nv21中对应位置、大小的byte数据替换成要叠在上面的nv21数据即可。

  • 贴图中无透明像素点的情况


    叠图.png
    /**
     * 叠图
     * 调用完成后改方法后,直接使用传入的nv21 数据即可。
     *
     * @param nv21          叠图最下面的图的nv21数据,大小要比被叠图的nv21数据大
     * @param width         最下面叠图的nv21数据的宽
     * @param height        最下面叠图的nv21数据的高
     * @param left          叠图起始左边位置
     * @param top           叠图起始的上边位置
     * @param overlayNv21   小图的nv21数据
     * @param overlayWidth  小图的宽
     * @param overlayHeight 小图的高
     */
    public static void overlayNV21(byte[] nv21, int width, int height, int left, int top, byte[] overlayNv21, int overlayWidth, int overlayHeight) {
        if (nv21.length != width * height * 3 / 2) {
            return;
        }
        if (overlayNv21.length != overlayWidth * overlayHeight * 3 / 2) {
            return;
        }
        int originalOverlayWidth = overlayWidth;
        int originalOverlayHeight = overlayHeight;
        if (overlayWidth + left > width) {
            //不符合要求,进行二次剪裁
            overlayWidth = width - left;
        }
        if (overlayHeight + top > height) {
            //不符合要求,进行二次剪裁
            overlayHeight = height - top;
        }
        //确保为偶数
        left &= ~1;
        top &= ~1;
        overlayWidth &= ~1;
        overlayHeight &= ~1;

        //裁剪
        overlayNv21 = cropNV21(overlayNv21, originalOverlayWidth, originalOverlayHeight, overlayWidth, overlayHeight, 0, 0);
        if (overlayNv21 == null) {
            return;
        }

        //先复制Y数据
        for (int i = 0; i < overlayHeight; i++) {
            System.arraycopy(overlayNv21, i * overlayWidth, nv21, left + (top + i) * width, overlayWidth);
        }
        //复制UV数据
        int startH = overlayHeight;
        int endH = overlayHeight + (overlayWidth * overlayHeight / 2) / overlayWidth;

        int basic = height + top / 2;
        for (int i = startH; i < endH; i++) {
            System.arraycopy(overlayNv21,
                    i * overlayWidth,
                    nv21,
                    left + (basic + (i - startH)) * width,
                    overlayWidth);
        }
    }
  • 贴图中有透明像素点的情况
    这种情况适用于图片中添加水印的情况,我们再使用上面的方法来试验一下


    有透明像素点.png

可以看到透明的部分变成了纯黑色,这很显然和我们的预期不一致。所以要换个方法,既然叠图的本质就是换数据,那我们是不是只要过滤到透明像素点的数据不就行了。
直接上结果,我测试的是6x6 "♡"型图片,用小像素的图片,对比nv21数据。有兴趣的也可以自己测试。

透明nv21

最终结果:透明Y数据是16,透明UV数据是-128。
注意:看颜色区分,4个Y共用一组UV。
下面我们就在处理数据时跳过透明Y、UV数据即可。
直接上代码(处理透明数据会耗时):

    /**
     * 透明Y值
     */
    public static final byte TRANSPARENT_Y = 0x10;
    /**
     * 透明UV值
     */
    public static final byte TRANSPARENT_UV = (byte) 0x80;
   /**
     * 叠图
     * 调用完成后改方法后,直接使用传入的nv21 数据即可。
     *
     * @param nv21          叠图最下面的图的nv21数据,大小要比被叠图的nv21数据大
     * @param width         最下面叠图的nv21数据的宽
     * @param height        最下面叠图的nv21数据的高
     * @param left          叠图起始左边位置
     * @param top           叠图起始的上边位置
     * @param overlayNv21   小图的nv21数据
     * @param overlayWidth  小图的宽
     * @param overlayHeight 小图的高
     * @param transparent   叠图中是否有透明数据;如果有,但传参false的话,会以黑色填充;如果是true的话,会比较耗时
     */
    public static void overlayNV21(byte[] nv21, int width, int height, int left, int top, byte[] overlayNv21, int overlayWidth, int overlayHeight, boolean transparent) {
        if (nv21.length != width * height * 3 / 2) {
            return;
        }
        if (overlayNv21.length != overlayWidth * overlayHeight * 3 / 2) {
            return;
        }
        int originalOverlayWidth = overlayWidth;
        int originalOverlayHeight = overlayHeight;
        if (overlayWidth + left > width) {
            //不符合要求,进行二次剪裁
            overlayWidth = width - left;
        }
        if (overlayHeight + top > height) {
            //不符合要求,进行二次剪裁
            overlayHeight = height - top;
        }
        //确保为偶数
        left &= ~1;
        top &= ~1;
        overlayWidth &= ~1;
        overlayHeight &= ~1;

        //裁剪
        overlayNv21 = cropNV21(overlayNv21, originalOverlayWidth, originalOverlayHeight, overlayWidth, overlayHeight, 0, 0);
        if (overlayNv21 == null) {
            return;
        }
        if (transparent) {
            //途中有透明部分
            //先剪裁出nv21中覆盖部分的相同位置的数据
            byte[] cropNV21 = cropNV21(nv21, width, height, overlayWidth, overlayHeight, left, top);
            if (cropNV21 == null) {
                return;
            }
            int size = overlayWidth * overlayHeight * 3 / 2;
            //然后合并
            if (cropNV21.length != size || overlayNv21.length != size) {
                return;
            }

            int splitY = overlayWidth * overlayHeight;
            for (int i = 0; i < size; i++) {
                if (i < splitY) {
                    //Y数据
                    if (overlayNv21[i] != TRANSPARENT_Y) {
                        cropNV21[i] = overlayNv21[i];
                    }
                } else {
                    //UV数据
                    if (overlayNv21[i] != TRANSPARENT_UV) {
                        cropNV21[i] = overlayNv21[i];
                    }
                }
            }
            overlayNv21 = cropNV21;
        }

        //先复制Y数据
        for (int i = 0; i < overlayHeight; i++) {
            System.arraycopy(overlayNv21, i * overlayWidth, nv21, left + (top + i) * width, overlayWidth);
        }
        //复制UV数据
        int startH = overlayHeight;
        int endH = overlayHeight + (overlayWidth * overlayHeight / 2) / overlayWidth;

        int basic = height + top / 2;
        for (int i = startH; i < endH; i++) {
            System.arraycopy(overlayNv21,
                    i * overlayWidth,
                    nv21,
                    left + (basic + (i - startH)) * width,
                    overlayWidth);
        }
    }

结果就是这样,周围有一圈黑边,我的怀疑是4个Y数据对应一组UV,透明UV需要4个Y数据必须全是透明的才行,这可能是问题原因。还望清楚伙伴在下面评论告知一下。


透明图片

供上全部代码:

public class NV21Util {
    /**
     * 透明Y值
     */
    public static final byte TRANSPARENT_Y = 0x10;
    /**
     * 透明UV值
     */
    public static final byte TRANSPARENT_UV = (byte) 0x80;

    /**
     * nv21数据剪裁
     *
     * @param src        原始nv21数据
     * @param srcWidth   原始nv21数据的宽
     * @param srcHeight  原始nv21数据的高
     * @param clipWidth  剪裁的宽度
     * @param clipHeight 剪裁的高度
     * @param left       剪裁的开始的左边位置,坐标相对于nv21原始数据
     * @param top        剪裁的开始的上边位置,坐标相对于nv21原始数据
     * @return 剪裁异常时返回null,成功时返回剪裁的nv21数据
     */
    @Nullable
    public static byte[] cropNV21(@NonNull byte[] src, int srcWidth, int srcHeight, int clipWidth, int clipHeight, int left, int top) {
        if (src.length != srcWidth * srcHeight * 3 / 2) {
            return null;
        }
        if (clipWidth + left > srcWidth || clipHeight + top > srcHeight) {
            return null;
        }
        if (clipWidth == srcWidth && clipHeight == srcHeight && left == 0 && top == 0) {
            return src;
        }
        //确保为偶数
        clipWidth &= ~1;
        clipHeight &= ~1;
        left &= ~1;
        top &= ~1;
        byte[] cropBytes = new byte[clipWidth * clipHeight * 3 / 2];
        int bottom = top + clipHeight;
        //先复制Y数据
        for (int i = top; i < bottom; i++) {
            System.arraycopy(src, left + i * srcWidth, cropBytes, (i - top) * clipWidth, clipWidth);
        }
        //复制UV数据
        int startH = srcHeight + top / 2;
        int endH = srcHeight + bottom / 2;
        for (int i = startH; i < endH; i++) {
            System.arraycopy(src,
                    left + i * srcWidth,
                    cropBytes,
                    (i - startH + clipHeight) * clipWidth,
                    clipWidth);
        }
        return cropBytes;
    }

    /**
     * 叠图
     * 调用完成后改方法后,直接使用传入的nv21 数据即可。
     *
     * @param nv21          叠图最下面的图的nv21数据,大小要比被叠图的nv21数据大
     * @param width         最下面叠图的nv21数据的宽
     * @param height        最下面叠图的nv21数据的高
     * @param left          叠图起始左边位置
     * @param top           叠图起始的上边位置
     * @param overlayNv21   小图的nv21数据
     * @param overlayWidth  小图的宽
     * @param overlayHeight 小图的高
     */
    public static void overlayNV21(byte[] nv21, int width, int height, int left, int top, byte[] overlayNv21, int overlayWidth, int overlayHeight) {
        overlayNV21(nv21, width, height, left, top, overlayNv21, overlayWidth, overlayHeight, false);
    }

    /**
     * 叠图
     * 调用完成后改方法后,直接使用传入的nv21 数据即可。
     *
     * @param nv21          叠图最下面的图的nv21数据,大小要比被叠图的nv21数据大
     * @param width         最下面叠图的nv21数据的宽
     * @param height        最下面叠图的nv21数据的高
     * @param left          叠图起始左边位置
     * @param top           叠图起始的上边位置
     * @param overlayNv21   小图的nv21数据
     * @param overlayWidth  小图的宽
     * @param overlayHeight 小图的高
     * @param transparent   叠图中是否有透明数据;如果有,但传参false的话,会以黑色填充;如果是true的话,会比较耗时
     */
    public static void overlayNV21(byte[] nv21, int width, int height, int left, int top, byte[] overlayNv21, int overlayWidth, int overlayHeight, boolean transparent) {
        if (nv21.length != width * height * 3 / 2) {
            return;
        }
        if (overlayNv21.length != overlayWidth * overlayHeight * 3 / 2) {
            return;
        }
        int originalOverlayWidth = overlayWidth;
        int originalOverlayHeight = overlayHeight;
        if (overlayWidth + left > width) {
            //不符合要求,进行二次剪裁
            overlayWidth = width - left;
        }
        if (overlayHeight + top > height) {
            //不符合要求,进行二次剪裁
            overlayHeight = height - top;
        }
        //确保为偶数
        left &= ~1;
        top &= ~1;
        overlayWidth &= ~1;
        overlayHeight &= ~1;

        //裁剪
        overlayNv21 = cropNV21(overlayNv21, originalOverlayWidth, originalOverlayHeight, overlayWidth, overlayHeight, 0, 0);
        if (overlayNv21 == null) {
            return;
        }
        if (transparent) {
            //途中有透明部分
            //先剪裁出nv21中覆盖部分的相同位置的数据
            byte[] cropNV21 = cropNV21(nv21, width, height, overlayWidth, overlayHeight, left, top);
            if (cropNV21 == null) {
                return;
            }
            int size = overlayWidth * overlayHeight * 3 / 2;
            //然后合并
            if (cropNV21.length != size || overlayNv21.length != size) {
                return;
            }

            int splitY = overlayWidth * overlayHeight;
            for (int i = 0; i < size; i++) {
                if (i < splitY) {
                    //Y数据
                    if (overlayNv21[i] != TRANSPARENT_Y) {
                        cropNV21[i] = overlayNv21[i];
                    }
                } else {
                    //UV数据
                    if (overlayNv21[i] != TRANSPARENT_UV) {
                        cropNV21[i] = overlayNv21[i];
                    }
                }
            }
            overlayNv21 = cropNV21;
        }

        //先复制Y数据
        for (int i = 0; i < overlayHeight; i++) {
            System.arraycopy(overlayNv21, i * overlayWidth, nv21, left + (top + i) * width, overlayWidth);
        }
        //复制UV数据
        int startH = overlayHeight;
        int endH = overlayHeight + (overlayWidth * overlayHeight / 2) / overlayWidth;

        int basic = height + top / 2;
        for (int i = startH; i < endH; i++) {
            System.arraycopy(overlayNv21,
                    i * overlayWidth,
                    nv21,
                    left + (basic + (i - startH)) * width,
                    overlayWidth);
        }
    }

}

如有错误或建议,欢迎指正!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,524评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,869评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,813评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,210评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,085评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,117评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,533评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,219评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,487评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,582评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,362评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,218评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,589评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,899评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,176评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,503评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,707评论 2 335

推荐阅读更多精彩内容