android 工具类之图片加工

  1     /** 
  2      * 图片加工厂 
  3      *  
  4      * @author way 
  5      *  
  6      */  
  7     public class ImageUtil {  
  8         /** 
  9          * 通过路径获取输入流 
 10          *  
 11          * @param path 
 12          *            路径 
 13          * @return 输入流 
 14          * @throws Exception 
 15          *             异常 
 16          */  
 17         public static InputStream getRequest(String path) throws Exception {  
 18             URL url = new URL(path);  
 19             HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
 20             conn.setRequestMethod("GET");  
 21             conn.setConnectTimeout(5000);  
 22             if (conn.getResponseCode() == 200) {  
 23                 return conn.getInputStream();  
 24             }  
 25             return null;  
 26         }  
 27       
 28         /** 
 29          * 从流中读取二进制数据 
 30          *  
 31          * @param inStream 
 32          *            输入流 
 33          * @return 二进制数据 
 34          * @throws Exception 
 35          *             异常 
 36          */  
 37         public static byte[] readInputStream(InputStream inStream) throws Exception {  
 38             ByteArrayOutputStream outSteam = new ByteArrayOutputStream();  
 39             byte[] buffer = new byte[4096];  
 40             int len = 0;  
 41             while ((len = inStream.read(buffer)) != -1) {  
 42                 outSteam.write(buffer, 0, len);  
 43             }  
 44             outSteam.close();  
 45             inStream.close();  
 46             return outSteam.toByteArray();  
 47         }  
 48       
 49         /** 
 50          * 把一个路径转换成Drawable对象 
 51          *  
 52          * @param url 
 53          *            路径 
 54          * @return Drawable对象 
 55          */  
 56         public static Drawable loadImageFromUrl(String url) {  
 57             URL m;  
 58             InputStream i = null;  
 59             try {  
 60                 m = new URL(url);  
 61                 i = (InputStream) m.getContent();  
 62             } catch (MalformedURLException e1) {  
 63                 e1.printStackTrace();  
 64             } catch (IOException e) {  
 65                 e.printStackTrace();  
 66             }  
 67             Drawable d = Drawable.createFromStream(i, "src");  
 68             return d;  
 69         }  
 70       
 71         /** 
 72          * 把一个路径转换成Drawable对象 
 73          *  
 74          * @param url 
 75          *            字符串路径 
 76          * @return Drawable对象 
 77          * @throws Exception 
 78          *             异常 
 79          */  
 80         public static Drawable getDrawableFromUrl(String url) throws Exception {  
 81             return Drawable.createFromStream(getRequest(url), null);  
 82         }  
 83       
 84         /** 
 85          * 从路径中得到位图 
 86          *  
 87          * @param url 
 88          *            字符串路径 
 89          * @return 位图 
 90          * @throws Exception 
 91          *             异常 
 92          */  
 93         public static Bitmap getBitmapFromUrl(String url) throws Exception {  
 94             byte[] bytes = getBytesFromUrl(url);  
 95             return byteToBitmap(bytes);  
 96         }  
 97       
 98         /** 
 99          * 从路径中得到圆角位图 
100          *  
101          * @param url 
102          *            字符串路径 
103          * @param pixels 
104          *            圆角弧度 
105          * @return 圆角位图 
106          * @throws Exception 
107          *             异常 
108          */  
109         public static Bitmap getRoundBitmapFromUrl(String url, int pixels)  
110                 throws Exception {  
111             byte[] bytes = getBytesFromUrl(url);  
112             Bitmap bitmap = byteToBitmap(bytes);  
113             return toRoundCorner(bitmap, pixels);  
114         }  
115       
116         /** 
117          * 从路径中得到圆角Drawable对象 
118          *  
119          * @param url 
120          *            字符串路径 
121          * @param pixels 
122          *            圆角弧度 
123          * @return 圆角Drawable对象 
124          * @throws Exception 
125          *             异常 
126          */  
127         public static Drawable geRoundDrawableFromUrl(String url, int pixels)  
128                 throws Exception {  
129             byte[] bytes = getBytesFromUrl(url);  
130             BitmapDrawable bitmapDrawable = (BitmapDrawable) byteToDrawable(bytes);  
131             return toRoundCorner(bitmapDrawable, pixels);  
132         }  
133       
134         /** 
135          * 从路径中得到二进制数据 
136          *  
137          * @param url 
138          *            字符串路径 
139          * @return 二进制数据 
140          * @throws Exception 
141          *             异常 
142          */  
143         public static byte[] getBytesFromUrl(String url) throws Exception {  
144             return readInputStream(getRequest(url));  
145         }  
146       
147         /** 
148          * 从二进制数据中得到位图 
149          *  
150          * @param byteArray 
151          *            二进制数据 
152          * @return 位图 
153          */  
154         public static Bitmap byteToBitmap(byte[] byteArray) {  
155             if (byteArray.length != 0) {  
156                 return BitmapFactory  
157                         .decodeByteArray(byteArray, 0, byteArray.length);  
158             } else {  
159                 return null;  
160             }  
161         }  
162       
163         /** 
164          * 从二进制数据中得到Drawable对象 
165          *  
166          * @param byteArray 
167          *            二进制数据 
168          * @return Drawable对象 
169          */  
170         public static Drawable byteToDrawable(byte[] byteArray) {  
171             ByteArrayInputStream ins = new ByteArrayInputStream(byteArray);  
172             return Drawable.createFromStream(ins, null);  
173         }  
174       
175         /** 
176          * 把位图转换称二进制数据 
177          *  
178          * @param bm 
179          *            位图 
180          * @return 二进制数据 
181          */  
182         public static byte[] Bitmap2Bytes(Bitmap bm) {  
183             ByteArrayOutputStream baos = new ByteArrayOutputStream();  
184             bm.compress(Bitmap.CompressFormat.PNG, 100, baos);  
185             return baos.toByteArray();  
186         }  
187       
188         /** 
189          * 把Drawable对象转换称位图 
190          *  
191          * @param drawable 
192          *            Drawable对象 
193          * @return 位图 
194          */  
195         public static Bitmap drawableToBitmap(Drawable drawable) {  
196       
197             Bitmap bitmap = Bitmap  
198                     .createBitmap(  
199                             drawable.getIntrinsicWidth(),  
200                             drawable.getIntrinsicHeight(),  
201                             drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888  
202                                     : Bitmap.Config.RGB_565);  
203             Canvas canvas = new Canvas(bitmap);  
204             drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),  
205                     drawable.getIntrinsicHeight());  
206             drawable.draw(canvas);  
207             return bitmap;  
208         }  
209       
210         /** 
211          * 图片去色,返回灰度图片 
212          *  
213          * @param bmpOriginal 
214          *            传入的图片 
215          * @return 去色后的图片 
216          */  
217         public static Bitmap toGrayscale(Bitmap bmpOriginal) {  
218             int width, height;  
219             height = bmpOriginal.getHeight();  
220             width = bmpOriginal.getWidth();  
221       
222             Bitmap bmpGrayscale = Bitmap.createBitmap(width, height,  
223                     Bitmap.Config.RGB_565);  
224             Canvas c = new Canvas(bmpGrayscale);  
225             Paint paint = new Paint();  
226             ColorMatrix cm = new ColorMatrix();  
227             cm.setSaturation(0);  
228             ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);  
229             paint.setColorFilter(f);  
230             c.drawBitmap(bmpOriginal, 0, 0, paint);  
231             return bmpGrayscale;  
232         }  
233       
234         /** 
235          * 去色同时加圆角 
236          *  
237          * @param bmpOriginal 
238          *            原图 
239          * @param pixels 
240          *            圆角弧度 
241          * @return 修改后的图片 
242          */  
243         public static Bitmap toGrayscale(Bitmap bmpOriginal, int pixels) {  
244             return toRoundCorner(toGrayscale(bmpOriginal), pixels);  
245         }  
246       
247         /** 
248          * 把位图变成圆角位图 
249          *  
250          * @param bitmap 
251          *            需要修改的位图 
252          * @param pixels 
253          *            圆角的弧度 
254          * @return 圆角位图 
255          */  
256         public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {  
257       
258             Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),  
259                     bitmap.getHeight(), Config.ARGB_8888);  
260             Canvas canvas = new Canvas(output);  
261       
262             final int color = 0xff424242;  
263             final Paint paint = new Paint();  
264             final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());  
265             final RectF rectF = new RectF(rect);  
266             final float roundPx = pixels;  
267       
268             paint.setAntiAlias(true);  
269             canvas.drawARGB(0, 0, 0, 0);  
270             paint.setColor(color);  
271             canvas.drawRoundRect(rectF, roundPx, roundPx, paint);  
272       
273             paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));  
274             canvas.drawBitmap(bitmap, rect, rect, paint);  
275       
276             return output;  
277         }  
278       
279         /** 
280          * 将BitampDrawable转换成圆角的BitampDrawable 
281          *  
282          * @param bitmapDrawable 
283          *            原生BitampDrawable对象 
284          * @param pixels 
285          *            圆角弧度 
286          * @return 圆角BitampDrawable对象 
287          */  
288         public static BitmapDrawable toRoundCorner(BitmapDrawable bitmapDrawable,  
289                 int pixels) {  
290             Bitmap bitmap = bitmapDrawable.getBitmap();  
291             bitmapDrawable = new BitmapDrawable(toRoundCorner(bitmap, pixels));  
292             return bitmapDrawable;  
293         }  
294       
295         /** 
296          * 图片水印生成的方法 
297          *  
298          * @param src 
299          *            源图片位图 
300          * @param watermark 
301          *            水印图片位图 
302          * @return 返回一个加了水印的图片 
303          */  
304         public static Bitmap createBitmap(Bitmap src, Bitmap watermark) {  
305             if (src == null)  
306                 return null;  
307             int w = src.getWidth();  
308             int h = src.getHeight();  
309             int ww = watermark.getWidth();  
310             int wh = watermark.getHeight();  
311             Bitmap newb = Bitmap.createBitmap(w, h, Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图  
312             Canvas cv = new Canvas(newb);// 初始化画布  
313             cv.drawBitmap(src, 0, 0, null);// 在 0,0坐标开始画入src  
314             cv.drawBitmap(watermark, w - ww + 5, h - wh + 5, null);// 在src的右下角画入水印  
315             cv.save(Canvas.ALL_SAVE_FLAG);// 保存,用来保存Canvas的状态。save之后,可以调用Canvas的平移、放缩、旋转、错切、裁剪等操作。  
316             cv.restore();// 存储,用来恢复Canvas之前保存的状态。防止save后对Canvas执行的操作对后续的绘制有影响。  
317             return newb;  
318         }  
319     }  
原文地址:https://www.cnblogs.com/liangstudyhome/p/3857571.html