1.首先给大家推荐一个安卓工具库,该库封装了Android开发中常用的功能
https://github.com/Blankj/AndroidUtilCode/blob/master/lib/utilcode/README-CN.md
2.给图片添加水印
/***
* 添加图片水印
* @param srcBitmap 源图像
* @param watermark 水印图像
* @return
*/
private static Bitmap createWaterMarkBitmap(Bitmap srcBitmap, Bitmap watermark) {
if (srcBitmap == null) {
return null;
}
int srcWidth = srcBitmap.getWidth();//获取源图像的宽
int srcHeight = srcBitmap.getHeight();//获取源图像的高
int waterWidth = watermark.getWidth();//获取水印图像的宽
int waterHeight = watermark.getHeight();//获取水印图像的高
//int proportion= waterWidth / srcWidth; //得到宽初始比例
int proportionWidth = (int) (srcWidth * 0.16); //计算后宽的比例值
int proportionHeight = ((proportionWidth * 100 / waterWidth)) * waterHeight / 100;//计算后高的比例值
//返回根据比例缩放之后bitmap
Bitmap newBitmap = getNewBitmap(watermark, proportionWidth, proportionHeight);
Bitmap bitmap = Bitmap.createBitmap(srcWidth, srcHeight, Bitmap.Config.ARGB_8888);//创建一个空白位图
Canvas canvas = new Canvas(bitmap);//根据位图创建画布
canvas.drawBitmap(srcBitmap, 0, 0, null);//先画出源图像
canvas.drawBitmap(newBitmap, srcWidth - newBitmap.getWidth() - (newBitmap.getWidth() / 4), srcHeight - newBitmap.getHeight() - (newBitmap.getHeight() / 5), null);//然后在画水印图像
return bitmap;//返回水印图片
}
放大缩放原始水印图片,因对不同大小的图片
/**
* 代码修改bitmap的宽高
*
* @param bitmap
* @param newWidth
* @param newHeight
* @return
*/
public static Bitmap getNewBitmap(Bitmap bitmap, int newWidth, int newHeight) {
// 获得图片的宽高.
int width = bitmap.getWidth();
int height = bitmap.getHeight();
// 计算缩放比例.
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 取得想要缩放的matrix参数.
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 得到新的图片.
Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
return newBitmap;
}
最后使用
/**
* 保存图片并添加水印
* @param imgUrl
* @param context
*/
private static void SaveWatermarkImg(final String imgUrl, final Activity context) {
new AsyncTask<Void, Void, Bitmap>() {
@Override
protected Bitmap doInBackground(Void... params) {
Bitmap bitmap = null;
try {
bitmap = Glide.with(context)
.load(imgUrl)
.asBitmap()
.centerCrop()
.into(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
.get();
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
Bitmap watermarkImg;
watermarkImg = createWaterMarkBitmap(bitmap, BitmapFactory.decodeResource(context.getResources(), R.mipmap.pplogo));
String saveImgUrl = Environment.getExternalStorageDirectory().getPath();
//图片路径文件 (这里我使用已经封装好的工具类,我就不贴出来了)
FileUtils.createOrExistsDir(saveImgUrl);//判断是否有当前路径,没有创建
String createImg = DateUtil.getCurrentTime() + ".jpg";
//(这里我使用就是推荐安卓工具库已经封装好的工具类)
ImageUtils.save(watermarkImg, saveImgUrl + createImg, Bitmap.CompressFormat.JPEG);
// 把文件插入到系统图库
try {
MediaStore.Images.Media.insertImage(context.getContentResolver(), watermarkImg, "", "");
} catch (Exception e) {
e.printStackTrace();
}
// 通知图库更新
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + saveImgUrl)));
ToastUtil.showRaw("图片下载成功");
}
}.execute();