The picture tool class, which can be used for conversion between bitmap, byte array, drawable, and image scaling, is currently weak and will be enhanced later. such as:
bitmaptodrawable (Bitmap b) bimap convert to drawable
Drawabletobitmap (drawable D) drawable convert to bitmap
Drawabletobyte (drawable D) drawable convert to byte
Scaleimage (Bitmap org, float scalewidth, float scaleheight) zoom picture
Package Cn.trinea.android.common.util;import Java.io.bytearrayoutputstream;import Java.io.ioexception;import Java.io.inputstream;import Java.net.httpurlconnection;import Java.net.malformedurlexception;import Java.net.URL; Import Java.util.map;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.matrix;import Android.graphics.drawable.bitmapdrawable;import android.graphics.drawable.drawable;/** * imageutils * <ul> * convert between Bitmap, byte array, drawable * <LI&G T {@link #bitmapToByte (Bitmap)}</li> * <li>{@link #bitmapToDrawable (Bitmap)}</li> * <li>{@link #byteToBitmap (byte[])}</li> * <li>{@link #byteToDrawable (byte[])}</li> * <li>{@link # Drawabletobitmap (drawable)}</li> * <li>{@link #drawableToByte (drawable)}</li> * </ul> * < ul> * Get image * <li>{@link #getInputStreamFromUrl (String, int)}</li> * <li>{@link # Getbitmapfromurl (String, int)}</li> * <li>{@link #getDrawableFromUrl (String, int)}</li> * </ul> * <ul> * Scale image * < li>{@link #scaleImageTo (Bitmap, int, int)}</li> * <li>{@link #scaleImage (Bitmap, float, float)}</li > * </ul> * * @author <a href= "http://www.trinea.cn" target= "_blank" >Trinea</a> 2012-6-27 */public C Lass Imageutils {private imageutils () {throw new Assertionerror (); }/** * Convert Bitmap to byte array * * @param b * @return */public static byte[] Bitmaptobyt E (Bitmap b) {if (b = = null) {return null; } bytearrayoutputstream o = new Bytearrayoutputstream (); B.compress (Bitmap.CompressFormat.PNG, N, O); return O.tobytearray (); }/** * Convert byte array to Bitmap * * @param b * @return */public static Bitmap Bytetobitma P (byte[] b) {return (b = = NULL | | b.length = = 0)? null:BitmapFactory.decodeByteArRay (b, 0, b.length); }/** * Convert drawable to Bitmap * * @param d * @return */public static Bitmap Drawabletobit Map (drawable D) {return d = = null? Null: ((bitmapdrawable) d). Getbitmap (); }/** * Convert Bitmap to drawable * * @param b * @return */public static drawable Bitmaptodra Wable (Bitmap b) {return b = = null? null:new bitmapdrawable (b); }/** * Convert drawable to byte array * * @param d * @return */public static byte[] Drawablet Obyte (drawable D) {return Bitmaptobyte (Drawabletobitmap (d)); }/** * Convert byte array to drawable * * @param b * @return */public static drawable Bytetod Rawable (byte[] b) {return bitmaptodrawable (Bytetobitmap (b)); }/** * Get input stream from the network by ImageUrl, the need to close inputstream yourself * * @param image URL * @param readtimeoutmillis * @return * @see Imageutils#getinputstreamfromurl (String, int, Boolean) */public static InputStream Getinputstreamfromurl (string IMAGEURL, int readtimeoutmillis) {return Getinputstreamfromurl (IMAGEURL, readtimeoutmillis, NULL); }/** * Get input stream from the network by ImageUrl, the need to close inputstream yourself * * @param image URL * @param readtimeoutmillis Read Time out, if less than 0, no set, in Mills * @param requestproperties http RE Quest Properties * @return * @throws malformedurlexception * @throws IOException */public static Input Stream Getinputstreamfromurl (String imageUrl, int readtimeoutmillis, map<string, string> Requestpropertie s) {InputStream stream = null; try {URL url = new URL (imageUrl); HttpURLConnection con = (httpurlconnection) url.openconnection (); Httputils.seturlconnection (requestproperties, con); if (Readtimeoutmillis > 0) { Con.setreadtimeout (Readtimeoutmillis); stream = Con.getinputstream (); } catch (Malformedurlexception e) {closeinputstream (stream); throw new RuntimeException ("malformedurlexception occurred.", e); } catch (IOException e) {closeinputstream (stream); throw new RuntimeException ("IOException occurred.", e); } return stream; }/** * Get drawable by IMAGEURL * * @param imageUrl * @param readtimeoutmillis * @return * @s EE imageutils#getdrawablefromurl (String, int, Boolean) */public static drawable Getdrawablefromurl (String imageUrl , int readtimeoutmillis) {return Getdrawablefromurl (IMAGEURL, readtimeoutmillis, NULL); }/** * Get drawable by IMAGEURL * * @param imageUrl * @param readtimeoutmillis "read Time Out" if less than 0, not set, in Mills * @param requestproperties HTTP Request Properties * @return */ public static drawable Getdrawablefromurl (String imageUrl, int readtimeoutmillis, map<string, string> requestproperties) {InputStream stream = Getinputstreamfromurl (ImageUrl, Readtimeoutmillis, requestproperties); drawable d = drawable.createfromstream (stream, "src"); Closeinputstream (stream); return D; }/** * Get Bitmap by IMAGEURL * * @param imageUrl * @param readtimeout * @return * @see Image Utils#getbitmapfromurl (String, int, Boolean) */public static Bitmap Getbitmapfromurl (string imageUrl, int readtime Out) {return Getbitmapfromurl (IMAGEURL, readtimeout, NULL); }/** * Get Bitmap by IMAGEURL * * @param imageUrl * @param requestproperties HTTP request Properties * @return */public static Bitmap Getbitmapfromurl (String imageUrl, int readtimeout, map<string, string> requestproperties) {InputStream stream = Getinputstreamfromurl (IMAGEURL, reAdtimeout, requestproperties); Bitmap B = Bitmapfactory.decodestream (stream); Closeinputstream (stream); return b; }/** * Scale image * * @param org * @param newwidth * @param newheight * @return */PU Blic static Bitmap Scaleimageto (Bitmap org, int newwidth, int newheight) {return scaleimage (org, (float) newwidth/ Org.getwidth (), (float) newheight/org.getheight ()); }/** * Scale image * * @param org * @param scalewidth sacle of width * @param scaleheight scale O F Height * @return */public static Bitmap scaleimage (Bitmap org, float scalewidth, float scaleheight) { if (org = = null) {return null; } Matrix Matrix = new Matrix (); Matrix.postscale (ScaleWidth, ScaleHeight); return bitmap.createbitmap (org, 0, 0, org.getwidth (), Org.getheight (), Matrix, True); }/** * Close InputStream * * @param s */private static VOID Closeinputstream (InputStream s) {if (s = = null) {return; } try {s.close (); } catch (IOException e) {throw new RuntimeException ("IOException occurred.", e); } }}
Android Image processing Tool class