1、给图片加水印
在做项目时想给图片加一个水印让其显示,水印添加的照片放置到资源目录下名称为R.mipmap.watermask,记录一下实现的代码方便以后再用。
/**
* @date : 2020/2/23
* @time : 21:24
* @author : viki
* @description : 添加水印图片
*/
private Bitmap waterMaskVideoPhoto( String imagepath) {
Bitmap bitmap = null;
try {
Bitmap src = BitmapFactory.decodeFile(imagepath);
//使用Drawble目录下的图片
//Bitmap src = BitmapFactory.decodeResource(context.getResources(),R.drawable.plus);
Bitmap mask = BitmapFactory.decodeResource(context.getResources(),R.mipmap.watermask);
Log.e(TAG," mask宽度: "+mask.getWidth());
Log.e(TAG," mask高度度: "+mask.getHeight());
Bitmap suoxiao = ThumbnailUtils.extractThumbnail(mask,200,200);
Log.e(TAG," 缩小后mask宽度: "+suoxiao.getWidth());
Log.e(TAG," 缩小后mask高度: "+suoxiao.getHeight());
bitmap = ImageUtil.createWaterMaskCenter(src,suoxiao);
} catch (Exception e){
e.printStackTrace();
}
return bitmap;
}
参数是图片的Android手机中的绝对地址加名称。
在此添加水印的代码参考的优快云上用户名成为lxq_xsyu的文章,文章为:Android给图片加文字和图片水印,该文章给出了一个工具类。下面我把代码贴。
2、工具类
(如有需要请查看上面连接中原作者的文章,我仅做下记录)
public class ImageUtil {
/**
* 设置水印图片在左上角
* @param
* @param src
* @param watermark
* @param paddingLeft
* @param paddingTop
* @return
*/
// public static Bitmap createWaterMaskLeftTop(
// Context context, Bitmap src, Bitmap watermark,
// int paddingLeft, int paddingTop) {
// return createWaterMaskBitmap(src, watermark,
// dp2px(context, paddingLeft), dp2px(context, paddingTop));
// }
private static Bitmap createWaterMaskBitmap(Bitmap src, Bitmap watermark,
int paddingLeft, int paddingTop) {
if (src == null) {
return null;
}
int width = src.getWidth();
int height = src.getHeight();
//创建一个bitmap
Bitmap newb = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图
//将该图片作为画布
Canvas canvas = new Canvas(newb);
//在画布 0,0坐标上开始绘制原始图片
canvas.drawBitmap(src, 0, 0, null);
//在画布上绘制水印图片
canvas.drawBitmap(watermark, paddingLeft, paddingTop, null);
// 保存
canvas.save();
// 存储
canvas.restore();
return newb;
}
// /**
// * 设置水印图片在右下角
// * @param
// * @param src
// * @param watermark
// * @param paddingRight
// * @param paddingBottom
// * @return
// */
// public static Bitmap createWaterMaskRightBottom(
// Context context, Bitmap src, Bitmap watermark,
// int paddingRight, int paddingBottom) {
// return createWaterMaskBitmap(src, watermark,
// src.getWidth() - watermark.getWidth() - dp2px(context, paddingRight),
// src.getHeight() - watermark.getHeight() - dp2px(context, paddingBottom));
// }
//
// /**
// * 设置水印图片到右上角
// * @param
// * @param src
// * @param watermark
// * @param paddingRight
// * @param paddingTop
// * @return
// */
// public static Bitmap createWaterMaskRightTop(
// Context context, Bitmap src, Bitmap watermark,
// int paddingRight, int paddingTop) {
// return createWaterMaskBitmap( src, watermark,
// src.getWidth() - watermark.getWidth() - dp2px(context, paddingRight),
// dp2px(context, paddingTop));
// }
//
// /**
// * 设置水印图片到左下角
// * @param
// * @param src
// * @param watermark
// * @param paddingLeft
// * @param paddingBottom
// * @return
// */
// public static Bitmap createWaterMaskLeftBottom(
// Context context, Bitmap src, Bitmap watermark,
// int paddingLeft, int paddingBottom) {
// return createWaterMaskBitmap(src, watermark, dp2px(context, paddingLeft),
// src.getHeight() - watermark.getHeight() - dp2px(context, paddingBottom));
// }
/**
* 设置水印图片到中间
* @param
* @param src
* @param watermark
* @return
*/
public static Bitmap createWaterMaskCenter(Bitmap src, Bitmap watermark) {
Log.e("测试水印","经过");
return createWaterMaskBitmap(src, watermark,
(src.getWidth() - watermark.getWidth()) / 2,
(src.getHeight() - watermark.getHeight()) / 2);
}
// /**
// * 给图片添加文字到左上角
// * @param context
// * @param bitmap
// * @param text
// * @return
// */
// public static Bitmap drawTextToLeftTop(Context context, Bitmap bitmap, String text,
// int size, int color, int paddingLeft, int paddingTop) {
// Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
// paint.setColor(color);
// paint.setTextSize(dp2px(context, size));
// Rect bounds = new Rect();
// paint.getTextBounds(text, 0, text.length(), bounds);
// return drawTextToBitmap(context, bitmap, text, paint, bounds,
// dp2px(context, paddingLeft),
// dp2px(context, paddingTop) + bounds.height());
// }
//
// /**
// * 绘制文字到右下角
// * @param context
// * @param bitmap
// * @param text
// * @param size
// * @param color
// * @param paddingRight
// * @param paddingBottom
// * @return
// */
// public static Bitmap drawTextToRightBottom(Context context, Bitmap bitmap, String text,
// int size, int color, int paddingRight, int paddingBottom) {
// Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
// paint.setColor(color);
// paint.setTextSize(dp2px(context, size));
// Rect bounds = new Rect();
// paint.getTextBounds(text, 0, text.length(), bounds);
// return drawTextToBitmap(context, bitmap, text, paint, bounds,
// bitmap.getWidth() - bounds.width() - dp2px(context, paddingRight),
// bitmap.getHeight() - dp2px(context, paddingBottom));
// }
//
// /**
// * 绘制文字到右上方
// * @param context
// * @param bitmap
// * @param text
// * @param size
// * @param color
// * @param paddingRight
// * @param paddingTop
// * @return
// */
// public static Bitmap drawTextToRightTop(Context context, Bitmap bitmap, String text,
// int size, int color, int paddingRight, int paddingTop) {
// Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
// paint.setColor(color);
// paint.setTextSize(dp2px(context, size));
// Rect bounds = new Rect();
// paint.getTextBounds(text, 0, text.length(), bounds);
// return drawTextToBitmap(context, bitmap, text, paint, bounds,
// bitmap.getWidth() - bounds.width() - dp2px(context, paddingRight),
// dp2px(context, paddingTop) + bounds.height());
// }
//
// /**
// * 绘制文字到左下方
// * @param context
// * @param bitmap
// * @param text
// * @param size
// * @param color
// * @param paddingLeft
// * @param paddingBottom
// * @return
// */
// public static Bitmap drawTextToLeftBottom(Context context, Bitmap bitmap, String text,
// int size, int color, int paddingLeft, int paddingBottom) {
// Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
// paint.setColor(color);
// paint.setTextSize(dp2px(context, size));
// Rect bounds = new Rect();
// paint.getTextBounds(text, 0, text.length(), bounds);
// return drawTextToBitmap(context, bitmap, text, paint, bounds,
// dp2px(context, paddingLeft),
// bitmap.getHeight() - dp2px(context, paddingBottom));
// }
//
// /**
// * 绘制文字到中间
// * @param context
// * @param bitmap
// * @param text
// * @param size
// * @param color
// * @return
// */
// public static Bitmap drawTextToCenter(Context context, Bitmap bitmap, String text,
// int size, int color) {
// Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
// paint.setColor(color);
// paint.setTextSize(dp2px(context, size));
// Rect bounds = new Rect();
// paint.getTextBounds(text, 0, text.length(), bounds);
// return drawTextToBitmap(context, bitmap, text, paint, bounds,
// (bitmap.getWidth() - bounds.width()) / 2,
// (bitmap.getHeight() + bounds.height()) / 2);
// }
//
// //图片上绘制文字
// private static Bitmap drawTextToBitmap(Context context, Bitmap bitmap, String text,
// Paint paint, Rect bounds, int paddingLeft, int paddingTop) {
// Bitmap.Config bitmapConfig = bitmap.getConfig();
//
// paint.setDither(true); // 获取跟清晰的图像采样
// paint.setFilterBitmap(true);// 过滤一些
// if (bitmapConfig == null) {
// bitmapConfig = Bitmap.Config.ARGB_8888;
// }
// bitmap = bitmap.copy(bitmapConfig, true);
// Canvas canvas = new Canvas(bitmap);
//
// canvas.drawText(text, paddingLeft, paddingTop, paint);
// return bitmap;
// }
//
// /**
// * 缩放图片
// * @param src
// * @param w
// * @param h
// * @return
// */
// public static Bitmap scaleWithWH(Bitmap src, double w, double h) {
// if (w == 0 || h == 0 || src == null) {
// return src;
// } else {
// // 记录src的宽高
// int width = src.getWidth();
// int height = src.getHeight();
// // 创建一个matrix容器
// Matrix matrix = new Matrix();
// // 计算缩放比例
// float scaleWidth = (float) (w / width);
// float scaleHeight = (float) (h / height);
// // 开始缩放
// matrix.postScale(scaleWidth, scaleHeight);
// // 创建缩放后的图片
// return Bitmap.createBitmap(src, 0, 0, width, height, matrix, true);
// }
// }
//
// /**
// * dip转pix
// * @param context
// * @param dp
// * @return
// */
// public static int dp2px(Context context, float dp) {
// final float scale = context.getResources().getDisplayMetrics().density;
// return (int) (dp * scale + 0.5f);
// }
}