Android Camera录制视频添加水印

一般用Camera 采集视频 获得预览数据,使用mediaCodec获取视频数据,用mediaMuxer进行音视频的混流,java

若是想要添加水印很简单:canvas

一、拿到相机预览的帧数据函数

二、将帧数据转为Bitmapui

三、在Bitmap上添加水印(文字或者图片)编码

四、将图片转为帧数据spa

而后继续混流,效果以下:3d

 

拿相机预览的数据很简单:code

 

 关键在第二步,帧数据转为bitmap常规是这样作的  可是这种作法很耗时会致使视频卡顿:orm

YuvImage image = new YuvImage(dst, ImageFormat.NV21, CameraSettings.SRC_IMAGE_WIDTH,CameraSettings.SRC_IMAGE_HEIGHT, null)
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        image.compressToJpeg(new Rect(0, 0, CameraSettings.SRC_IMAGE_WIDTH,CameraSettings.SRC_IMAGE_HEIGHT), 100, stream);
        Bitmap bitmapAll = BitmapFactory.decodeByteArray(stream.toByteArray(), 0, stream.size());

  比较好的作法是使用 RenderScript的内联函数 能够更加高效的将帧数据转为bitmap:视频

public class MyClass {
    private RenderScript rs;
    private ScriptIntrinsicYuvToRGB yuvToRgbIntrinsic;
    private Type.Builder yuvType, rgbaType;
    private Allocation in, out;
    public MyClass(Context context) {
        rs = RenderScript.create(context);
        yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));
    }
    public Bitmap nv21ToBitmap(byte[] nv21, int width, int height){
        if (yuvType == null){
            yuvType = new Type.Builder(rs, Element.U8(rs)).setX(nv21.length);
            in = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT);
            rgbaType = new Type.Builder(rs, Element.RGBA_8888(rs)).setX(width).setY(height);
            out = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT);
        }
        in.copyFrom(nv21);
        yuvToRgbIntrinsic.setInput(in);
        yuvToRgbIntrinsic.forEach(out);
        Bitmap bmpout = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        out.copyTo(bmpout);
        return bmpout;
    }
}

  因此第二步、第三步能够这样使用:

 /**
     * 将拿到的预览帧数据转为bitmap添加水印 再讲bitmap转为帧数据
     * @param dst 预览的帧数据
     * @return
     */
    private byte[] dealByte(byte[] dst) {
//        YuvImage image = new YuvImage(dst, ImageFormat.NV21, CameraSettings.SRC_IMAGE_WIDTH,CameraSettings.SRC_IMAGE_HEIGHT, null)
//        ByteArrayOutputStream stream = new ByteArrayOutputStream();
//        image.compressToJpeg(new Rect(0, 0, CameraSettings.SRC_IMAGE_WIDTH,CameraSettings.SRC_IMAGE_HEIGHT), 100, stream);
//        Bitmap bitmapAll = BitmapFactory.decodeByteArray(stream.toByteArray(), 0, stream.size());

        Bitmap bitmapAll = myClass.nv21ToBitmap(dst, CameraSettings.SRC_IMAGE_WIDTH,
                CameraSettings.SRC_IMAGE_HEIGHT);

        Bitmap bitmapAllNew=bitmapAll.copy(Bitmap.Config.ARGB_8888,true);
        Canvas canvas = new Canvas(bitmapAllNew);
        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 = bitmapToNv21(bitmapAllNew,CameraSettings.SRC_IMAGE_WIDTH, CameraSettings.SRC_IMAGE_HEIGHT);
        if(newBytes!=null){
            return newBytes;
        }else{
            return null;
        }
    }

  bitmap转为帧数据

 public static byte[] bitmapToNv21(Bitmap src, int width, int height) {
        if (src != null && src.getWidth() >= width && src.getHeight() >= height) {
            int[] argb = new int[width * height];
            src.getPixels(argb, 0, width, 0, 0, width, height);
            return argbToNv21(argb, width, height);
        } else {
            return null;
        }
    }

  最后再将帧数据 经过mediaCodec编码,再用mediaMuxer进行音视频混流便可

相关文章
相关标签/搜索