歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android中圖片的處理(放大縮小,去色,轉換格式,增加水印等)

Android中圖片的處理(放大縮小,去色,轉換格式,增加水印等)

日期:2017/3/1 10:55:53   编辑:Linux編程
Android中圖片的處理(放大縮小,去色,轉換格式,增加水印等),多張圖片四個方位的圖片合成,改變bitmap大小,圖片去色等功能



Java代碼
  1. package com.dzh.operateimage;
  2. import android.graphics.Bitmap;
  3. import android.graphics.Bitmap.Config;
  4. import android.graphics.BitmapFactory;
  5. import android.graphics.Canvas;
  6. import android.graphics.ColorMatrix;
  7. import android.graphics.ColorMatrixColorFilter;
  8. import android.graphics.Paint;
  9. import android.graphics.PorterDuff.Mode;
  10. import android.graphics.PorterDuffXfermode;
  11. import android.graphics.Rect;
  12. import android.graphics.RectF;
  13. import android.graphics.drawable.BitmapDrawable;
  14. import android.graphics.drawable.Drawable;
  15. import java.io.ByteArrayOutputStream;
  16. import java.io.File;
  17. import java.io.FileNotFoundException;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. /**
  21. * 處理圖片的工具類.
  22. */
  23. public class ImageTools {
  24. public static final int LEFT = 0;
  25. public static final int RIGHT = 1;
  26. public static final int TOP = 3;
  27. public static final int BOTTOM = 4;
  28. /** */
  29. /**
  30. * 圖片去色,返回灰度圖片
  31. *
  32. * @param bmpOriginal 傳入的圖片
  33. * @return 去色後的圖片
  34. */
  35. public static Bitmap toGrayscale(Bitmap bmpOriginal) {
  36. int width, height;
  37. height = bmpOriginal.getHeight();
  38. width = bmpOriginal.getWidth();
  39. Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
  40. Canvas c = new Canvas(bmpGrayscale);
  41. Paint paint = new Paint();
  42. ColorMatrix cm = new ColorMatrix();
  43. cm.setSaturation(0);
  44. ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
  45. paint.setColorFilter(f);
  46. c.drawBitmap(bmpOriginal, 0, 0, paint);
  47. return bmpGrayscale;
  48. }
  49. /** */
  50. /**
  51. * 去色同時加圓角
  52. *
  53. * @param bmpOriginal 原圖
  54. * @param pixels 圓角弧度
  55. * @return 修改後的圖片
  56. */
  57. public static Bitmap toGrayscale(Bitmap bmpOriginal, int pixels) {
  58. return toRoundCorner(toGrayscale(bmpOriginal), pixels);
  59. }
  60. /** */
  61. /**
  62. * 把圖片變成圓角
  63. *
  64. * @param bitmap 需要修改的圖片
  65. * @param pixels 圓角的弧度
  66. * @return 圓角圖片
  67. */
  68. public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {
  69. Bitmap output = Bitmap
  70. .createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
  71. Canvas canvas = new Canvas(output);
  72. final int color = 0xff424242;
  73. final Paint paint = new Paint();
  74. final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
  75. final RectF rectF = new RectF(rect);
  76. final float roundPx = pixels;
  77. paint.setAntiAlias(true);
  78. canvas.drawARGB(0, 0, 0, 0);
  79. paint.setColor(color);
  80. canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
  81. paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
  82. canvas.drawBitmap(bitmap, rect, rect, paint);
  83. return output;
  84. }
  85. /** */
  86. /**
  87. * 使圓角功能支持BitampDrawable
  88. *
  89. * @param bitmapDrawable
  90. * @param pixels
  91. * @return
  92. */
  93. public static BitmapDrawable toRoundCorner(BitmapDrawable bitmapDrawable, int pixels) {
  94. Bitmap bitmap = bitmapDrawable.getBitmap();
  95. bitmapDrawable = new BitmapDrawable(toRoundCorner(bitmap, pixels));
  96. return bitmapDrawable;
  97. }
  98. /**
  99. * 讀取路徑中的圖片,然後將其轉化為縮放後的bitmap
  100. *
  101. * @param path
  102. */
  103. public static void saveBefore(String path) {
  104. BitmapFactory.Options options = new BitmapFactory.Options();
  105. options.inJustDecodeBounds = true;
  106. // 獲取這個圖片的寬和高
  107. Bitmap bitmap = BitmapFactory.decodeFile(path, options); // 此時返回bm為空
  108. options.inJustDecodeBounds = false;
  109. // 計算縮放比
  110. int be = (int)(options.outHeight / (float)200);
  111. if (be <= 0)
  112. be = 1;
  113. options.inSampleSize = 2; // 圖片長寬各縮小二分之一
  114. // 重新讀入圖片,注意這次要把options.inJustDecodeBounds 設為 false哦
  115. bitmap = BitmapFactory.decodeFile(path, options);
  116. int w = bitmap.getWidth();
  117. int h = bitmap.getHeight();
  118. System.out.println(w + " " + h);
  119. // savePNG_After(bitmap,path);
  120. saveJPGE_After(bitmap, path);
  121. }
  122. /**
  123. * 保存圖片為PNG
  124. *
  125. * @param bitmap
  126. * @param name
  127. */
  128. public static void savePNG_After(Bitmap bitmap, String name) {
  129. File file = new File(name);
  130. try {
  131. FileOutputStream out = new FileOutputStream(file);
  132. if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)) {
  133. out.flush();
  134. out.close();
  135. }
  136. } catch (FileNotFoundException e) {
  137. e.printStackTrace();
  138. } catch (IOException e) {
  139. e.printStackTrace();
  140. }
  141. }
  142. /**
  143. * 保存圖片為JPEG
  144. *
  145. * @param bitmap
  146. * @param path
  147. */
  148. public static void saveJPGE_After(Bitmap bitmap, String path) {
  149. File file = new File(path);
  150. try {
  151. FileOutputStream out = new FileOutputStream(file);
  152. if (bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)) {
  153. out.flush();
  154. out.close();
  155. }
  156. } catch (FileNotFoundException e) {
  157. e.printStackTrace();
  158. } catch (IOException e) {
  159. e.printStackTrace();
  160. }
  161. }
  162. /**
  163. * 水印
  164. *
  165. * @param bitmap
  166. * @return
  167. */
  168. public static Bitmap createBitmapForWatermark(Bitmap src, Bitmap watermark) {
  169. if (src == null) {
  170. return null;
  171. }
  172. int w = src.getWidth();
  173. int h = src.getHeight();
  174. int ww = watermark.getWidth();
  175. int wh = watermark.getHeight();
  176. // create the new blank bitmap
  177. Bitmap newb = Bitmap.createBitmap(w, h, Config.ARGB_8888);// 創建一個新的和SRC長度寬度一樣的位圖
  178. Canvas cv = new Canvas(newb);
  179. // draw src into
  180. cv.drawBitmap(src, 0, 0, null);// 在 0,0坐標開始畫入src
  181. // draw watermark into
  182. cv.drawBitmap(watermark, w - ww + 5, h - wh + 5, null);// 在src的右下角畫入水印
  183. // save all clip
  184. cv.save(Canvas.ALL_SAVE_FLAG);// 保存
  185. // store
  186. cv.restore();// 存儲
  187. return newb;
  188. }
  189. /**
  190. * 圖片合成
  191. *
  192. * @return
  193. */
  194. public static Bitmap potoMix(int direction, Bitmap... bitmaps) {
  195. if (bitmaps.length <= 0) {
  196. return null;
  197. }
  198. if (bitmaps.length == 1) {
  199. return bitmaps[0];
  200. }
  201. Bitmap newBitmap = bitmaps[0];
  202. // newBitmap = createBitmapForFotoMix(bitmaps[0],bitmaps[1],direction);
  203. for (int i = 1; i < bitmaps.length; i++) {
  204. newBitmap = createBitmapForFotoMix(newBitmap, bitmaps[i], direction);
  205. }
  206. return newBitmap;
  207. }
  208. private static Bitmap createBitmapForFotoMix(Bitmap first, Bitmap second, int direction) {
  209. if (first == null) {
  210. return null;
  211. }
  212. if (second == null) {
  213. return first;
  214. }
  215. int fw = first.getWidth();
  216. int fh = first.getHeight();
  217. int sw = second.getWidth();
  218. int sh = second.getHeight();
  219. Bitmap newBitmap = null;
  220. if (direction == LEFT) {
  221. newBitmap = Bitmap.createBitmap(fw + sw, fh > sh ? fh : sh, Config.ARGB_8888);
  222. Canvas canvas = new Canvas(newBitmap);
  223. canvas.drawBitmap(first, sw, 0, null);
  224. canvas.drawBitmap(second, 0, 0, null);
  225. } else if (direction == RIGHT) {
  226. newBitmap = Bitmap.createBitmap(fw + sw, fh > sh ? fh : sh, Config.ARGB_8888);
  227. Canvas canvas = new Canvas(newBitmap);
  228. canvas.drawBitmap(first, 0, 0, null);
  229. canvas.drawBitmap(second, fw, 0, null);
  230. } else if (direction == TOP) {
  231. newBitmap = Bitmap.createBitmap(sw > fw ? sw : fw, fh + sh, Config.ARGB_8888);
  232. Canvas canvas = new Canvas(newBitmap);
  233. canvas.drawBitmap(first, 0, sh, null);
  234. canvas.drawBitmap(second, 0, 0, null);
  235. } else if (direction == BOTTOM) {
  236. newBitmap = Bitmap.createBitmap(sw > fw ? sw : fw, fh + sh, Config.ARGB_8888);
  237. Canvas canvas = new Canvas(newBitmap);
  238. canvas.drawBitmap(first, 0, 0, null);
  239. canvas.drawBitmap(second, 0, fh, null);
  240. }
  241. return newBitmap;
  242. }
  243. /**
  244. * 將Bitmap轉換成指定大小
  245. * @param bitmap
  246. * @param width
  247. * @param height
  248. * @return
  249. */
  250. public static Bitmap createBitmapBySize(Bitmap bitmap,int width,int height)
  251. {
  252. return Bitmap.createScaledBitmap(bitmap, width, height, true);
  253. }
  254. /**
  255. * Drawable 轉 Bitmap
  256. *
  257. * @param drawable
  258. * @return
  259. */
  260. public static Bitmap drawableToBitmapByBD(Drawable drawable) {
  261. BitmapDrawable bitmapDrawable = (BitmapDrawable)drawable;
  262. return bitmapDrawable.getBitmap();
  263. }
  264. /**
  265. * Bitmap 轉 Drawable
  266. *
  267. * @param bitmap
  268. * @return
  269. */
  270. public static Drawable bitmapToDrawableByBD(Bitmap bitmap) {
  271. Drawable drawable = new BitmapDrawable(bitmap);
  272. return drawable;
  273. }
  274. /**
  275. * byte[] 轉 bitmap
  276. *
  277. * @param b
  278. * @return
  279. */
  280. public static Bitmap bytesToBimap(byte[] b) {
  281. if (b.length != 0) {
  282. return BitmapFactory.decodeByteArray(b, 0, b.length);
  283. } else {
  284. return null;
  285. }
  286. }
  287. /**
  288. * bitmap 轉 byte[]
  289. *
  290. * @param bm
  291. * @return
  292. */
  293. public static byte[] bitmapToBytes(Bitmap bm) {
  294. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  295. bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
  296. return baos.toByteArray();
  297. }
  298. }
Copyright © Linux教程網 All Rights Reserved