Android錄製視頻添加水印的高效方案之YUV幀數據覆蓋

        上篇說過添加水印的方案,幀數據轉爲Bitmap,在bitmap上畫水印,而後將加了水印的Bitmap轉爲幀數據html

這種方案能夠實現水印添加,儘管用了RenderScript內聯函數 增長了效率,可是由於多了幀數據到bitmap再到幀數據的轉換過程,因此總體效率仍是慢canvas

針對此問題,爲了提升效率,咱們能夠直接在獲取到的YUV幀數據上直接添加水印的YUV數據,具體步驟:數組

  一、提早將水印內容畫在Bitmap上並將Bitmap轉爲YUV格式的字節數組函數

  二、獲取到視頻原始幀數據編碼

  三、將水印的YUV數組裏有效地水印部分對應賦值到原始幀數據相應位置spa

 

一、水印內容畫到Bitmap再轉爲字節數組 這裏bitmap是黑底白字,之因此黑底是方便咱們後邊合成YUV幀數據根據顏色作判斷code

  

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 ++;
        }
    }
}

二、獲取到視頻幀數據就很少說了 獲取到的幀數據 該轉顏色格式的轉顏色格式 參考我以前的博客:http://www.javashuo.com/article/p-khkeoiyt-nv.html視頻

三、將水印的YUV數組裏有效地水印部分對應賦值到原始幀數據相應位置htm

//本方法是將NV12格式的水印數據的有效部分(即上邊所說的黑底白字的白字部分)覆蓋到原始NV12視頻幀數據上

    將數組B對應的值覆蓋到數組A  blog

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就是覆蓋完有效水印的幀數據了  能夠放入編碼器編碼了

相關文章
相關標籤/搜索