/**
* @Desc: 水印的处理
* @Created by: 2020/3/27 17:19
**/
public class WaterMarkUtils {
private static final String TAG = "WaterMarkUtils";
private static final int DURATION_TAILWATERMARK = 3;
/**
* 添加片尾水印
*/
public static void addTailWaterMark(TXVideoEditer videoEditer, String originVideoPath) {
TXVideoEditConstants.TXVideoInfo info = TXVideoInfoReader.getInstance().getVideoFileInfo(originVideoPath);
if (info == null) {
Log.e(TAG, "addTailWaterMark info is null");
return;
}
Bitmap tailWaterMarkBitmap =createdBitmap();
float widthHeightRatio = tailWaterMarkBitmap.getWidth() / (float) tailWaterMarkBitmap.getHeight();
TXVideoEditConstants.TXRect rect = new TXVideoEditConstants.TXRect();
// 归一化的片尾水印,这里设置了一个固定值,水印占屏幕宽度的0.25。
rect.width = 0.25f;
// 后面根据实际图片的宽高比,计算出对应缩放后的图片的宽度:txRect.width * videoInfo.width 和高度:txRect.width * videoInfo.width / widthHeightRatio,然后计算出水印放中间时的左上角位置
rect.x = (info.width - rect.width * info.width) / (2f * info.width);
rect.y = (info.height - rect.width * info.width / widthHeightRatio) / (2f * info.height);
if (videoEditer != null) {
videoEditer.setTailWaterMark(tailWaterMarkBitmap, rect, DURATION_TAILWATERMARK);
}
}
public static void addWaterMark(TXVideoEditer videoEditer, String originVideoPath) {
TXVideoEditConstants.TXVideoInfo info = TXVideoInfoReader.getInstance().getVideoFileInfo(originVideoPath);
if (info == null) {
Log.e(TAG, "addTailWaterMark info is null");
return;
}
Bitmap waterMarkBitmap=createdBitmap();
TXVideoEditConstants.TXRect rect = new TXVideoEditConstants.TXRect();
rect.width = 0.2f;
rect.x=rect.y=0.01f;
if (videoEditer != null) {
videoEditer.setWaterMark(waterMarkBitmap, rect);
}
}
private static Bitmap createdBitmap(){
Bitmap waterMarkBitmap = BitmapFactory.decodeResource(CommonAppContext.sInstance.getResources(), R.mipmap.ic_launcher);
//加文字
return getHasTextBitmap("ID:"+ CommonAppConfig.getInstance().getUid(),waterMarkBitmap);
}
/**
* 生成带文字的bitmap
* @param text
* @param srcBitmap
* @return
*/
private static Bitmap getHasTextBitmap(String text,Bitmap srcBitmap) {
Bitmap newBitmap = Bitmap.createBitmap(srcBitmap.getWidth(),srcBitmap.getHeight()*2, Bitmap.Config.ARGB_4444);
Canvas canvas = new Canvas(newBitmap);
canvas.drawBitmap(srcBitmap, 0,0,null);
TextPaint textPaint = new TextPaint();
textPaint.setColor(Color.parseColor("#ff6131"));
textPaint.setAntiAlias(true);
textPaint.setTextSize(18.0F);
textPaint.setFakeBoldText(true);
StaticLayout sl= new StaticLayout(text, textPaint, newBitmap.getWidth(), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
canvas.translate(10, srcBitmap.getHeight()+10);
sl.draw(canvas);
return newBitmap;
}
public static Bitmap bitmapScale(Bitmap bitmap, int dst_w, int dst_h) {
int src_w = bitmap.getWidth();
int src_h = bitmap.getHeight();
float scale_w = ((float) dst_w) / src_w;
float scale_h = ((float) dst_h) / src_h;
Matrix matrix = new Matrix();
matrix.postScale(scale_w, scale_h);
Bitmap dstbmp = Bitmap.createBitmap(bitmap, 0, 0, src_w, src_h, matrix,
true);
return dstbmp;
}
}
腾讯短视频二次开发-水印添加
最新推荐文章于 2022-08-11 10:04:16 发布