Android给图片加文字和图片水印
我们在做项目的时候有时候需要给图片添加水印,水寒今天就遇到了这样的问题,所以搞了一个工具类,贴出来大家直接调用就行。
/*** 图片工具类* @author 水寒* 欢迎访问水寒的个人博客:http://www.sunhome.org.cn**/
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();//创建一个bitmapBitmap 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>
(本文出自水寒的CSDN博客:http://blog.csdn.net/dawanganban)
在Activity中获取到ImageView对象,并且获取Bitmap对象,对Bitmap进行canva绘图,添加水印:
/*** 图片工具类* @author 水寒* 欢迎访问水寒的个人博客:http://www.sunhome.org.cn**/
public class MainActivity extends Activity {private ImageView mSourImage;private ImageView mWartermarkImage;@Overrideprotected 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给图片加文字和图片水印相关推荐
- ASP.NET(C#)图片加文字、图片水印
ASP.NET(C#)图片加文字.图片水印 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 2 ...
- 图片加文字(批量水印 批量水印大师)
图片加文字(批量水印 批量水印大师) 批量水印大师是一款好用的批量添加水印软件.三步操作即可轻松完成. 第一步: 添加图片 - 选择需要添加水印的图片文件. 第二步:水印设置 - 设置水印效果,所见即 ...
- ASP.NET(C#)图片加文字、图片水印(转)
一.图片上加文字: //using System.Drawing; //using System.IO; //using System.Drawing.Imaging; pri ...
- (PHP)图片加文字和图片合成
图片加文字 <?php $bigImgPath = 'backgroud.png';$img = imagecreatefromstring(file_get_contents($bigImgP ...
- C#给图片加文字和图片的水印
/// <summary> /// WaterMark 的摘要说明 /// </summary> /// 图片加水印/// <param name="strCo ...
- android 给图片加文字、图片水印
Stamper is a tool for stamping a pattern into a picture,it likes a watermark. Stamper是一个给图片打水印的工具,支持 ...
- php给图片加图片水印,php给图片添加文字或图片水印实现代码
原标题:php给图片添加文字或图片水印实现代码 一.文字水印 文字水印就是在图片上加上文字,主要使用gd库的imagefttext方法,并且需要字体文件.效果图如下: $dst_path = 'dst ...
- PHP实现给图片加文字水印
PHP实现给图片加文字水印 一.开发环境 1.Windows+Apache+MySQL+PHP的环境. 2.文本编辑器:Sublime. 二.主要技术 PHP+HTML+CSS 三.效果图与具体步骤 ...
- Java图片加文字水印
Java图片加文字水印 import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.I ...
- 阿里云 OSS 对象存储 OSS 图片加文字水印
阿里云 OSS 对象存储 实际开发需先阅读 阿里云 [OSS快速入门文档](https://help.aliyun.com/document_detail/31883.html?spm=5176.20 ...
最新文章
- 【java】实现数据在页面之间传输
- manjaro 火焰截图添加快捷键_Linux中功能强大的截图工具 - Flameshot
- 浏览器html5/css3兼容性检测的javascript类库 - Modernizr简单介绍
- [React Router v4] Conditionally Render a Route with the Switch Component
- mysql 中 add2_计算器中的F,4,2,0,ADD2怎么调,MU键有什么用??急急急
- 使用SAP Analytics Cloud显示新冠肺炎病毒感染人数的实时信息
- 域名解析文件hosts文件是什么?如何修改hosts文件?
- sqlmapapi的基本使用和源码阅读
- WPF Demo15 MVVM
- Ubuntu16.06LTS安装gnome-3.8桌面
- php中ci框架分页,Codeigniter(CI)框架分页函数及相关知识
- ruby服务器端解析json字符串
- 會議管理系統--項目總結
- length属性,length()方法和size()的方法的区别
- HTML5期末大作业:我的家乡网站设计——我的家乡-南京(4页)
- SPRAY 光谱光线追迹仿真软件
- 锂电池电量百分比计算_锂电池的电量、电压与放电时间的计算
- Java高性能序列化工具Kryo序列化
- python- selenium-快眼看书-林深终有路
- 《iPad开发从入门到精通》——6.4节收藏历史