当前位置:   article > 正文

Android图片添加水印_android 图片加自定义水印

android 图片加自定义水印

最终效果

参考https://github.com/echoMu/WaterMaskDemohttps://github.com/echoMu/WaterMaskDemo

首先要自定义左下角的view

WaterMaskView

  1. import android.content.Context;
  2. import android.util.AttributeSet;
  3. import android.view.LayoutInflater;
  4. import android.widget.RelativeLayout;
  5. import android.widget.TextView;
  6. import com.enneagon.net.R;
  7. /**
  8. * 自定义水印view
  9. */
  10. public class WaterMaskView extends RelativeLayout {
  11. private TextView tv_date;
  12. private TextView tv_stationName;
  13. private TextView tv_long;
  14. private TextView tv_lat;
  15. private TextView tv_address;
  16. private TextView tv_date2;
  17. private TextView tv_stationName2;
  18. private TextView tv_long2;
  19. private TextView tv_lat2;
  20. private TextView tv_address2;
  21. public WaterMaskView(Context context) {
  22. this(context, null);
  23. }
  24. public WaterMaskView(Context context, AttributeSet attrs) {
  25. super(context, attrs);
  26. LayoutInflater.from(context).inflate(R.layout.layout_watermask, this, true);
  27. tv_date = (TextView) findViewById(R.id.tv_date);
  28. tv_stationName = (TextView) findViewById(R.id.tv_stationName);
  29. tv_long = (TextView) findViewById(R.id.tv_long);
  30. tv_lat = (TextView) findViewById(R.id.tv_lat);
  31. tv_address = (TextView) findViewById(R.id.tv_address);
  32. tv_date2 = (TextView) findViewById(R.id.tv_date2);
  33. tv_stationName2 = (TextView) findViewById(R.id.tv_stationName2);
  34. tv_long2 = (TextView) findViewById(R.id.tv_long2);
  35. tv_lat2 = (TextView) findViewById(R.id.tv_lat2);
  36. tv_address2 = (TextView) findViewById(R.id.tv_address2);
  37. }
  38. public void setInfoDate(String content) {
  39. tv_date.setText(content);
  40. tv_date2.setText(content);
  41. }
  42. public void setInfoAddress(String content) {
  43. tv_address.setText(content);
  44. tv_address2.setText(content);
  45. }
  46. public void setInfoLat(String content) {
  47. tv_lat.setText(content);
  48. tv_lat2.setText(content);
  49. }
  50. public void setInfoLon(String content) {
  51. tv_long.setText(content);
  52. tv_long2.setText(content);
  53. }
  54. public void setInfoStationName(String content) {
  55. tv_stationName.setText(content);
  56. tv_stationName2.setText(content);
  57. }
  58. }

layout_watermask.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="wrap_content"
  5. android:layout_height="wrap_content">
  6. <LinearLayout
  7. android:paddingRight="4dp"
  8. android:layout_width="220dp"
  9. android:layout_height="wrap_content"
  10. android:alpha="0.4"
  11. android:background="@color/black"
  12. android:orientation="vertical"
  13. android:paddingTop="4dp"
  14. android:paddingLeft="4dp"
  15. android:paddingBottom="4dp">
  16. <LinearLayout
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:orientation="horizontal">
  20. <TextView
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:text="日"
  24. android:textColor="@color/white"
  25. android:textSize="14sp"
  26. android:visibility="invisible" />
  27. <TextView
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:text="占"
  31. android:textColor="@color/white"
  32. android:textSize="14sp"
  33. android:visibility="invisible" />
  34. <TextView
  35. android:layout_width="wrap_content"
  36. android:layout_height="wrap_content"
  37. android:text="占"
  38. android:textColor="@color/white"
  39. android:textSize="14sp"
  40. android:visibility="invisible" />
  41. <TextView
  42. android:layout_width="wrap_content"
  43. android:layout_height="wrap_content"
  44. android:text="期:"
  45. android:textColor="@color/white"
  46. android:textSize="14sp"
  47. android:visibility="invisible" />
  48. <TextView
  49. android:id="@+id/tv_date2"
  50. android:layout_width="wrap_content"
  51. android:layout_height="wrap_content"
  52. android:layout_marginLeft="4dp"
  53. android:textColor="@color/white"
  54. android:textSize="14sp"
  55. android:visibility="invisible"
  56. tools:text="2021-07-12 00:00:00" />
  57. </LinearLayout>
  58. <LinearLayout
  59. android:layout_width="wrap_content"
  60. android:layout_height="wrap_content"
  61. android:layout_marginTop="4dp"
  62. android:orientation="horizontal">
  63. <TextView
  64. android:layout_width="wrap_content"
  65. android:layout_height="wrap_content"
  66. android:text="站点名称:"
  67. android:textColor="@color/white"
  68. android:textSize="14sp"
  69. android:visibility="invisible" />
  70. <TextView
  71. android:id="@+id/tv_stationName2"
  72. android:layout_width="wrap_content"
  73. android:layout_height="wrap_content"
  74. android:layout_marginLeft="4dp"
  75. android:textColor="@color/white"
  76. android:textSize="14sp"
  77. android:visibility="invisible"
  78. tools:text="xxx站点" />
  79. </LinearLayout>
  80. <LinearLayout
  81. android:layout_width="wrap_content"
  82. android:layout_height="wrap_content"
  83. android:layout_marginTop="4dp"
  84. android:orientation="horizontal">
  85. <TextView
  86. android:layout_width="wrap_content"
  87. android:layout_height="wrap_content"
  88. android:text="经"
  89. android:textColor="@color/white"
  90. android:textSize="14sp"
  91. android:visibility="invisible" />
  92. <TextView
  93. android:layout_width="wrap_content"
  94. android:layout_height="wrap_content"
  95. android:text="占"
  96. android:textColor="@color/white"
  97. android:textSize="14sp"
  98. android:visibility="invisible" />
  99. <TextView
  100. android:layout_width="wrap_content"
  101. android:layout_height="wrap_content"
  102. android:text="占"
  103. android:textColor="@color/white"
  104. android:textSize="14sp"
  105. android:visibility="invisible" />
  106. <TextView
  107. android:layout_width="wrap_content"
  108. android:layout_height="wrap_content"
  109. android:text="度:"
  110. android:textColor="@color/white"
  111. android:textSize="14sp"
  112. android:visibility="invisible" />
  113. <TextView
  114. android:id="@+id/tv_long2"
  115. android:layout_width="wrap_content"
  116. android:layout_height="wrap_content"
  117. android:layout_marginLeft="4dp"
  118. android:textColor="@color/white"
  119. android:textSize="14sp"
  120. android:visibility="invisible"
  121. tools:text="333333" />
  122. </LinearLayout>
  123. <LinearLayout
  124. android:layout_width="wrap_content"
  125. android:layout_height="wrap_content"
  126. android:layout_marginTop="4dp"
  127. android:orientation="horizontal">
  128. <TextView
  129. android:layout_width="wrap_content"
  130. android:layout_height="wrap_content"
  131. android:text="纬"
  132. android:textColor="@color/white"
  133. android:textSize="14sp"
  134. android:visibility="invisible" />
  135. <TextView
  136. android:layout_width="wrap_content"
  137. android:layout_height="wrap_content"
  138. android:text="占"
  139. android:textColor="@color/white"
  140. android:textSize="14sp"
  141. android:visibility="invisible" />
  142. <TextView
  143. android:layout_width="wrap_content"
  144. android:layout_height="wrap_content"
  145. android:text="占"
  146. android:textColor="@color/white"
  147. android:textSize="14sp"
  148. android:visibility="invisible" />
  149. <TextView
  150. android:layout_width="wrap_content"
  151. android:layout_height="wrap_content"
  152. android:text="度:"
  153. android:textColor="@color/white"
  154. android:textSize="14sp"
  155. android:visibility="invisible" />
  156. <TextView
  157. android:id="@+id/tv_lat2"
  158. android:layout_width="wrap_content"
  159. android:layout_height="wrap_content"
  160. android:layout_marginLeft="4dp"
  161. android:textColor="@color/white"
  162. android:textSize="14sp"
  163. android:visibility="invisible"
  164. tools:text="1" />
  165. </LinearLayout>
  166. <LinearLayout
  167. android:layout_width="wrap_content"
  168. android:layout_height="wrap_content"
  169. android:layout_marginTop="4dp"
  170. android:orientation="horizontal">
  171. <TextView
  172. android:layout_width="wrap_content"
  173. android:layout_height="wrap_content"
  174. android:text="地"
  175. android:textColor="@color/white"
  176. android:textSize="14sp"
  177. android:visibility="invisible" />
  178. <TextView
  179. android:layout_width="wrap_content"
  180. android:layout_height="wrap_content"
  181. android:text="占"
  182. android:textColor="@color/white"
  183. android:textSize="14sp"
  184. android:visibility="invisible" />
  185. <TextView
  186. android:layout_width="wrap_content"
  187. android:layout_height="wrap_content"
  188. android:text="占"
  189. android:textColor="@color/white"
  190. android:textSize="14sp"
  191. android:visibility="invisible" />
  192. <TextView
  193. android:layout_width="wrap_content"
  194. android:layout_height="wrap_content"
  195. android:text="址:"
  196. android:textColor="@color/white"
  197. android:textSize="14sp"
  198. android:visibility="invisible" />
  199. <TextView
  200. android:id="@+id/tv_address2"
  201. android:layout_width="wrap_content"
  202. android:layout_height="wrap_content"
  203. android:layout_marginLeft="4dp"
  204. android:textColor="@color/white"
  205. android:textSize="14sp"
  206. android:visibility="invisible"
  207. tools:text="1234" />
  208. </LinearLayout>
  209. </LinearLayout>
  210. <LinearLayout
  211. android:layout_width="220dp"
  212. android:layout_height="wrap_content"
  213. android:orientation="vertical"
  214. android:paddingLeft="4dp"
  215. android:paddingTop="4dp"
  216. android:paddingRight="4dp"
  217. android:paddingBottom="4dp">
  218. <LinearLayout
  219. android:layout_width="wrap_content"
  220. android:layout_height="wrap_content"
  221. android:orientation="horizontal">
  222. <TextView
  223. android:layout_width="wrap_content"
  224. android:layout_height="wrap_content"
  225. android:text="日"
  226. android:textColor="@color/white"
  227. android:textSize="14sp" />
  228. <TextView
  229. android:layout_width="wrap_content"
  230. android:layout_height="wrap_content"
  231. android:text="占"
  232. android:textColor="@color/white"
  233. android:textSize="14sp"
  234. android:visibility="invisible" />
  235. <TextView
  236. android:layout_width="wrap_content"
  237. android:layout_height="wrap_content"
  238. android:text="占"
  239. android:textColor="@color/white"
  240. android:textSize="14sp"
  241. android:visibility="invisible" />
  242. <TextView
  243. android:layout_width="wrap_content"
  244. android:layout_height="wrap_content"
  245. android:text="期:"
  246. android:textColor="@color/white"
  247. android:textSize="14sp" />
  248. <TextView
  249. android:id="@+id/tv_date"
  250. android:layout_width="wrap_content"
  251. android:layout_height="wrap_content"
  252. android:layout_marginLeft="4dp"
  253. android:textColor="@color/white"
  254. android:textSize="14sp"
  255. tools:text="2021-07-12 00:00:00" />
  256. </LinearLayout>
  257. <LinearLayout
  258. android:layout_width="wrap_content"
  259. android:layout_height="wrap_content"
  260. android:layout_marginTop="4dp"
  261. android:orientation="horizontal">
  262. <TextView
  263. android:layout_width="wrap_content"
  264. android:layout_height="wrap_content"
  265. android:text="站点名称:"
  266. android:textColor="@color/white"
  267. android:textSize="14sp" />
  268. <TextView
  269. android:id="@+id/tv_stationName"
  270. android:layout_width="wrap_content"
  271. android:layout_height="wrap_content"
  272. android:layout_marginLeft="4dp"
  273. android:textColor="@color/white"
  274. android:textSize="14sp"
  275. tools:text="xxx站点" />
  276. </LinearLayout>
  277. <LinearLayout
  278. android:layout_width="wrap_content"
  279. android:layout_height="wrap_content"
  280. android:layout_marginTop="4dp"
  281. android:orientation="horizontal">
  282. <TextView
  283. android:layout_width="wrap_content"
  284. android:layout_height="wrap_content"
  285. android:text="经"
  286. android:textColor="@color/white"
  287. android:textSize="14sp" />
  288. <TextView
  289. android:layout_width="wrap_content"
  290. android:layout_height="wrap_content"
  291. android:text="占"
  292. android:textColor="@color/white"
  293. android:textSize="14sp"
  294. android:visibility="invisible" />
  295. <TextView
  296. android:layout_width="wrap_content"
  297. android:layout_height="wrap_content"
  298. android:text="占"
  299. android:textColor="@color/white"
  300. android:textSize="14sp"
  301. android:visibility="invisible" />
  302. <TextView
  303. android:layout_width="wrap_content"
  304. android:layout_height="wrap_content"
  305. android:text="度:"
  306. android:textColor="@color/white"
  307. android:textSize="14sp" />
  308. <TextView
  309. android:id="@+id/tv_long"
  310. android:layout_width="wrap_content"
  311. android:layout_height="wrap_content"
  312. android:layout_marginLeft="4dp"
  313. android:textColor="@color/white"
  314. android:textSize="14sp"
  315. tools:text="333333" />
  316. </LinearLayout>
  317. <LinearLayout
  318. android:layout_width="wrap_content"
  319. android:layout_height="wrap_content"
  320. android:layout_marginTop="4dp"
  321. android:orientation="horizontal">
  322. <TextView
  323. android:layout_width="wrap_content"
  324. android:layout_height="wrap_content"
  325. android:text="纬"
  326. android:textColor="@color/white"
  327. android:textSize="14sp" />
  328. <TextView
  329. android:layout_width="wrap_content"
  330. android:layout_height="wrap_content"
  331. android:text="占"
  332. android:textColor="@color/white"
  333. android:textSize="14sp"
  334. android:visibility="invisible" />
  335. <TextView
  336. android:layout_width="wrap_content"
  337. android:layout_height="wrap_content"
  338. android:text="占"
  339. android:textColor="@color/white"
  340. android:textSize="14sp"
  341. android:visibility="invisible" />
  342. <TextView
  343. android:layout_width="wrap_content"
  344. android:layout_height="wrap_content"
  345. android:text="度:"
  346. android:textColor="@color/white"
  347. android:textSize="14sp" />
  348. <TextView
  349. android:id="@+id/tv_lat"
  350. android:layout_width="wrap_content"
  351. android:layout_height="wrap_content"
  352. android:layout_marginLeft="4dp"
  353. android:textColor="@color/white"
  354. android:textSize="14sp"
  355. tools:text="11F" />
  356. </LinearLayout>
  357. <LinearLayout
  358. android:layout_width="wrap_content"
  359. android:layout_height="wrap_content"
  360. android:layout_marginTop="4dp"
  361. android:orientation="horizontal">
  362. <TextView
  363. android:layout_width="wrap_content"
  364. android:layout_height="wrap_content"
  365. android:text="地"
  366. android:textColor="@color/white"
  367. android:textSize="14sp" />
  368. <TextView
  369. android:layout_width="wrap_content"
  370. android:layout_height="wrap_content"
  371. android:text="占"
  372. android:textColor="@color/white"
  373. android:textSize="14sp"
  374. android:visibility="invisible" />
  375. <TextView
  376. android:layout_width="wrap_content"
  377. android:layout_height="wrap_content"
  378. android:text="占"
  379. android:textColor="@color/white"
  380. android:textSize="14sp"
  381. android:visibility="invisible" />
  382. <TextView
  383. android:layout_width="wrap_content"
  384. android:layout_height="wrap_content"
  385. android:text="址:"
  386. android:textColor="@color/white"
  387. android:textSize="14sp" />
  388. <TextView
  389. android:id="@+id/tv_address"
  390. android:layout_width="wrap_content"
  391. android:layout_height="wrap_content"
  392. android:layout_marginLeft="4dp"
  393. android:textColor="@color/white"
  394. android:textSize="14sp"
  395. tools:text="1234" />
  396. </LinearLayout>
  397. </LinearLayout>
  398. </RelativeLayout>

接着是水印工具类

WaterMaskUtil

  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. import android.view.View;
  8. /**
  9. * <pre>
  10. * author : echoMu
  11. * e-mail :
  12. * time : 2017/07/12
  13. * desc :
  14. * version:
  15. * </pre>
  16. */
  17. public class WaterMaskUtil {
  18. /**
  19. * 设置水印图片在左上角
  20. * @param src
  21. * @param watermark
  22. * @param paddingLeft
  23. * @param paddingTop
  24. * @return
  25. */
  26. public static Bitmap createWaterMaskLeftTop(
  27. Context context, Bitmap src, Bitmap watermark,
  28. int paddingLeft, int paddingTop) {
  29. return createWaterMaskBitmap(src, watermark,
  30. dp2px(context, paddingLeft), dp2px(context, paddingTop));
  31. }
  32. /**
  33. * 设置水印图片在右下角
  34. * @param src
  35. * @param watermark
  36. * @param paddingRight
  37. * @param paddingBottom
  38. * @return
  39. */
  40. public static Bitmap createWaterMaskRightBottom(
  41. Context context, Bitmap src, Bitmap watermark,
  42. int paddingRight, int paddingBottom) {
  43. return createWaterMaskBitmap(src, watermark,
  44. src.getWidth() - watermark.getWidth() - dp2px(context, paddingRight),
  45. src.getHeight() - watermark.getHeight() - dp2px(context, paddingBottom));
  46. }
  47. /**
  48. * 设置水印图片到右上角
  49. * @param src
  50. * @param watermark
  51. * @param paddingRight
  52. * @param paddingTop
  53. * @return
  54. */
  55. public static Bitmap createWaterMaskRightTop(
  56. Context context, Bitmap src, Bitmap watermark,
  57. int paddingRight, int paddingTop) {
  58. return createWaterMaskBitmap( src, watermark,
  59. src.getWidth() - watermark.getWidth() - dp2px(context, paddingRight),
  60. dp2px(context, paddingTop));
  61. }
  62. /**
  63. * 设置水印图片到左下角
  64. * @param src
  65. * @param watermark
  66. * @param paddingLeft
  67. * @param paddingBottom
  68. * @return
  69. */
  70. public static Bitmap createWaterMaskLeftBottom(
  71. Context context, Bitmap src, Bitmap watermark,
  72. int paddingLeft, int paddingBottom) {
  73. return createWaterMaskBitmap(src, watermark, dp2px(context, paddingLeft),
  74. src.getHeight() - watermark.getHeight() - dp2px(context, paddingBottom));
  75. }
  76. /**
  77. * 设置水印图片到中间
  78. * @param src
  79. * @param watermark
  80. * @return
  81. */
  82. public static Bitmap createWaterMaskCenter(Bitmap src, Bitmap watermark) {
  83. return createWaterMaskBitmap(src, watermark,
  84. (src.getWidth() - watermark.getWidth()) / 2,
  85. (src.getHeight() - watermark.getHeight()) / 2);
  86. }
  87. /**
  88. * 绘制水印图片
  89. * @param src 原图
  90. * @param watermark 水印
  91. * @param paddingLeft
  92. * @param paddingTop
  93. * @return
  94. */
  95. private static Bitmap createWaterMaskBitmap(Bitmap src, Bitmap watermark,
  96. int paddingLeft, int paddingTop) {
  97. if (src == null) {
  98. return null;
  99. }
  100. int width = src.getWidth();
  101. int height = src.getHeight();
  102. //创建一个bitmap
  103. Bitmap newb = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图
  104. //将该图片作为画布
  105. Canvas canvas = new Canvas(newb);
  106. //在画布 0,0坐标上开始绘制原始图片
  107. canvas.drawBitmap(src, 0, 0, null);
  108. //在画布上绘制水印图片
  109. canvas.drawBitmap(watermark, paddingLeft, paddingTop, null);
  110. // 保存
  111. canvas.save();
  112. // canvas.save(Canvas.ALL_SAVE_FLAG);
  113. // 存储
  114. canvas.restore();
  115. return newb;
  116. }
  117. /**
  118. * 给图片添加文字到左上角
  119. * @param context
  120. * @param bitmap
  121. * @param text
  122. * @return
  123. */
  124. public static Bitmap drawTextToLeftTop(Context context, Bitmap bitmap, String text,
  125. int size, int color, int paddingLeft, int paddingTop) {
  126. Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  127. paint.setColor(color);
  128. paint.setTextSize(dp2px(context, size));
  129. Rect bounds = new Rect();
  130. paint.getTextBounds(text, 0, text.length(), bounds);
  131. return drawTextToBitmap(context, bitmap, text, paint, bounds,
  132. dp2px(context, paddingLeft),
  133. dp2px(context, paddingTop) + bounds.height());
  134. }
  135. /**
  136. * 绘制文字到右下角
  137. * @param context
  138. * @param bitmap
  139. * @param text
  140. * @param size
  141. * @param color
  142. * @param paddingRight
  143. * @param paddingBottom
  144. * @return
  145. */
  146. public static Bitmap drawTextToRightBottom(Context context, Bitmap bitmap, String text,
  147. int size, int color, int paddingRight, int paddingBottom) {
  148. Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  149. paint.setColor(color);
  150. paint.setTextSize(dp2px(context, size));
  151. Rect bounds = new Rect();
  152. paint.getTextBounds(text, 0, text.length(), bounds);
  153. return drawTextToBitmap(context, bitmap, text, paint, bounds,
  154. bitmap.getWidth() - bounds.width() - dp2px(context, paddingRight),
  155. bitmap.getHeight() - dp2px(context, paddingBottom));
  156. }
  157. /**
  158. * 绘制文字到右上方
  159. * @param context
  160. * @param bitmap
  161. * @param text
  162. * @param size
  163. * @param color
  164. * @param paddingRight
  165. * @param paddingTop
  166. * @return
  167. */
  168. public static Bitmap drawTextToRightTop(Context context, Bitmap bitmap, String text,
  169. int size, int color, int paddingRight, int paddingTop) {
  170. Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  171. paint.setColor(color);
  172. paint.setTextSize(dp2px(context, size));
  173. Rect bounds = new Rect();
  174. paint.getTextBounds(text, 0, text.length(), bounds);
  175. return drawTextToBitmap(context, bitmap, text, paint, bounds,
  176. bitmap.getWidth() - bounds.width() - dp2px(context, paddingRight),
  177. dp2px(context, paddingTop) + bounds.height());
  178. }
  179. /**
  180. * 绘制文字到左下方
  181. * @param context
  182. * @param bitmap
  183. * @param text
  184. * @param size
  185. * @param color
  186. * @param paddingLeft
  187. * @param paddingBottom
  188. * @return
  189. */
  190. public static Bitmap drawTextToLeftBottom(Context context, Bitmap bitmap, String text,
  191. int size, int color, int paddingLeft, int paddingBottom) {
  192. Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  193. paint.setColor(color);
  194. paint.setTextSize(dp2px(context, size));
  195. Rect bounds = new Rect();
  196. paint.getTextBounds(text, 0, text.length(), bounds);
  197. return drawTextToBitmap(context, bitmap, text, paint, bounds,
  198. dp2px(context, paddingLeft),
  199. bitmap.getHeight() - dp2px(context, paddingBottom+2));
  200. }
  201. /**
  202. * 绘制文字到中间
  203. * @param context
  204. * @param bitmap
  205. * @param text
  206. * @param size
  207. * @param color
  208. * @return
  209. */
  210. public static Bitmap drawTextToCenter(Context context, Bitmap bitmap, String text,
  211. int size, int color) {
  212. Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  213. paint.setColor(color);
  214. paint.setTextSize(dp2px(context, size));
  215. Rect bounds = new Rect();
  216. paint.getTextBounds(text, 0, text.length(), bounds);
  217. return drawTextToBitmap(context, bitmap, text, paint, bounds,
  218. (bitmap.getWidth() - bounds.width()) / 2,
  219. (bitmap.getHeight() + bounds.height()) / 2);
  220. }
  221. //图片上绘制文字
  222. private static Bitmap drawTextToBitmap(Context context, Bitmap bitmap, String text,
  223. Paint paint, Rect bounds, int paddingLeft, int paddingTop) {
  224. Bitmap.Config bitmapConfig = bitmap.getConfig();
  225. paint.setDither(true); // 获取跟清晰的图像采样
  226. paint.setFilterBitmap(true);// 过滤一些
  227. if (bitmapConfig == null) {
  228. bitmapConfig = Bitmap.Config.ARGB_8888;
  229. }
  230. bitmap = bitmap.copy(bitmapConfig, true);
  231. Canvas canvas = new Canvas(bitmap);
  232. // canvas.drawText(text, paddingLeft, paddingTop, paint);
  233. drawMultiLineText(text, paddingLeft, paddingTop, paint,canvas);
  234. return bitmap;
  235. }
  236. private static void drawMultiLineText(String str, float x, float y, Paint paint,
  237. Canvas canvas) {
  238. String[] lines = str.split("\n");
  239. float txtSize = -paint.ascent() + paint.descent();
  240. if (paint.getStyle() == Paint.Style.FILL_AND_STROKE
  241. || paint.getStyle() == Paint.Style.STROKE) {
  242. txtSize += paint.getStrokeWidth(); // add stroke width to the text
  243. }
  244. float lineSpace = txtSize * 0.1f; // default line spacing
  245. for (int i = 0; i < lines.length; ++i) {
  246. canvas.drawText(lines[i], x, y + (txtSize + lineSpace) * i, paint);
  247. }
  248. }
  249. /**
  250. * 缩放图片
  251. * @param src
  252. * @param w
  253. * @param h
  254. * @return
  255. */
  256. public static Bitmap scaleWithWH(Bitmap src, double w, double h) {
  257. if (w == 0 || h == 0 || src == null) {
  258. return src;
  259. } else {
  260. // 记录src的宽高
  261. int width = src.getWidth();
  262. int height = src.getHeight();
  263. // 创建一个matrix容器
  264. Matrix matrix = new Matrix();
  265. // 计算缩放比例
  266. float scaleWidth = (float) (w / width);
  267. float scaleHeight = (float) (h / height);
  268. // 开始缩放
  269. matrix.postScale(scaleWidth, scaleHeight);
  270. // 创建缩放后的图片
  271. return Bitmap.createBitmap(src, 0, 0, width, height, matrix, true);
  272. }
  273. }
  274. /**
  275. * dip转pix
  276. * @param context
  277. * @param dp
  278. * @return
  279. */
  280. public static int dp2px(Context context, float dp) {
  281. final float scale = context.getResources().getDisplayMetrics().density;
  282. return (int) (dp * scale + 0.5f);
  283. }
  284. /**
  285. * 将一个view转换为Bitmap
  286. * @param view
  287. * @return
  288. */
  289. public static Bitmap convertViewToBitmap(View view){
  290. view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
  291. view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
  292. view.buildDrawingCache();
  293. Bitmap bitmap = view.getDrawingCache();
  294. return bitmap;
  295. }
  296. /**
  297. * 缩放Bitmap图片
  298. *
  299. */
  300. public static Bitmap zoomBitmap(Bitmap bitmap, int width, int height) {
  301. int w = bitmap.getWidth();
  302. int h = bitmap.getHeight();
  303. Matrix matrix = new Matrix();
  304. float scaleWidth = ((float) width / w);
  305. float scaleHeight = ((float) height / h);
  306. matrix.postScale(scaleWidth, scaleHeight);// 利用矩阵进行缩放不会造成内存溢出
  307. Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
  308. return newbmp;
  309. }
  310. }

使用方式:

1、获取图片的URL,然后转成bitmap

  Bitmap sourBitmap = BitmapFactory.decodeFile(fileUrl);

2、添加水印,并转为Bitmap

  1. //默认设置水印位置在左下
  2. Bitmap WaterBitmap = saveWaterMask(context, 3, sourBitmap,lng, lat, address, stationName);

  1. /**
  2. * @param position 左上为0,顺时针算起
  3. */
  4. private static Bitmap saveWaterMask(Context context, int position, Bitmap sourBitmap,String lng, String lat, String address, String stationName) {
  5. WaterMaskView waterMaskView = new WaterMaskView(context);
  6. RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
  7. waterMaskView.setLayoutParams(params);
  8. // waterMaskView.setCompanyText(address);
  9. // waterMaskView.setInfoText(stationName);
  10. String time = TimeUtils.getCurrentTimeInString();
  11. waterMaskView.setInfoDate(time);
  12. waterMaskView.setInfoAddress(address);
  13. waterMaskView.setInfoLat(lat);
  14. waterMaskView.setInfoLon(lng);
  15. waterMaskView.setInfoStationName(stationName);
  16. Bitmap waterBitmap = WaterMaskUtil.convertViewToBitmap(waterMaskView);
  17. //根据原图处理要生成的水印的宽高
  18. float width = sourBitmap.getWidth();
  19. float height = sourBitmap.getHeight();
  20. float be = width / height;
  21. if ((float) 16 / 9 >= be && be >= (float) 4 / 3) {
  22. //在图片比例区间内16;9~4:3内,将生成的水印bitmap设置为原图宽高各自的1/5
  23. waterBitmap = WaterMaskUtil.zoomBitmap(waterBitmap, (int) width / 5, (int) height / 5);
  24. } else if (be > (float) 16 / 9) {
  25. //生成4:3的水印
  26. waterBitmap = WaterMaskUtil.zoomBitmap(waterBitmap, (int) width / 5, (int) width * 3 / 20);
  27. } else if (be < (float) 4 / 3) {
  28. //生成4:3的水印
  29. waterBitmap = WaterMaskUtil.zoomBitmap(waterBitmap, (int) height * 4 / 15, (int) height / 5);
  30. }
  31. Bitmap watermarkBitmap = null;
  32. switch (position) {
  33. case 0:
  34. watermarkBitmap = WaterMaskUtil.createWaterMaskLeftTop(context, sourBitmap, waterBitmap, 0, 0);
  35. break;
  36. case 1:
  37. watermarkBitmap = WaterMaskUtil.createWaterMaskRightTop(context, sourBitmap, waterBitmap, 0, 0);
  38. break;
  39. case 2:
  40. watermarkBitmap = WaterMaskUtil.createWaterMaskRightBottom(context, sourBitmap, waterBitmap, 0, 0);
  41. break;
  42. case 3:
  43. watermarkBitmap = WaterMaskUtil.createWaterMaskLeftBottom(context, sourBitmap, waterBitmap, 0, 0);
  44. break;
  45. }
  46. return watermarkBitmap;
  47. }

3、将Bitmap保存到手机,获取文件URL,最后将文件上传到服务器

  1. //保存图片
  2. String fileName = "IMG_" + new Date().getTime() + RandomUtil.getRandom(10000) + ".png";
  3. String targetFileUrl = saveBitmap(context, WaterBitmap, fileName);
  1. private static String saveBitmap(Context context, Bitmap mBitmap, String name) {
  2. String appName = context.getResources().getString(R.string.app_name);
  3. String folder = Environment.getExternalStorageDirectory()
  4. + File.separator + appName + File.separator + "photo";
  5. LogUtils.i("folder=" + folder);
  6. File folderFile = new File(folder);
  7. if (!folderFile.exists()) {
  8. boolean isSuccess = folderFile.mkdirs();
  9. LogUtils.i("创建文件夹路径是否成功=" + isSuccess);
  10. }
  11. String target = folder + File.separator + name;
  12. LogUtils.i("target=" + target);
  13. File targetFile = new File(target);
  14. try {
  15. targetFile.createNewFile();
  16. } catch (IOException e) {
  17. LogUtils.e("在保存图片时出错:" + e.toString());
  18. }
  19. FileOutputStream fOut = null;
  20. try {
  21. fOut = new FileOutputStream(targetFile);
  22. } catch (FileNotFoundException e) {
  23. e.printStackTrace();
  24. }
  25. mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
  26. try {
  27. fOut.flush();
  28. } catch (IOException e) {
  29. e.printStackTrace();
  30. }
  31. try {
  32. fOut.close();
  33. } catch (IOException e) {
  34. e.printStackTrace();
  35. }
  36. return target;
  37. }

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

闽ICP备14008679号