Common bitmap Processing Methods in Android
A lot of bitmap-related processing methods have been collected, and almost all of them are applied to projects!
Package com. tmacsky. utils; import java. io. byteArrayOutputStream; import java. io. IOException; import android. content. context; import android. content. res. resources; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. graphics. canvas; import android. graphics. matrix; import android. graphics. paint; import android. graphics. pixelFormat; import android. graphics. porterduxfermode; imp Ort android. graphics. rect; import android. graphics. rectF; import android. graphics. bitmap. config; import android. graphics. porterDuff. mode; import android. graphics. drawable. bitmapDrawable; import android. graphics. drawable. drawable; import android. view. view; import android. view. view. measureSpec; public class ImageUtils {// ---> bitmap related // reference website http://www.cnblogs.com/fighter/archive/2012/02/20/android-bitmap-draw Able.html // see blog: Convert * View to bitmap * @ param view * @ return */public static Bitmap convertViewToBitmap (View view) {view. setDrawingCacheEnabled (true); view. measure (MeasureSpec. makeMeasureSpec (0, MeasureSpec. UNSPECIFIED), MeasureSpec. makeMeasureSpec (0, MeasureSpec. UNSPECIFIED); view. layout (0, 0, view. getMeasuredWidth (), view. getMeasuredHeight (); view. BuildDrawingCache (); return view. getDrawingCache ();} /*** scale Drawable * @ param drawable * @ param w the width required after zooming * @ param h the height required after zoomDrawable * @ return */public static Drawable zoomDrawable (Drawable drawable, int w, int h) {int width = drawable. getIntrinsicWidth (); int height = drawable. getIntrinsicHeight (); // drawable is converted to bitmap Bitmap oldbmp = drawableToBitmap (drawable); // The Matrix object Matrix = new used to create an operation Image Matrix (); // calculate the zoom ratio float sx = (float) w/width); float sy = (float) h/height); // set the zoom ratio matrix. postScale (sx, sy); // create a new bitmap, whose content is bitmap newbmp = Bitmap after the original Bitmap is scaled. createBitmap (oldbmp, 0, 0, width, height, matrix, true); return new BitmapDrawable (newbmp );} /*** scale bitmap * @ param oldBitmap input bitmap * @ param newWidth * @ param newHeight * @ return */public static Bitmap zoomBitmap (Bitmap OldBitmap, int newWidth, int newHeight) {// obtain the width and height of the image. int width = oldBitmap. getWidth (); int height = oldBitmap. getHeight (); // calculate the scaling ratio float scaleWidth = (float) newWidth)/width; float scaleHeight = (float) newHeight)/height; // obtain the matrix Parameter Matrix = new matrix (); Matrix. postScale (scaleWidth, scaleHeight); // obtain the new image Bitmap newbm = Bitmap. createBitmap (oldBitmap, 0, 0, width, height, matrix, true ); Return newbm ;} /*** zoomBitmap * @ param img * @ param newWidth * @ param newHeight * @ return */public static Bitmap zoomImg (String img, int newWidth, int newHeight) {// image source Bitmap bm = BitmapFactory. decodeFile (img); if (null! = Bm) {return zoomBitmap (bm, newWidth, newHeight);} return null ;} /*** zoomBitmap * @ param context * @ param img * @ param newWidth * @ param newHeight * @ return */public static Bitmap zoomImg (Context context, string img, int newWidth, int newHeight) {// image source try {Bitmap bm = BitmapFactory. decodeStream (context. getAssets (). open (img); if (null! = Bm) {return zoomBitmap (bm, newWidth, newHeight) ;}} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();} return null;}/*** determines whether bitmap exists * @ param bitmap * @ return */public static boolean bitmapAvailable (Bitmap bitmap) {return bitmap! = Null & bitmap. getWidth ()> 0 & bitmap. getHeight ()> 0;}/*** drawable into bitmap * @ param drawable * @ return */public static Bitmap drawableToBitmap (Drawable drawable) {// obtain the length and width of drawable int w = drawable. getIntrinsicWidth (); int h = drawable. getIntrinsicHeight (); // retrieves the drawable color format Bitmap. config config = drawable. getOpacity ()! = PixelFormat. OPAQUE? Bitmap. config. ARGB_8888: Bitmap. config. RGB_565; // create the corresponding bitmap Bitmap bitmap = Bitmap. createBitmap (w, h, config); // create the Canvas canvas Canvas = new Canvas (bitmap); drawable. setBounds (0, 0, w, h); // draw the drawable content into the canvas. draw (canvas); return bitmap;}/*** converts Bitmap to Drawable * @ param context * @ param bitmap * @ return */public static Drawable bitmapToDrawable (Context context, Bitmap bitma P) {// because BtimapDrawable is a subclass of Drawable, you can directly use the bd object. BitmapDrawable bd = new BitmapDrawable (context. getResources (), bitmap); return bd;}/*** obtain Bitmap * @ param context * @ param req R from the resource. drawable. icon (eg .) * @ return */public Bitmap getBitmapFromResources (Context context, int req) {Resources res = context. getResources (); Bitmap bmp = BitmapFactory. decodeResource (res, req); return bmp;}/*** Byte []-> Bitmap conversion */public Bitmap Bytes2Bimap (byte [] B) {if (B. length! = 0) {return BitmapFactory. decodeByteArray (B, 0, B. length) ;}else {return null ;}} /*** Bitmap-> Byte [] conversion * @ param bm * @ return */public byte [] Bitmap2Bytes (Bitmap bm) {ByteArrayOutputStream baos = new ByteArrayOutputStream (); bm. compress (Bitmap. compressFormat. PNG, 100, baos); return baos. toByteArray ();}/*** obtain the rounded corner image * @ param bitmap * @ param roundPx radian * @ return */public static Bitmap getRoundedCornerBitmap (Bitmap bitmap, float roundPx) {int w = bitmap. getWidth (); int h = bitmap. getHeight (); Bitmap output = Bitmap. createBitmap (w, h, Config. ARGB_8888); Canvas canvas = new Canvas (output); final int color = 0xff0000242; final Paint paint = new Paint (); final Rect rect = new Rect (0, 0, w, h); final RectF rectF = new RectF (rect); paint. setAntiAlias (true); canvas. drawARGB (0, 0, 0, 0); paint. setColor (color); canvas. drawRoundRect (rectF, roundPx, roundPx, paint); paint. setXfermode (new porterduduxfermode (Mode. SRC_IN); canvas. drawBitmap (bitmap, rect, rect, paint); return output ;}}