public static Bitmap CreateBitmap (Bitmap source, int x, int y, int width, int height, Matrix m, Boolean filter)
Cutting an image from the original bitmap is an advanced way. You can use matrix to achieve advanced methods such as rotation
Parameter description:
Bitmap Source: The original bitmap from which to
int x: starting x coordinate
int y: start y coordinate
int width: The width of the graph to be truncated
int height: The width of the graph to be truncated
Bitmap.config Config: A configuration of an enumeration type that can define the quality of the new bitmap that is truncated
Return value: Returns a cut-bitmap
three,bitmap cut package
In practice, because the project needs to encapsulate the basic functionality from time to time, here is a package code for reference only.
Import Java.io.ByteArrayOutputStream; Import Java.io.File; Import Java.io.FileInputStream; Import java.io.FileNotFoundException; Import java.io.IOException; Import Java.io.InputStream; Import java.util.ArrayList; Import Android.content.Context; Import Android.graphics.Bitmap; Import Android.graphics.Bitmap.Config; Import Android.graphics.BitmapFactory; Import Android.graphics.Canvas; Import Android.graphics.Paint; Import Android.graphics.PorterDuff; Import Android.graphics.PorterDuff.Mode; Import Android.graphics.PorterDuffXfermode; Import Android.graphics.Rect; Import Android.graphics.RectF; public class Bitmapcut {/** * converted to bitmap by resource ID * * @param context * @param resId * @return */public static Bitmap Readbitmapbyid (context context, int resId) {bitmapfactory.options OPT = new Bitmapfactory.options (); Opt.inpreferredconfig = Bitmap.Config.RGB_565; Opt.inpurgeable = true; Opt.ininputshareable = true; InputStream is = Context.getresources (). Openrawresource (ResId); Return Bitmapfactory.decodestream (IS, null, opt); }/** * Set the background to fillet * * @param bitmap * @param pixels * @return */public static Bitmap Removeyuanjiao (Bitmap Bitmap, int pixels) { int width = bitmap.getwidth (); int height = bitmap.getheight (); Bitmap Crebitmap = bitmap.createbitmap (width, height, android.graphics.Bitmap.Config.ARGB_8888); Canvas canvas = new canvas (CREBITMAP); Paint paint = new paint (); float roundpx = pixels; RECTF RECTF = new RECTF (0, 0, bitmap.getwidth ()-Pixels, Bitmap.getheight ()-pixels); Paint.setantialias (TRUE); Canvas.drawargb (0, 0, 0, 0); Canvas.drawroundrect (RECTF, ROUNDPX, ROUNDPX, paint); Paint.setxfermode (New Porterduffxfermode (PorterDuff.Mode.SRC_IN)); Canvas.drawbitmap (bitmap, 0, 0, paint); if (!bitmap.isrecycled ()) bitmap.recycle (); return crebitmap; }/** * Crop a picture by square */public static Bitmap Imagecrop (Bitmap Bitmap, Boolean isrecy cled) {if (bitmap = = null) {return null; } int w = Bitmap.getwidth (); Get the picture wide, high int h = bitmap.getheight (); int WH = w > h? h:w;//the square area edge length int retx = w > H after trimming? (w-h)/2:0;//based on original, take square upper left corner x coordinate int rety = w > H? 0: (h-w)/2; Bitmap bmp = Bitmap.createbitmap (Bitmap, Retx, rety, WH, WH, NULL, FALSE); if (isRecycled && bitmap! = null &&!bitmap.equals (BMP) &&!bitmap.isrecycled ()) {bitmap.recycle (); bitmap = null; }//The following sentence is the key return bmp;//Bitmap.createbitmap (Bitmap, Retx, rety, WH, WH, NULL, FALSE); /** * Cut pictures by Rectangle * * @param bitmap * @return */Publ IC Static Bitmap imagecropwithrect (Bitmap Bitmap) {if (Bitmap = = null) { return null; } int w = Bitmap.getwidth (); Get the picture wide, high int h = bitmap.getheight (); int NW, NH, RETX, rety; if (W > h) {nw = H/2; Nh= h; Retx = (W-NW)/2; rety = 0; } else {NW = W/2; NH = w; Retx = W/4; Rety = (h-w)/2; }//The following sentence is key Bitmap bmp = Bitmap.createbitmap (Bitmap, Retx, Rety, NW, NH, NULL, FALSE); if (bitmap! = null &&!bitmap.equals (BMP) &&!bitmap.isrecycled ()) {bitmap. Recycle (); bitmap = null; } return bmp;//Bitmap.createbitmap (Bitmap, Retx, Rety, NW, NH, NULL,//False); }/** * Bitmap-byte[] * * @param BMP * @return */public static byte[] Readbitmap (Bitmap bmp) {Bytearrayoutputstream BAOs = new ByteArray OutputStream (); Bmp.compress (Bitmap.compressformat. JPEG, BAOs); try {baos.flush (); Baos.close (); } catch (IOException e) {e.printstacktrace (); } return Baos.tobytearray (); /** * Crop the image to a circle * * @param bitmap * @return * * public static Bitmap Toroundbitmap (Bitmap Bitmap) {if (Bitmap = = null) { return null; } int width = Bitmap.getwidth (); int height = bitmap.getheight (); float ROUNDPX; Float left, top, right, bottom, Dst_left, Dst_top, Dst_right, Dst_bottom; if (width <= height) {roundpx = WIDTH/2; top = 0; bottom = width; left = 0; right = width; Height = width; Dst_left = 0; dst_top = 0; Dst_right = width; Dst_bottom = width; } else {roundpx = HEIGHT/2; Float clip = (width-height)/2; left = clip; right = Width-clip; top = 0; bottom = height; width = height; Dst_left = 0; dst_top = 0; Dst_right = height; Dst_bottom = height; } Bitmap output = Bitmap.createbitmap (width, height, config.argb_8888); Canvas canvas = new canvas (output); final int color = 0xff424242; Final Paint paint = new paint (); Final rect src = new rect ((int) left, (int) top, (int.) right, (int) bottom); Final rect dst = new rect ((int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom); Final RECTF RECTF = new RECTF (DST); Paint.setantialias (TRUE); Canvas.drawargb (0, 0, 0, 0); Paint.setcolor (color); Canvas.drawroundrect (RECTF, ROUNDPX, ROUNDPX, paint); Paint.setxfermode (New Porterduffxfermode (mode.src_in)); Canvas.drawbitmap (bitmap, SRC, DST, paint); if (bitmap! = null &&!bitmap.isrecycled ()) {bitmap.recycle (); bitmap = null; } return output; }//Turn the picture into a circular picture with rounded edges public static Bitmap Getroundbitmap (Bitmap Bitmap, int width, int height) {if (bitmap = = null) {return null; }//Turn the picture into a fillet Bitmap Roundbitmap = bitmap.createbitmap (width, height, Config . argb_8888); Canvas Canvas = new Canvas (ROUNDBITMAP); Paint paint = new paint (Paint.anti_alias_flag); int len = (width > height)? Height:width; Canvas.drawcircle (WIDTH/2, HEIGHT/2, LEN/2-8, paint); Paint.setxfermode (New Porterduffxfermode (mode.src_in)); Bitmap Scaledbitmap = Bitmap.createscaledbitmap (Bitmap, Len, Len, True); Canvas.drawbitmap (scaledbitmap, 0, 0, paint); Add the picture round the edge Bitmap Outbitmap = bitmap.createbitmap (width, height, config.argb_8888); Canvas = new canvas (OUTBITMAP); Paint = new Paint (Paint.anti_alias_flag); Paint.setcolor (0xFFFFFFFF); Canvas.drawcircle (WIDTH/2, HEIGHT/2, LEN/2-4, paint); Paint.setxfermode (New Porterduffxfermode (Mode.src_over)); Canvas.drawbitmap (roundbitmap, 0, 0, paint); Bitmap.recycle (); bitmap = null; Roundbitmap.recycle (); Roundbitmap = null; Scaledbitmap.recycle (); Scaledbitmap = null; return outbitmap; }//Turn the picture into a circular picture with rounded edges public static Bitmap Getroundbitmap (Bitmap Bitmap, int width, int height, int color) {if (bitmap = = null) {return null; }//Turn the picture into a fillet Bitmap Roundbitmap = bitmap.createbitmap (width, height, Co Nfig. argb_8888); Canvas canvas = new canvas (ROUNDBITMAP); Paint paint = new paint (Paint.anti_alias_flag); int len = (width > height)? Height:width; Canvas.drawcircle (WIDTH/2, HEIGHT/2, LEN/2-8, paint); Paint.setxfermode (New Porterduffxfermode (mode.src_in)); Bitmap Scaledbitmap = Bitmap.createscaledbitmap (Bitmap, Len, Len, True); Canvas.drawbitmap (scaledbitmap, 0, 0, paint); Add a picture to a circleEdge Bitmap Outbitmap = bitmap.createbitmap (width, height, config.argb_8888); Canvas = new canvas (OUTBITMAP); Paint = new Paint (Paint.anti_alias_flag); Paint.setcolor (color); Canvas.drawcircle (WIDTH/2, HEIGHT/2, LEN/2-4, paint); Paint.setxfermode (New Porterduffxfermode (Mode.src_over)); Canvas.drawbitmap (roundbitmap, 0, 0, paint); Bitmap.recycle (); bitmap = null; Roundbitmap.recycle (); Roundbitmap = null; Scaledbitmap.recycle (); Scaledbitmap = null; return outbitmap; }/** * Function: Picture round corner * * @param bitmap * need to turn bitmap * @param pixels * Rounded radian * @return Round BITMAP */public static Bitmap T Oroundcorner (Bitmap Bitmap, int pixels) {Bitmap output = Bitmap.crEatebitmap (Bitmap.getwidth (), Bitmap.getheight (), config.argb_8888); Canvas canvas = new canvas (output); final int color = 0xff424242; Final Paint paint = new paint (); Final rect rect = new Rect (0, 0, bitmap.getwidth (), Bitmap.getheight ()); Final RECTF 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 Porterduffxfermode (mode.src_in)); Canvas.drawbitmap (Bitmap, rect, rect, paint); if (bitmap! = null &&!bitmap.isrecycled ()) {bitmap.recycle (); } return output; /** * Gets the specified fillet picture * * @param bitmap * @return * * public static Bitmap Getradiusbitmap (Bitmap Bitmap) {Paint paint = new Paint (paint.anti_alias_ FLAG); Paint.setcolor (0xFFFFFFFF); Bitmap Radiusbitmap = Bitmap.createbitmap (Bitmap.getwidth (), Bitmap.getheight (), config.argb_8888); Canvas canvas = new canvas (RADIUSBITMAP); RECTF RECTF = new RECTF (0, 0, bitmap.getwidth (), Bitmap.getheight ()); Canvas.drawroundrect (RECTF, 7, 7, paint); Paint.setxfermode (New Porterduffxfermode (mode.src_in)); Canvas.drawbitmap (bitmap, 0, 0, paint); if (bitmap! = null &&!bitmap.isrecycled ()) {bitmap.recycle (); } return Radiusbitmap; Bitmap array public static arraylist<bitmap> getradiusbitmaplist (string[] Patharr for the rounded edge of the specified size ay, int size, int len, float radius, int color) { Bitmap canvasbitmap = null; Canvas canvas = null; Paint paint = null; RECTF RECTF = new RECTF (0, 0, Len-radius, Len-radius); File file = null; FileInputStream FIS = null; Bitmap Bitmap = null; Bitmap scaledbitmap = null; arraylist<bitmap> list = new arraylist<bitmap> (); for (int i = 0; i < patharray.length; i++) {file = new file (Patharray[i]); if (!file.exists ()) continue; try {fis = new FileInputStream (file); Bitmap = Bitmapfactory.decodestream (FIS); if (bitmap! = null) {Canvasbitmap = Bitmap.createbitmap (len, Len, config.argb_8888); Canvas = new canvas (CANVASBITMAP); Paint = new Paint (Paint.anti_alias_flag); Paint.setcolor (color); Canvas.drawroundrect (RECTF, radius, radius, paint); Paint.setxfermode (New Porterduffxfermode (mode.src_in)); Scaledbitmap = Bitmap.createscaledbitmap (Bitmap, Len, Len, True); Canvas.drawbitmap (scaledbitmap, 0, 0, paint); List.add (CANVASBITMAP); }} catch (FileNotFoundException e) {} finally { if (FIS! = null) {try { Fis.close (); } catch (IOException e1) {}}} if (list.size () = = Size) break; } if (Scaledbitmap! = null &&!scaledbitmap.isrecycled ()) {Scaledbitma P.recycle (); Scaledbitmap = null; } if (bitmap! = null &&!bitmap.isrecycled ()) {bitmap.recycle (); bitmap = null; } return list; }/** * Cut the picture according to a certain width/height ratio * * @param bitmap * @param num1 * Long edge ratio * @param num2 * Short side ratio * @return */public static Bitmap Imagecrop (Bitmap Bitmap, int num1, int num2, Boolean isRecycled) {if (bitmap = = null) {return null; } int w = Bitmap.getwidth (); Get the picture wide, high int h = bitmap.getheight (); InchT Retx, rety; int NW, NH; if (W > H) {if (H > w * num2/num1) {NW = W; NH = w * NUM2/NUM1; Retx = 0; Rety = (H-NH)/2; } else {NW = h * NUM1/NUM2; NH = h; Retx = (W-NW)/2; rety = 0; }} else {if (W > H * num2/num1) { NH = h; NW = h * NUM2/NUM1; rety = 0; Retx = (W-NW)/2; } else {nh = w * NUM1/NUM2; NW = W; Rety = (H-NH)/2; Retx = 0; }} Bitmap bmp = Bitmap.creaTebitmap (Bitmap, Retx, Rety, NW, NH, NULL, FALSE); if (isRecycled && bitmap! = null &&!bitmap.equals (BMP) &&!bitmap.isrecycled ()) {bitmap.recycle (); bitmap = null; } return bmp;//Bitmap.createbitmap (Bitmap, Retx, Rety, NW, NH, NULL,//False); } }
Cut an image from the original bitmap