zoukankan      html  css  js  c++  java
  • Android录制视频添加水印的高效方案之YUV帧数据覆盖

            上篇说过添加水印的方案,帧数据转为Bitmap,在bitmap上画水印,然后将加了水印的Bitmap转为帧数据

    这种方案可以实现水印添加,尽管用了RenderScript内联函数 增加了效率,但是因为多了帧数据到bitmap再到帧数据的转换过程,所以整体效率还是慢

    针对此问题,为了提高效率,我们可以直接在获取到的YUV帧数据上直接添加水印的YUV数据,具体步骤:

      1、提前将水印内容画在Bitmap上并将Bitmap转为YUV格式的字节数组

      2、获取到视频原始帧数据

      3、将水印的YUV数组里有效地水印部分对应赋值到原始帧数据相应位置

     

    1、水印内容画到Bitmap再转为字节数组 这里bitmap是黑底白字,之所以黑底是方便我们后边合成YUV帧数据根据颜色做判断

      

    private byte[] getOsdByte() {
            Bitmap bitmap=Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888)
            Canvas canvas = new Canvas(bitmap);
            canvas.drawColor(getResource.getColor(R.color.black));
            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            paint.setColor(Color.WHITE);
            paint.setTextSize(80);
            canvas.drawText("毕哥制作", CameraSettings.SRC_IMAGE_WIDTH/2, 100, paint);
            byte[] newBytes = bitmapToNv12(bitmapAllNew,CameraSettings.SRC_IMAGE_WIDTH, CameraSettings.SRC_IMAGE_HEIGHT);
            if(newBytes!=null){
                return newBytes;
            }else{
                return null;
            }
        }
    byte[] bitmapToNV12(int inputWidth, int inputHeight, Bitmap scaled) {
    
        int[] argb = new int[inputWidth * inputHeight];
    
        scaled.getPixels(argb, 0, inputWidth, 0, 0, inputWidth, inputHeight);
    
        byte[] yuv = new byte[inputWidth * inputHeight * 3 / 2];
        encodeYUV420SP(yuv, argb, inputWidth, inputHeight);
    
        scaled.recycle();
    
        return yuv;
    }
    
    public static void encodeYUV420SP(byte[] yuv420sp, int[] argb, int width, int height) {
        final int frameSize = width * height;
    
        int yIndex = 0;
        int uvIndex = frameSize;
    
        int a, R, G, B, Y, U, V;
        int index = 0;
        for (int j = 0; j < height; j++) {
            for (int i = 0; i < width; i++) {
    
                a = (argb[index] & 0xff000000) >> 24; // a is not used obviously
                R = (argb[index] & 0xff0000) >> 16;
                G = (argb[index] & 0xff00) >> 8;
                B = (argb[index] & 0xff) >> 0;
    
                // well known RGB to YUV algorithm
                Y = ( (  66 * R + 129 * G +  25 * B + 128) >> 8) +  16;
                U = ( ( -38 * R -  74 * G + 112 * B + 128) >> 8) + 128;
                V = ( ( 112 * R -  94 * G -  18 * B + 128) >> 8) + 128;
    
                // NV21 has a plane of Y and interleaved planes of VU each sampled by a factor of 2
                //    meaning for every 4 Y pixels there are 1 V and 1 U.  Note the sampling is every other
                //    pixel AND every other scanline.
                yuv420sp[yIndex++] = (byte) ((Y < 0) ? 0 : ((Y > 255) ? 255 : Y));
                if (j % 2 == 0 && index % 2 == 0) {
                    yuv420sp[uvIndex++] = (byte)((U<0) ? 0 : ((U > 255) ? 255 : U));
                    yuv420sp[uvIndex++] = (byte)((V<0) ? 0 : ((V > 255) ? 255 : V));
                }
    
                index ++;
            }
        }
    }

    2、获取到视频帧数据就不多说了 获取到的帧数据 该转颜色格式的转颜色格式 参考我之前的博客:https://www.cnblogs.com/bimingcong/p/14082860.html

    3、将水印的YUV数组里有效地水印部分对应赋值到原始帧数据相应位置

    //本方法是将NV12格式的水印数据的有效部分(即上边所说的黑底白字的白字部分)覆盖到原始NV12视频帧数据上

        将数组B对应的值覆盖到数组A  

    offset_x是B在A的X轴偏移量
    offset_y是B在A的Y轴偏移量
     //
        public static void mergeOsd(byte[] nv12_A, byte[] nv12_B, int offset_x, int offset_y, int a_width, int a_height, int b_width, int b_height) {
            for (int i = 0; i < b_height; i++) {
                for (int j = 0; j < b_width; j++) {
                    if(nv12_B[i * b_width + j] != 16){//如果不是黑色则将水印像素添加上去,黑色(#ff000000)就是黑底 值为16
                        nv12_A[i * a_width + offset_y * a_width + j + offset_x] = nv12_B[i * b_width + j];
                    }
                }
            }
        }

            这样数组A就是覆盖完有效水印的帧数据了  可以放入编码器编码了

  • 相关阅读:
    postman Variables变量的详解与应用
    windows 快速设置环境变量工具 Rapid Environment Editor
    Redis 客户端工具
    python 安装 pymongo
    python ImportError:No module named 'PIL'
    linux 通过命令行终端去控制vnc终端【export DISPLAY使用方法】
    centos7 安装vnc-server 与卸载
    vagrant box centos7硬盘扩容【不删除原数据】
    vboxmanage不是内部或外部命令
    用docker搭建的nginx报upstream错误
  • 原文地址:https://www.cnblogs.com/bimingcong/p/14170429.html
Copyright © 2011-2022 走看看