export function addWatermark(tempFilePath, gText) {
let Resources = plus.android.importClass('android.content.res.Resources')
let Bitmap = plus.android.importClass('android.graphics.Bitmap')
let BitmapFactory = plus.android.importClass('android.graphics.BitmapFactory')
let Canvas = plus.android.importClass('android.graphics.Canvas')
let Paint = plus.android.importClass('android.graphics.Paint')
let Color = plus.android.importClass('android.graphics.Color')
let TextPaint = plus.android.importClass('android.text.TextPaint')
let Layout = plus.android.importClass('android.text.Layout')
let StaticLayout = plus.android.importClass('android.text.StaticLayout')
let BufferedOutputStream = plus.android.importClass('java.io.BufferedOutputStream')
let File = plus.android.importClass('java.io.File')
let FileOutputStream = plus.android.importClass('java.io.FileOutputStream')
let filePath = plus.io.convertLocalFileSystemURL(tempFilePath)
let options = new BitmapFactory.Options()
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
let bitmap = BitmapFactory.decodeFile(filePath, options);
// 画图
let main = plus.android.runtimeMainActivity()
let application = main.getApplication()
let resources = application.getResources()
let displayMetrics = resources.getDisplayMetrics()
let scale = plus.android.getAttribute(displayMetrics, 'density' )
let bitmapConfig = bitmap.getConfig();
bitmapConfig = Bitmap.Config.ARGB_8888;
bitmap = bitmap.copy(bitmapConfig, true);
let canvas = new Canvas(bitmap);
let paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
// 设置颜色
paint.setColor(Color.WHITE);
paint.setTextSize((16 * scale));
paint.setShadowLayer(1, 0, 1, Color.WHITE);
let textWidth = canvas.getWidth() - (16 * scale);
// text-align: ALIGN_NORMAL,ALIGN_OPPOSITE,ALIGN_CENTER,ALIGN_LEFT,ALIGN_RIGHT;
let textLayout = new StaticLayout(
gText, paint, textWidth, Layout.Alignment.ALIGN_CENTER, 1, 0, false);
let textHeight = textLayout.getHeight();
canvas.save();
// 计算画的位置
canvas.translate(0, canvas.getHeight() - textHeight);
textLayout.draw(canvas);
canvas.restore();
let file = new File(filePath);
let bos = new BufferedOutputStream(new FileOutputStream(file));
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
bos.flush();
bos.close();
}
叠加地址和日期 使用\n可以折行,叠加位置可以自己计算
addWaterMark(tempFilePath) {
return new Promise((resolve, reject) => {
uni.getLocation({
geocode: true,
success: ({ address: { city, district, street, streetNum, poiName } }) => {
const address = city + district + street + streetNum + poiName
const date = dayjs().format('YYYY-MM-DD HH:mm')
addWatermark(tempFilePath, `${address}\n${date}`)
resolve(tempFilePath)
}
})
})
}