我们在做项目的时候有时候需要给图片添加水印,水寒今天就遇到了这样的问题,所以搞了一个工具类,贴出来大家直接调用就行。
/**
* 图片工具类
* @author 水寒
*
*/
public class ImageUtil {
/**
* 设置水印图片在左上角
* @param Context
* @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, 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.ALL_SAVE_FLAG);
// 存储
canvas.restore();
return newb;
}
/**
* 设置水印图片在右下角
* @param Context
* @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 Context
* @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 Context
* @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 Context
* @param src
* @param watermark
* @return
*/
public static Bitmap createWaterMaskCenter(Bitmap src, Bitmap watermark) {
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 paddingLeft
* @param paddingTop
* @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) {
android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
paint.setDither(true); // 获取跟清晰的图像采样
paint.setFilterBitmap(true);// 过滤一些
if (bitmapConfig == null) {
bitmapConfig = android.graphics.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);
}
}
使用方法如下:
添加一个布局,上面是原始图片,下面是添加水印后的图片
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/sour_pic_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="原图" />
<ImageView
android:id="@+id/sour_pic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerInside"/>
<TextView
android:id="@+id/watermark_pic_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="水印" />
<ImageView
android:id="@+id/wartermark_pic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerInside"/>
</LinearLayout>
在Activity中获取到ImageView对象,并且获取Bitmap对象,对Bitmap进行canva绘图,添加水印:
/**
* 图片工具类
* @author 水寒
*
*/
public class MainActivity extends Activity {
private ImageView mSourImage;
private ImageView mWartermarkImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView(){
mSourImage = (ImageView) findViewById(R.id.sour_pic);
mWartermarkImage = (ImageView) findViewById(R.id.wartermark_pic);
Bitmap sourBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sour_pic);
mSourImage.setImageBitmap(sourBitmap);
Bitmap waterBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.weixin);
Bitmap watermarkBitmap = ImageUtil.createWaterMaskCenter(sourBitmap, waterBitmap);
watermarkBitmap = ImageUtil.createWaterMaskLeftBottom(this, watermarkBitmap, waterBitmap, 0, 0);
watermarkBitmap = ImageUtil.createWaterMaskRightBottom(this, watermarkBitmap, waterBitmap, 0, 0);
watermarkBitmap = ImageUtil.createWaterMaskLeftTop(this, watermarkBitmap, waterBitmap, 0, 0);
watermarkBitmap = ImageUtil.createWaterMaskRightTop(this, watermarkBitmap, waterBitmap, 0, 0);
Bitmap textBitmap = ImageUtil.drawTextToLeftTop(this, watermarkBitmap, "左上角", 16, Color.RED, 0, 0);
textBitmap = ImageUtil.drawTextToRightBottom(this, textBitmap, "右下角", 16, Color.RED, 0, 0);
textBitmap = ImageUtil.drawTextToRightTop(this, textBitmap, "右上角", 16, Color.RED, 0, 0);
textBitmap = ImageUtil.drawTextToLeftBottom(this, textBitmap, "左下角", 16, Color.RED, 0, 0);
textBitmap = ImageUtil.drawTextToCenter(this, textBitmap, "中间", 16, Color.RED);
mWartermarkImage.setImageBitmap(textBitmap);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# android
# 图片添加水印
# 图片水印
# 图片加水印
# Android视频处理之动态时间水印效果
# Android添加水印的正确方法 只要三步!
# Android实现为图片添加水印
# Android 给图片加上水印的示例代码(支持logo+文字)
# Android给任何view添加全屏倾斜水印
# Android 图片添加水印的实现方法
# android实现文字水印效果 支持多行水印
# Android图片添加水印图片并把图片保存到文件存储的实现代码
# Android实现分享长图并且添加全图水印
# Android可配置透明度的水印
# 创建一个
# 标上
# 就行
# 贴出
# 将该
# 搞了
# 大家多多
# 方法如下
# 直接调用
# paddingRight
# createWaterMaskRightBottom
# paddingBottom
# createWaterMaskRightTop
# text
# drawTextToLeftTop
# createWaterMaskLeftBottom
# createWaterMaskCenter
# Canvas
相关文章:
设计网站制作公司有哪些,制作网页教程?
php条件判断怎么写_ifelse和switchcase的使用区别【对比】
建站之星Pro快速搭建教程:模板选择与功能配置指南
一键制作网站软件下载安装,一键自动采集网页文档制作步骤?
详解免费开源的.NET多类型文件解压缩组件SharpZipLib(.NET组件介绍之七)
网站图片在线制作软件,怎么在图片上做链接?
如何快速生成ASP一键建站模板并优化安全性?
如何在景安服务器上快速搭建个人网站?
高性价比服务器租赁——企业级配置与24小时运维服务
如何快速搭建个人网站并优化SEO?
C++ static_cast和dynamic_cast区别_C++静态转换与动态类型安全转换
攀枝花网站建设,攀枝花营业执照网上怎么年审?
实现虚拟支付需哪些建站技术支撑?
如何登录建站主机?访问步骤全解析
免费制作海报的网站,哪位做平面的朋友告诉我用什么软件做海报比较好?ps还是cd还是ai这几个软件我都会些我是做网页的?
建站主机是什么?如何选择适合的建站主机?
如何高效配置香港服务器实现快速建站?
在线ppt制作网站有哪些软件,如何把网页的内容做成ppt?
如何用美橙互联一键搭建多站合一网站?
制作公司内部网站有哪些,内网如何建网站?
活动邀请函制作网站有哪些,活动邀请函文案?
做企业网站制作流程,企业网站制作基本流程有哪些?
如何在Golang中实现微服务服务拆分_Golang微服务拆分与接口管理方法
如何实现建站之星域名转发设置?
,想在网上投简历,哪几个网站比较好?
教学论文网站制作软件有哪些,写论文用什么软件
?
官网自助建站平台指南:在线制作、快速建站与模板选择全解析
家庭服务器如何搭建个人网站?
如何在香港服务器上快速搭建免备案网站?
如何做网站制作流程,*游戏网站怎么搭建?
香港服务器如何优化才能显著提升网站加载速度?
建站之星多图banner生成与模板自定义指南
宝塔建站无法访问?如何排查配置与端口问题?
如何解决ASP生成WAP建站中文乱码问题?
如何在万网自助建站中设置域名及备案?
建站之星24小时客服电话如何获取?
如何快速搭建高效服务器建站系统?
如何快速上传建站程序避免常见错误?
如何自定义建站之星模板颜色并下载新样式?
动图在线制作网站有哪些,滑动动图图集怎么做?
建站之星安装提示数据库无法连接如何解决?
如何高效搭建专业期货交易平台网站?
制作网站软件推荐手机版,如何制作属于自己的手机网站app应用?
建站DNS解析失败?如何正确配置域名服务器?
西安大型网站制作公司,西安招聘网站最好的是哪个?
香港服务器建站指南:外贸独立站搭建与跨境电商配置流程
如何正确选择百度移动适配建站域名?
rsync同步时出现rsync: failed to set times on “xxxx”: Operation not permitted
如何在服务器上三步完成建站并提升流量?
网站制作与设计教程,如何制作一个企业网站,建设网站的基本步骤有哪些?
*请认真填写需求信息,我们会在24小时内与您取得联系。