一般用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進行音視頻混流便可