图片加水印文字

private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap photo = BitmapFactory.decodeFile(uri, options);
int width = photo.getWidth();
int height = photo.getHeight();

Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);//建立一个空的Bitmap
Canvas canvas = new Canvas(bitmap);

Paint photoPaint = new Paint(); //建立画笔
photoPaint.setDither(true); //获取跟清晰的图像采样
photoPaint.setFilterBitmap(true);//过滤一些
Rect src = new Rect(0, 0, width, height);//创建一个指定的新矩形的坐标
Rect dst = new Rect(0, 0, width, height);//创建一个指定的新矩形的坐标
canvas.drawBitmap(photo, src, dst, photoPaint);

Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);//设置画笔
textPaint.setTextSize(width / 20);//字体大小
textPaint.setTypeface(Typeface.DEFAULT);//采用默认的宽度
textPaint.setColor(Color.RED);//采用的颜色
canvas.drawText("保单号:" + policyNum, 10, height - 100, textPaint);
canvas.drawText("经度:" + String.format("%.6f", longitude) + "\t纬度:" + String.format("%.6f", latitude), 10, height - 70, textPaint);
canvas.drawText("时间:" + sdf.format(new Date(System.currentTimeMillis())), 10, height - 40, textPaint);

canvas.save(Canvas.ALL_SAVE_FLAG);//保存
canvas.restore();//存储

FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(uri);
bitmap.compress(Bitmap.CompressFormat.PNG, 0, fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.flush();
fileOutputStream.close();
} catch (IOException e) {
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值