desc:java方式水印支持换行文字,文字以@分割

main
xiaowusky 2 years ago
parent be26453cfe
commit 65978e5c68

@ -414,7 +414,11 @@ public class MediaStream extends Service implements LifecycleObserver {
Camera.getCameraInfo(mCameraId, camInfo);
int cameraRotationOffset = camInfo.orientation;
if (enableOverlay) {
overlay.javaOverlay(data, width, height, cameraRotationOffset, "EasyPusher");
overlay.javaOverlay(data,
width,
height,
cameraRotationOffset,
"1111111@2222222@333333");
}
if (mDgree == 0) {
if (cameraRotationOffset % 180 != 0) {
@ -422,16 +426,11 @@ public class MediaStream extends Service implements LifecycleObserver {
}
save2file(data, String.format("/sdcard/yuv_%d_%d.yuv", height, width));
}
if (enableOverlay) {
String txt = "EasyPusher " + new SimpleDateFormat("yy-MM-dd HH:mm:ss SSS").format(new Date());
overlay.overlay(data, txt);
}
mVC.onVideo(data, NV21);
mCamera.addCallbackBuffer(data);
} else {
if (enableOverlay) {
String txt = "EasyPusher " + new SimpleDateFormat("yy-MM-dd HH:mm:ss SSS").format(new Date());
overlay.javaOverlay(data, width, height, 0, txt);
String txt = "1111111@2222222@333333";
overlay.overlay(data, txt);
}
mVC.onVideo(data, NV21);

@ -2,13 +2,13 @@ package org.easydarwin.sw;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.text.TextUtils;
import org.easydarwin.easypusher.R;
import org.easydarwin.util.YUVUtils;
import java.io.File;
import java.text.SimpleDateFormat;
/**
* Created by John on 2017/2/23.
@ -21,20 +21,21 @@ public class TxtOverlay {
}
private final Context context;
private final static int init_startX = 10;
private final static int init_startY = 50;
int startY = 50;//水印Y轴的位置
int startX = 10;//水印X轴的位置
int startY = init_startX;//水印Y轴的位置
int startX = init_startY;//水印X轴的位置
String mTip = "";
long lastTipUpdateTime = 0;
Bitmap bmp;
byte[] mark;
SimpleDateFormat dateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
public TxtOverlay(Context context) {
this.context = context;
//从drawble中获取水印图片
Bitmap bmp1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.float_button);
//缩小图片
bmp = YUVUtils.scaleImage(bmp1, 40, 40);
//转YUV
mark = YUVUtils.getYUVByBitmap(bmp);
}
private long ctx;
@ -57,15 +58,33 @@ public class TxtOverlay {
public void javaOverlay(byte[] data, int width, int height, int cameraRotationOffset,
String txt) {
if (TextUtils.isEmpty(txt)) {
return;
}
long currentTimeMillis = System.currentTimeMillis();
// 限制获取bitmap的频率保证性能
if (TextUtils.isEmpty(mTip) || (!txt.equals(mTip) || currentTimeMillis - lastTipUpdateTime > 1000)) {
// 记录更新时间和上一次的文字
lastTipUpdateTime = currentTimeMillis;
mTip = txt;
// 文字转bitmap
bmp = YUVUtils.generateBitmap(dateFormat.format(lastTipUpdateTime) + "@" + txt, 30, Color.WHITE);
// 缩放旋转bitmap
bmp = YUVUtils.scaleImage(bmp, bmp.getWidth() / 2, bmp.getHeight() / 2, cameraRotationOffset);
//转YUV
mark = YUVUtils.getYUVByBitmap(bmp);
}
// 根据旋转角度调整位置
int bmpWidth = bmp.getWidth();
int bmpHeight = bmp.getHeight();
if (cameraRotationOffset == 90) {
startY = height - 50 - bmpHeight;
startX = 10;
startY = height - init_startY - bmpHeight;
startX = init_startX;
} else if (cameraRotationOffset == 270) {
startY = 50;
startX = width - 50 - bmpWidth;
startY = init_startY;
startX = width - init_startX - bmpWidth;
}
// 替换YUV数据
int j = 0;
for (int i = startY; i < bmpHeight + startY; i++) {
for (int c = 0; c < bmpWidth; c++) {

@ -1,11 +1,14 @@
package org.easydarwin.util;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.text.TextPaint;
public class YUVUtils {
//缩小图片到制定长宽
public static Bitmap scaleImage(Bitmap bm, int newWidth, int newHeight) {
public static Bitmap scaleImage(Bitmap bm, int newWidth, int newHeight, int cameraRotationOffset) {
if (bm == null) {
return null;
}
@ -14,6 +17,7 @@ public class YUVUtils {
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
matrix.postRotate(360 - cameraRotationOffset);
matrix.postScale(scaleWidth, scaleHeight);
Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,
true);
@ -29,8 +33,9 @@ public class YUVUtils {
if (bitmap == null) {
return null;
}
int width = bitmap.getWidth();
int height = bitmap.getHeight();
// /2*2保证都是偶数
int width = bitmap.getWidth() / 2 * 2;
int height = bitmap.getHeight() / 2 * 2;
int size = width * height;
@ -70,4 +75,33 @@ public class YUVUtils {
}
return yuv;
}
public static Bitmap generateBitmap(String text, int textSizePx, int textColor) {
TextPaint textPaint = new TextPaint();
textPaint.setTextSize(textSizePx);
textPaint.setColor(textColor);
Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
int lineHeight = (int) Math.ceil(Math.abs(fontMetrics.bottom) + Math.abs(fontMetrics.top));
Bitmap bitmap = null;
if (text.contains("@")) {
String[] split = text.split("@");
int width = 0;
for (int i = 0; i < split.length; i++) {
width = Math.max(width, (int) Math.ceil(textPaint.measureText(split[i])));
}
int height = lineHeight * split.length;
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
for (int i = 0; i < split.length; i++) {
canvas.drawText(split[i], 0, Math.abs(fontMetrics.ascent) + lineHeight * i, textPaint);
}
} else {
int width = (int) Math.ceil(textPaint.measureText(text));
int height = lineHeight;
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawText(text, 0, Math.abs(fontMetrics.ascent), textPaint);
}
return bitmap;
}
}

Loading…
Cancel
Save