当前位置:   article > 正文

Android 为图片加图片水印和文字水印. 复制即用,阅读即懂_android createwatermaskrightbottom

android createwatermaskrightbottom

为防止客户的身份证图片流失,防止他人盗用,所以我们的身份证可能需要用到给身份证图片加水印


文章最后会给出demo下载地址


老规矩,线上效果图:

1.首先是我们的图片工具类

  1. import android.content.Context;
  2. import android.graphics.Bitmap;
  3. import android.graphics.Canvas;
  4. import android.graphics.Matrix;
  5. import android.graphics.Paint;
  6. import android.graphics.Rect;
  7. /**
  8. * 图片工具类
  9. *
  10. */
  11. public class ImageUtil {
  12. /**
  13. * 设置水印图片在左上角
  14. * @param context
  15. * @param src
  16. * 前者为原图,后者为水印图片
  17. * @return
  18. */
  19. public static Bitmap createWaterMaskLeftTop(
  20. Context context, Bitmap src, Bitmap watermark,
  21. int paddingLeft, int paddingTop) {
  22. return createWaterMaskBitmap(src, watermark,
  23. dp2px(context, paddingLeft), dp2px(context, paddingTop));
  24. }
  25. private static Bitmap createWaterMaskBitmap(Bitmap src, Bitmap watermark,
  26. int paddingLeft, int paddingTop) {
  27. if (src == null) {
  28. return null;
  29. }
  30. int width = src.getWidth();
  31. int height = src.getHeight();
  32. //创建一个bitmap
  33. Bitmap newb = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图
  34. //将该图片作为画布
  35. Canvas canvas = new Canvas(newb);
  36. //在画布 0,0坐标上开始绘制原始图片
  37. canvas.drawBitmap(src, 0, 0, null);
  38. //在画布上绘制水印图片
  39. canvas.drawBitmap(watermark, paddingLeft, paddingTop, null);
  40. // 保存
  41. canvas.save(Canvas.ALL_SAVE_FLAG);
  42. // 存储
  43. canvas.restore();
  44. return newb;
  45. }
  46. /**
  47. * 设置水印图片在右下角
  48. * @param Context
  49. * @param src
  50. * @param watermark
  51. * @param paddingRight
  52. * @param paddingBottom
  53. * @return
  54. */
  55. public static Bitmap createWaterMaskRightBottom(
  56. Context context, Bitmap src, Bitmap watermark,
  57. int paddingRight, int paddingBottom) {
  58. return createWaterMaskBitmap(src, watermark,
  59. src.getWidth() - watermark.getWidth() - dp2px(context, paddingRight),
  60. src.getHeight() - watermark.getHeight() - dp2px(context, paddingBottom));
  61. }
  62. /**
  63. * 设置水印图片到右上角
  64. * @param context
  65. * @param src
  66. * @param watermark
  67. * @param paddingRight
  68. * @param paddingTop
  69. * @return
  70. */
  71. public static Bitmap createWaterMaskRightTop(
  72. Context context, Bitmap src, Bitmap watermark,
  73. int paddingRight, int paddingTop) {
  74. return createWaterMaskBitmap( src, watermark,
  75. src.getWidth() - watermark.getWidth() - dp2px(context, paddingRight),
  76. dp2px(context, paddingTop));
  77. }
  78. /**
  79. * 设置水印图片到左下角
  80. * @param context
  81. * @param src
  82. * @param watermark
  83. * @param paddingLeft
  84. * @param paddingBottom
  85. * @return
  86. */
  87. public static Bitmap createWaterMaskLeftBottom(
  88. Context context, Bitmap src, Bitmap watermark,
  89. int paddingLeft, int paddingBottom) {
  90. return createWaterMaskBitmap(src, watermark, dp2px(context, paddingLeft),
  91. src.getHeight() - watermark.getHeight() - dp2px(context, paddingBottom));
  92. }
  93. /**
  94. * 设置水印图片到中间
  95. * @param context
  96. * @param src
  97. * @param watermark
  98. * @return
  99. */
  100. public static Bitmap createWaterMaskCenter(Bitmap src, Bitmap watermark) {
  101. return createWaterMaskBitmap(src, watermark,
  102. (src.getWidth() - watermark.getWidth()) / 2,
  103. (src.getHeight() - watermark.getHeight()) / 2);
  104. }
  105. /**
  106. * 给图片添加文字到左上角
  107. * @param context
  108. * @param bitmap
  109. * @param text
  110. * @return
  111. */
  112. public static Bitmap drawTextToLeftTop(Context context, Bitmap bitmap, String text,
  113. int size, int color, int paddingLeft, int paddingTop) {
  114. Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  115. paint.setColor(color);
  116. paint.setTextSize(dp2px(context, size));
  117. Rect bounds = new Rect();
  118. paint.getTextBounds(text, 0, text.length(), bounds);
  119. return drawTextToBitmap(context, bitmap, text, paint, bounds,
  120. dp2px(context, paddingLeft),
  121. dp2px(context, paddingTop) + bounds.height());
  122. }
  123. /**
  124. * 绘制文字到右下角
  125. * @param context
  126. * @param bitmap
  127. * @param text
  128. * @param size
  129. * @param color
  130. * @param paddingBottom
  131. * @param paddingRight
  132. * @return
  133. */
  134. public static Bitmap drawTextToRightBottom(Context context, Bitmap bitmap, String text,
  135. int size, int color, int paddingRight, int paddingBottom) {
  136. Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  137. paint.setColor(color);
  138. paint.setTextSize(dp2px(context, size));
  139. Rect bounds = new Rect();
  140. paint.getTextBounds(text, 0, text.length(), bounds);
  141. return drawTextToBitmap(context, bitmap, text, paint, bounds,
  142. bitmap.getWidth() - bounds.width() - dp2px(context, paddingRight),
  143. bitmap.getHeight() - dp2px(context, paddingBottom));
  144. }
  145. /**
  146. * 绘制文字到右上方
  147. * @param context
  148. * @param bitmap
  149. * @param text
  150. * @param size
  151. * @param color
  152. * @param paddingRight
  153. * @param paddingTop
  154. * @return
  155. */
  156. public static Bitmap drawTextToRightTop(Context context, Bitmap bitmap, String text,
  157. int size, int color, int paddingRight, int paddingTop) {
  158. Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  159. paint.setColor(color);
  160. paint.setTextSize(dp2px(context, size));
  161. Rect bounds = new Rect();
  162. paint.getTextBounds(text, 0, text.length(), bounds);
  163. return drawTextToBitmap(context, bitmap, text, paint, bounds,
  164. bitmap.getWidth() - bounds.width() - dp2px(context, paddingRight),
  165. dp2px(context, paddingTop) + bounds.height());
  166. }
  167. /**
  168. * 绘制文字到左下方
  169. * @param context
  170. * @param bitmap
  171. * @param text
  172. * @param size
  173. * @param color
  174. * @param paddingLeft
  175. * @param paddingBottom
  176. * @return
  177. */
  178. public static Bitmap drawTextToLeftBottom(Context context, Bitmap bitmap, String text,
  179. int size, int color, int paddingLeft, int paddingBottom) {
  180. Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  181. paint.setColor(color);
  182. paint.setTextSize(dp2px(context, size));
  183. Rect bounds = new Rect();
  184. paint.getTextBounds(text, 0, text.length(), bounds);
  185. return drawTextToBitmap(context, bitmap, text, paint, bounds,
  186. dp2px(context, paddingLeft),
  187. bitmap.getHeight() - dp2px(context, paddingBottom));
  188. }
  189. /**
  190. * 绘制文字到中间
  191. * @param context
  192. * @param bitmap
  193. * @param text
  194. * @param size
  195. * @param color
  196. * @return
  197. */
  198. public static Bitmap drawTextToCenter(Context context, Bitmap bitmap, String text,
  199. int size, int color) {
  200. Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  201. paint.setColor(color);
  202. paint.setTextSize(dp2px(context, size));
  203. Rect bounds = new Rect();
  204. paint.getTextBounds(text, 0, text.length(), bounds);
  205. return drawTextToBitmap(context, bitmap, text, paint, bounds,
  206. (bitmap.getWidth() - bounds.width()) / 2,
  207. (bitmap.getHeight() + bounds.height()) / 2);
  208. }
  209. //图片上绘制文字
  210. private static Bitmap drawTextToBitmap(Context context, Bitmap bitmap, String text,
  211. Paint paint, Rect bounds, int paddingLeft, int paddingTop) {
  212. android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
  213. paint.setDither(true); // 获取跟清晰的图像采样
  214. paint.setFilterBitmap(true);// 过滤一些
  215. if (bitmapConfig == null) {
  216. bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
  217. }
  218. bitmap = bitmap.copy(bitmapConfig, true);
  219. Canvas canvas = new Canvas(bitmap);
  220. canvas.drawText(text, paddingLeft, paddingTop, paint);
  221. return bitmap;
  222. }
  223. /**
  224. * 缩放图片
  225. * @param src
  226. * @param w
  227. * @param h
  228. * @return
  229. */
  230. public static Bitmap scaleWithWH(Bitmap src, double w, double h) {
  231. if (w == 0 || h == 0 || src == null) {
  232. return src;
  233. } else {
  234. // 记录src的宽高
  235. int width = src.getWidth();
  236. int height = src.getHeight();
  237. // 创建一个matrix容器
  238. Matrix matrix = new Matrix();
  239. // 计算缩放比例
  240. float scaleWidth = (float) (w / width);
  241. float scaleHeight = (float) (h / height);
  242. // 开始缩放
  243. matrix.postScale(scaleWidth, scaleHeight);
  244. // 创建缩放后的图片
  245. return Bitmap.createBitmap(src, 0, 0, width, height, matrix, true);
  246. }
  247. }
  248. /**
  249. * dip转pix
  250. * @param context
  251. * @param dp
  252. * @return
  253. */
  254. public static int dp2px(Context context, float dp) {
  255. final float scale = context.getResources().getDisplayMetrics().density;
  256. return (int) (dp * scale + 0.5f);
  257. }
  258. }

2.然后是布局文件

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6. <TextView
  7. android:layout_margin="10dp"
  8. android:gravity="center"
  9. android:id="@+id/sour_pic_title"
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:text="原图" />
  13. <ImageView
  14. android:id="@+id/sour_pic"
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content"
  17. android:scaleType="centerInside"/>
  18. <TextView
  19. android:layout_margin="10dp"
  20. android:gravity="center"
  21. android:id="@+id/watermark_pic_title"
  22. android:layout_width="match_parent"
  23. android:layout_height="wrap_content"
  24. android:text="水印" />
  25. <ImageView
  26. android:id="@+id/wartermark_pic"
  27. android:layout_width="match_parent"
  28. android:layout_height="wrap_content"
  29. android:scaleType="centerInside"/>
  30. </LinearLayout>

3.最后是我们的MainActivity

  1. import android.app.Activity;
  2. import android.graphics.Bitmap;
  3. import android.graphics.BitmapFactory;
  4. import android.graphics.Color;
  5. import android.os.Bundle;
  6. import android.widget.ImageView;
  7. import com.watermark.ImageUtil;
  8. import com.watermark.R;
  9. public class MainActivity extends Activity {
  10. private ImageView mSourImage;
  11. private ImageView mWartermarkImage;
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_main);
  16. initView();
  17. }
  18. private void initView(){
  19. mSourImage = (ImageView) findViewById(R.id.sour_pic);
  20. mWartermarkImage = (ImageView) findViewById(R.id.wartermark_pic);
  21. Bitmap idCardBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.idcard);
  22. Bitmap logoBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon_logo);
  23. mSourImage.setImageBitmap(idCardBitmap);
  24. // Bitmap watermarkBitmap = ImageUtil.createWaterMaskCenter(idCardBitmap, logoBitmap);
  25. Bitmap watermarkBitmap = ImageUtil.createWaterMaskLeftBottom(this, idCardBitmap, logoBitmap, 0, 0);
  26. // watermarkBitmap = ImageUtil.createWaterMaskRightBottom(this, watermarkBitmap, waterBitmap, 0, 0);
  27. // watermarkBitmap = ImageUtil.createWaterMaskLeftTop(this, watermarkBitmap, waterBitmap, 0, 0);
  28. // watermarkBitmap = ImageUtil.createWaterMaskRightTop(this, watermarkBitmap, waterBitmap, 0, 0);
  29. mWartermarkImage.setImageBitmap(watermarkBitmap);
  30. // Bitmap textBitmap = ImageUtil.drawTextToLeftTop(this, watermarkBitmap, "左上角", 16, Color.RED, 0, 0);
  31. // textBitmap = ImageUtil.drawTextToRightBottom(this, textBitmap, "右下角", 16, Color.RED, 0, 0);
  32. // textBitmap = ImageUtil.drawTextToRightTop(this, textBitmap, "右上角", 16, Color.RED, 0, 0);
  33. // textBitmap = ImageUtil.drawTextToLeftBottom(this, textBitmap, "左下角", 16, Color.RED, 0, 0);
  34. // textBitmap = ImageUtil.drawTextToCenter(this, textBitmap, "中间", 16, Color.RED);
  35. // mWartermarkImage.setImageBitmap(textBitmap);
  36. }
  37. }

4.文章下载地址:

Android图片加水印

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/45701
推荐阅读
相关标签
  

闽ICP备14008679号