Android-图片处理:改变大小,去色,圆角,合成

转自:http://zhanhao.iteye.com/blog/1276449

源码下载:http://vdisk.weibo.com/s/1fI6l

多张图片四个方位的图片合成,改变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. public class ImageTools  
  22. public static final int LEFT 0;  
  23. public static final int RIGHT 1;  
  24. public static final int TOP 3;  
  25. public static final int BOTTOM 4;  
  26.   
  27.   
  28. public static Bitmap toGrayscale(Bitmap bmpOriginal)  
  29. int width, height;  
  30. height bmpOriginal.getHeight();  
  31. width bmpOriginal.getWidth();  
  32. Bitmap bmpGrayscale Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);  
  33. Canvas new Canvas(bmpGrayscale);  
  34. Paint paint new Paint();  
  35. ColorMatrix cm new ColorMatrix();  
  36. cm.setSaturation(0);  
  37. ColorMatrixColorFilter new ColorMatrixColorFilter(cm);  
  38. paint.setColorFilter(f);  
  39. c.drawBitmap(bmpOriginal, 0, 0, paint);  
  40. return bmpGrayscale;  
  41.  
  42.   
  43.   
  44. public static Bitmap toGrayscale(Bitmap bmpOriginal, int pixels)  
  45. return toRoundCorner(toGrayscale(bmpOriginal), pixels);  
  46.  
  47.   
  48.   
  49. public static Bitmap toRoundCorner(Bitmap bitmap, int pixels)  
  50. Bitmap output Bitmap  
  51. .createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);  
  52. Canvas canvas new Canvas(output);  
  53. final int color 0xff424242;  
  54. final Paint paint new Paint();  
  55. final Rect rect new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());  
  56. final RectF rectF new RectF(rect);  
  57. final float roundPx pixels;  
  58. paint.setAntiAlias(true);  
  59. canvas.drawARGB(0, 0, 0, 0);  
  60. paint.setColor(color);  
  61. canvas.drawRoundRect(rectF, roundPx, roundPx, paint);  
  62. paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));  
  63. canvas.drawBitmap(bitmap, rect, rect, paint);  
  64. return output;  
  65.  
  66.   
  67.   
  68. public static BitmapDrawable toRoundCorner(BitmapDrawable bitmapDrawable, int pixels)  
  69. Bitmap bitmap bitmapDrawable.getBitmap();  
  70. bitmapDrawable new BitmapDrawable(toRoundCorner(bitmap, pixels));  
  71. return bitmapDrawable;  
  72.  
  73.   
  74. public static void saveBefore(String path)  
  75. BitmapFactory.Options options new BitmapFactory.Options();  
  76. options.inJustDecodeBounds true;  
  77. // 获取这个图片的宽和高  
  78. Bitmap bitmap BitmapFactory.decodeFile(path, options); // 此时返回bm为空  
  79. options.inJustDecodeBounds false;  
  80. // 计算缩放比  
  81. int be (int)(options.outHeight (float)200);  
  82. if (be <= 0)  
  83. be 1;  
  84. options.inSampleSize 2; // 图片长宽各缩小二分之一  
  85. // 重新读入图片,注意这次要把options.inJustDecodeBounds 设为 false哦  
  86. bitmap BitmapFactory.decodeFile(path, options);  
  87. int bitmap.getWidth();  
  88. int bitmap.getHeight();  
  89. System.out.println(w h);  
  90. // savePNG_After(bitmap,path);  
  91. saveJPGE_After(bitmap, path);  
  92.  
  93.   
  94. public static void savePNG_After(Bitmap bitmap, String name)  
  95. File file new File(name);  
  96. try  
  97. FileOutputStream out new FileOutputStream(file);  
  98. if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, out))  
  99. out.flush();  
  100. out.close();  
  101.  
  102. catch (FileNotFoundException e)  
  103. e.printStackTrace();  
  104. catch (IOException e)  
  105. e.printStackTrace();  
  106.  
  107.  
  108.   
  109. public static void saveJPGE_After(Bitmap bitmap, String path)  
  110. File file new File(path);  
  111. try  
  112. FileOutputStream out new FileOutputStream(file);  
  113. if (bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out))  
  114. out.flush();  
  115. out.close();  
  116.  
  117. catch (FileNotFoundException e)  
  118. e.printStackTrace();  
  119. catch (IOException e)  
  120. e.printStackTrace();  
  121.  
  122.  
  123.   
  124. public static Bitmap createBitmapForWatermark(Bitmap src, Bitmap watermark)  
  125. if (src == null)  
  126. return null;  
  127.  
  128. int src.getWidth();  
  129. int src.getHeight();  
  130. int ww watermark.getWidth();  
  131. int wh watermark.getHeight();  
  132. // create the new blank bitmap  
  133. Bitmap newb Bitmap.createBitmap(w, h, Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图  
  134. Canvas cv new Canvas(newb);  
  135. // draw src into  
  136. cv.drawBitmap(src, 0, 0, null);// 在 0,0坐标开始画入src  
  137. // draw watermark into  
  138. cv.drawBitmap(watermark, ww 5, wh 5, null);// 在src的右下角画入水印  
  139. // save all clip  
  140. cv.save(Canvas.ALL_SAVE_FLAG);// 保存  
  141. // store  
  142. cv.restore();// 存储  
  143. return newb;  
  144.  
  145.   
  146. public static Bitmap potoMix(int direction, Bitmap... bitmaps)  
  147. if (bitmaps.length <= 0)  
  148. return null;  
  149.  
  150. if (bitmaps.length == 1)  
  151. return bitmaps[0];  
  152.  
  153. Bitmap newBitmap bitmaps[0];  
  154. // newBitmap createBitmapForFotoMix(bitmaps[0],bitmaps[1],direction);  
  155. for (int 1; bitmaps.length; i++)  
  156. newBitmap createBitmapForFotoMix(newBitmap, bitmaps[i], direction);  
  157.  
  158. return newBitmap;  
  159.  
  160.   
  161. private static Bitmap createBitmapForFotoMix(Bitmap first, Bitmap second, int direction)  
  162. if (first == null)  
  163. return null;  
  164.  
  165. if (second == null)  
  166. return first;  
  167.   
  168. int fw first.getWidth();  
  169. int fh first.getHeight();  
  170. int sw second.getWidth();  
  171. int sh second.getHeight();  
  172. Bitmap newBitmap null;  
  173. if (direction == LEFT)  
  174. newBitmap Bitmap.createBitmap(fw sw, fh sh fh sh, Config.ARGB_8888);  
  175. Canvas canvas new Canvas(newBitmap);  
  176. canvas.drawBitmap(first, sw, 0, null);  
  177. canvas.drawBitmap(second, 0, 0, null);  
  178. else if (direction == RIGHT)  
  179. newBitmap Bitmap.createBitmap(fw sw, fh sh fh sh, Config.ARGB_8888);  
  180. Canvas canvas new Canvas(newBitmap);  
  181. canvas.drawBitmap(first, 0, 0, null);  
  182. canvas.drawBitmap(second, fw, 0, null);  
  183. else if (direction == TOP)  
  184. newBitmap Bitmap.createBitmap(sw fw sw fw, fh sh, Config.ARGB_8888);  
  185. Canvas canvas new Canvas(newBitmap);  
  186. canvas.drawBitmap(first, 0, sh, null);  
  187. canvas.drawBitmap(second, 0, 0, null);  
  188. else if (direction == BOTTOM)  
  189. newBitmap Bitmap.createBitmap(sw fw sw fw, fh sh, Config.ARGB_8888);  
  190. Canvas canvas new Canvas(newBitmap);  
  191. canvas.drawBitmap(first, 0, 0, null);  
  192. canvas.drawBitmap(second, 0, fh, null);  
  193.  
  194. return newBitmap;  
  195.  
  196.   
  197. public static Bitmap createBitmapBySize(Bitmap bitmap,int width,int height)  
  198.  
  199. return Bitmap.createScaledBitmap(bitmap, width, height, true);  
  200.  
  201.   
  202. public static Bitmap drawableToBitmapByBD(Drawable drawable)  
  203. BitmapDrawable bitmapDrawable (BitmapDrawable)drawable;  
  204. return bitmapDrawable.getBitmap();  
  205.  
  206.   
  207. public static Drawable bitmapToDrawableByBD(Bitmap bitmap)  
  208. Drawable drawable new BitmapDrawable(bitmap);  
  209. return drawable;  
  210.  
  211.   
  212. public static Bitmap bytesToBimap(byte[] b)  
  213. if (b.length != 0)  
  214. return BitmapFactory.decodeByteArray(b, 0, b.length);  
  215. else  
  216. return null;  
  217.  
  218.  
  219.   
  220. public static byte[] bitmapToBytes(Bitmap bm)  
  221. ByteArrayOutputStream baos new ByteArrayOutputStream();  
  222. bm.compress(Bitmap.CompressFormat.PNG, 100, baos);  
  223. return baos.toByteArray();  
  224.  
  225.  
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值