android图片上加有汉字,图片添加文字水印-安卓

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)

}

})

})

}

f9e08ca4b4163e131fa0c0739c36ed54.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值