Android添加水印的正确方法 只要三步!
作者:柒号公园 时间:2022-08-07 16:48:15
开门见山,添加水印的方法非常简单,其实就只有3个步骤:
1、载入原始图片
2、载入水印图片
3、保存带有水印的图片
实现的原理就是:获取原始图片的宽高,然后,新建一个同样宽高的bitmap,将这个新的bitmap作为画布,接着,就在这个画布上面画原图,画水印图片,有文字就接着画文字。
上面哪个顺序一定不能乱,不然你可能就看不到水印,或则文字了,因为画在原图下面去了
绘制水印的代码如下:
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 newBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图
//将该图片作为画布
Canvas canvas = new Canvas(newBitmap);
//在画布 0,0坐标上开始绘制原始图片
canvas.drawBitmap(src, 0, 0, null);
//在画布上绘制水印图片
canvas.drawBitmap(watermark, paddingLeft, paddingTop, null);
// 保存
canvas.save(Canvas.ALL_SAVE_FLAG);
// 存储
canvas.restore();
return newBitmap;
}
绘制文字的代码如下:
/**
* 绘制文字到中间
*
* @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) {
Config bitmapConfig = bitmap.getConfig();
paint.setDither(true); // 获取跟清晰的图像采样
paint.setFilterBitmap(true);// 过滤一些
if (bitmapConfig == null) {
bitmapConfig = Config.ARGB_8888;
}
bitmap = bitmap.copy(bitmapConfig, true);
Canvas canvas = new Canvas(bitmap);
canvas.drawText(text, paddingLeft, paddingTop, paint);
return bitmap;
}
效果图如下:
github地址为:https://github.com/chenguo4930/Watermark
git地址为:https://github.com/chenguo4930/Watermark.git
标签:Android,水印
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java毕业设计实战之图片展览馆管理系统的实现
2021-06-16 08:23:18
![](https://img.aspxhome.com/file/2023/1/62521_0s.jpg)
springboot基于docsify 实现随身文档
2023-04-25 04:52:23
![](https://img.aspxhome.com/file/2023/6/89486_0s.png)
Java Swing实现JTable检测单元格数据变更事件的方法示例
2022-10-16 19:49:29
![](https://img.aspxhome.com/file/2023/2/110622_0s.jpg)
SpringCloud重试机制配置详解
2023-05-13 23:48:46
![](https://img.aspxhome.com/file/2023/0/69710_0s.png)
C# 对PDF文档加密、解密(基于Spire.Cloud.SDK for .NET)
2021-11-23 05:37:26
![](https://img.aspxhome.com/file/2023/4/100174_0s.png)
如何使用Spring AOP的通知类型及创建通知
2022-03-19 19:32:51
![](https://img.aspxhome.com/file/2023/2/61242_0s.png)
springmvc 分页查询的简单实现示例代码
2022-01-09 11:08:22
详解java一维数组及练习题实例
2023-05-21 17:31:56
Java spring AOP基础
2022-08-10 12:39:25
Kotlin协程低级api startCoroutine与ContinuationInterceptor
2022-05-04 21:45:50
![](https://img.aspxhome.com/file/2023/2/137492_0s.jpg)
lambda表达式解决java后台分组排序过程解析
2023-11-29 06:03:39
Java Set集合及其子类HashSet与LinkedHashSet详解
2023-11-26 11:39:35
![](https://img.aspxhome.com/file/2023/0/59510_0s.png)
Java状态设计模式实现对象状态转换的优雅方式
2023-11-20 10:33:59
![](https://img.aspxhome.com/file/2023/5/92565_0s.png)
Android实现选项菜单子菜单
2023-06-14 16:06:26
![](https://img.aspxhome.com/file/2023/9/114279_0s.jpg)
java中javamail发送带附件的邮件实现方法
2023-12-06 02:46:30
JVM 心得分享(加载 链接 初始化)
2023-09-01 19:17:50
利用kotlin实现一个打方块的小游戏实例教程
2023-01-30 04:21:25
![](https://img.aspxhome.com/file/2023/4/111744_0s.gif)
在Visual Studio上构建C++的GUI框架wxWidgets的开发环境
2023-04-12 01:02:06
![](https://img.aspxhome.com/file/2023/2/139452_0s.png)
springboot基于Redis发布订阅集群下WebSocket的解决方案
2023-07-14 04:37:45
![](https://img.aspxhome.com/file/2023/7/125237_0s.png)
Java线程池ThreadPoolExecutor源码深入分析
2023-11-09 19:49:36