Import Java. io. bytearrayoutputstream; import Java. io. file; import Java. io. filenotfoundexception; import Java. io. fileoutputstream; import Java. io. ioexception; import android. graphics. bitmap; import android. graphics. bitmapfactory; import android. graphics. canvas; import android. graphics. colormatrix; import android. graphics. colormatrixcolorfilter; import android. graphics. paint; import android. graphics. porterdudu x Fermode; import 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;/*** image processing tool class. */public class imagetool {public static final int left = 0; public static final int right = 1; public static final int Top = 3; public static final Int Bottom = 4;/***** // **** de-color the image, return the grayscale image ** @ Param BMP original * the image that is passed in * @ return after color removal */public static bitmap tograyscale (bitmap BMP original) {int width, height; Height = BMP original. getheight (); width = BMP original. getwidth (); bitmap BMP grayscale = bitmap. createbitmap (width, height, bitmap. config. rgb_565); canvas c = new canvas (BMP grayscale); paint = new paint (); colormatrix CM = new CO Lormatrix (); cm. setsaturation (0); colormatrixcolorfilter F = new colormatrixcolorfilter (CM); paint. setcolorfilter (f); C. drawbitmap (BMP original, 0, 0, paint); Return BMP grayscale ;} /*** // *** remove the color and add the rounded corner ** @ Param BMP original * Source image * @ Param pixels * rounded corner radians * @ return Modified Image */public static bitmap tograyscale (bitmap BMP original, int pixels) {return toroundcorner (tograyscale (BMP original), pixels );}/* ** // ** Convert the image to a rounded corner ** @ Param bitmap * the image to be modified * @ Param pixels * The radian of the rounded corner * @ return rounded corner image */public static bitmap toroundcorner (Bitmap bitmap, int pixels) {bitmap output = bitmap. createbitmap (bitmap. getwidth (), bitmap. getheight (), config. argb_8888); canvas = new canvas (output); Final int color = 0xff0000242; Final paint = new paint (); Final rect = new rect (0, 0, bitmap. getwidth (), B Itmap. getheight (); Final rectf = new rectf (rect); Final float roundpx = pixels; 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;}/***** // *** enable bitampdrawable ** @ Param bitmapdrawable * @ Param pixels * @ return */public static bitmapdrawable toroundcorner (bitmapdrawable, int pixels) {Bitmap bitmap = bitmapdrawable. getbitmap (); bitmapdrawable = new bitmapdrawable (toroundcorner (bitmap, pixels); Return bitmapdrawable, then convert it to the scaled bitmap ** @ Param path */public static void savebefore (string path) {bitmapfactory. options = new bitmapfactory. Options (); options. injustdecodebounds = true; // obtain the width and height of the image. Bitmap = bitmapfactory. decodefile (path, options); // at this time, the returned BM is null options. injustdecodebounds = false; // calculate the zoom ratio of int be = (INT) (options. outheight/(float) 200); If (be <= 0) be = 1; options. insamplesize = 2; // The image length and width are reduced by 1/2. // read the image again. set injustdecodebounds to false. Bitmap = bitmapfactory. decodefile (path, options); int W = bit Map. getwidth (); int H = bitmap. getheight (); system. out. println (W + "" + H); // savepng_after (bitmap, PATH); savejpg_after (bitmap, PATH );} /*** Save the image as PNG ** @ Param bitmap * @ Param name */public static void savepng_after (Bitmap bitmap, string name) {file = new file (name ); try {fileoutputstream out = new fileoutputstream (File); If (bitmap. compress (bitmap. compressformat. PNG, 100, out) {ou T. flush (); out. close () ;}} catch (filenotfoundexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace () ;}}/*** Save the image as JPEG ** @ Param bitmap * @ Param path */public static void savejpg_after (Bitmap bitmap, string path) {file = new file (PATH); try {fileoutputstream out = new fileoutputstream (File); If (bitmap. compress (bitmap. compressformat. JPEG, 100, out) {out. f Lush (); out. close () ;}} catch (filenotfoundexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace () ;}}/*** watermark ** @ Param bitmap * @ return */public static bitmap createbitmapforwatermark (Bitmap SRC, bitmap watermark) {If (src = NULL) {return NULL;} int W = SRC. getwidth (); int H = SRC. getheight (); int WW = watermark. getwidth (); int wh = watermark. getheight (); // cr Eate the new blank Bitmap bitmap newb = bitmap. createbitmap (W, H, config. argb_8888); // create a new bitmap canvas CV = new canvas (NEWB) with the same width as SRC; // draw SRC into cv. drawbitmap (SRC, 0, 0, null); // draw SRC at coordinates 0, 0 // draw watermark into cv. drawbitmap (watermark, W-ww + 5, H-Wh + 5, null); // Add the watermark to the bottom right corner of SRC // save all clip cv. save (canvas. all_save_flag); // save // store cv. restore (); // store return newb;}/*** figure Merge slices ** @ return */public static bitmap potomix (INT direction, bitmap... bitmaps) {If (bitmaps. length <= 0) {return NULL;} If (bitmaps. length = 1) {return bitmaps [0];} bitmap newbitmap = bitmaps [0]; // newbitmap = createbitmapforfotomix (bitmaps [0], bitmaps [1], direction ); for (INT I = 1; I <bitmaps. length; I ++) {newbitmap = createbitmapforfotomix (newbitmap, bitmaps [I], direction);} return n Ewbitmap;} Private Static bitmap createbitmapforfotomix (Bitmap first, bitmap second, int direction) {If (first = NULL) {return NULL;} If (second = NULL) {return first;} int fw = first. getwidth (); int FH = first. getheight (); int Sw = second. getwidth (); int SH = second. getheight (); bitmap newbitmap = NULL; If (Direction = left) {newbitmap = bitmap. createbitmap (FW + SW, FH> Sh? FH: Sh, config. argb_8888); canvas = new canvas (newbitmap); canvas. drawbitmap (first, SW, 0, null); canvas. drawbitmap (second, 0, 0, null);} else if (Direction = right) {newbitmap = bitmap. createbitmap (FW + SW, FH> Sh? FH: Sh, config. argb_8888); canvas = new canvas (newbitmap); canvas. drawbitmap (first, 0, 0, null); canvas. drawbitmap (second, FW, 0, null);} else if (Direction = Top) {newbitmap = bitmap. createbitmap (SW> FW? SW: FW, FH + SH, config. argb_8888); canvas = new canvas (newbitmap); canvas. drawbitmap (first, 0, sh, null); canvas. drawbitmap (second, 0, 0, null);} else if (Direction = bottom) {newbitmap = bitmap. createbitmap (SW> FW? SW: FW, FH + SH, config. argb_8888); canvas = new canvas (newbitmap); canvas. drawbitmap (first, 0, 0, null); canvas. drawbitmap (second, 0, FH, null);} return newbitmap ;} /*** convert bitmap to the specified size ** @ Param bitmap * @ Param width * @ Param height * @ return */public static bitmap createbitmapbysize (Bitmap bitmap, int width, int height) {return bitmap. createscaledbitmap (bitmap, width, height, true) ;}/*** Drawable to bitmap ** @ Param drawable * @ return */public static bitmap writable (drawable) {bitmapdrawable = (bitmapdrawable) drawable; return bitmapdrawable. getbitmap ();}/*** bitmap to drawable ** @ Param bitmap * @ return */public static drawable bitmaptodrawablebybd (Bitmap bitmap) {drawable = new bitmapdrawable (Bitmap ); return drawable; }/*** Byte [] to bitmap ** @ Param B * @ return */public static bitmap bytestobimap (byte [] B) {If (B. length! = 0) {return bitmapfactory. decodebytearray (B, 0, B. length);} else {return NULL;}/*** convert bitmap to byte [] ** @ Param BM * @ return */public static byte [] bitmaptobytes (Bitmap BM) {bytearrayoutputstream baos = new bytearrayoutputstream (); BM. compress (bitmap. compressformat. PNG, 100, baos); Return baos. tobytearray ();}}