First, the problem description |
In the development, when we need to have a large picture at the same time also need some small picture, we just need to use the code to the different scale of this picture, so that greatly conserve resources, reduce the size of the installation package. In addition to zooming, we often do other things like cropping, rotating, storing, and so on.
This allows us to write general-purpose components for processing images for easy development. Here's a look at the components of the image processing Bitmaputil, the case interface:
Second, technical point description |
1. get bitmap through Bitmapfactory
Bitmap Bm=bitmapfactory.decodestream (InputStream is);
2, Bimap's CreateBitmap () method
Bitmap NEWBM = Bitmap.createbitmap (Bitmap s, int x, int y, int w, int h, Matrix m, Boolean f);
This method can be used for zooming, cropping, and rotating the bitmap.
Parameter description:
bitmap s: Original bitmap to be processed
int x, y: Start position coordinates
int W: Width of the graph to be truncated
int H: Width of the graph to be truncated
Span style= "font-family: ' Microsoft Yahei '; Font-size:14px ">matrix m matrix, mainly for planar scaling, panning, rotating
boolean F: Guaranteed equal to
return value: Returns the processed bitmap
Third, Bitmaputil components |
It can achieve the following methods, such as scaling the picture, cropping the picture proportionally, and processing the circular picture, and realizes the functions as follows:
1. Readbitmapbyid () method
/** * Converted from resource ID to Bitmap * @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); }
2. Scaleimage () method to zoom the picture by the specified width and height
Execution effect
/*** Zoom Picture * @param bm to zoom picture * @param newwidth width * @param newheight height * @return processed pictures * /public s Tatic Bitmap scaleimage (Bitmap BM, int newwidth, int newheight) { if (BM = = null) { return null; } int width = bm.getwidth (); int height = bm.getheight (); float ScaleWidth = ((float) newwidth)/width; float ScaleHeight = ((float) newheight)/height; Matrix matrix = new Matrix (); Matrix.postscale (ScaleWidth, scaleheight); Bitmap NEWBM = bitmap.createbitmap (BM, 0, 0, width, height, matrix,true); if (BM! = null &!bm.isrecycled ()) { bm.recycle ();//Destroy original picture BM = null; } return NEWBM; }
3. Imagecrop () method
Execution effect
/** * Cut the picture according to a certain width and height * @param bitmap the picture to be cropped * @param the proportion of the NUM1 long edge * @param the proportion of the num2 short edge * @param isRecycled Whether to recycle the original picture * @return Cropped picture */public static Bitmap Imagecrop (Bitmap Bitmap, int num1, int num2, Boolean isrecycl ed) {if (bitmap = = null) {return null; } int w = Bitmap.getwidth (); Get the picture wide, high int h = bitmap.getheight (); int 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 ();//recover original picture bitmap = null; } return BMP; }
4. Toroundcorner () to turn the picture round
Execution Effect
/** * picture to Fillet * @param bitmap need to turn the bitmap * @param the radian of the pixels round * @return to the fillet of bitmap */public static B Itmap Toroundcorner (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; }
5, Toroundbitmap () method to cut the image into a circle
Execution effect
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; }
6. Rotaingimageview () method to realize rotating picture
Execution effect
/** * rotate picture * @param angle Rotation angle * @param bitmap to process bitmap * @return processed bitmap */public static B Itmap rotaingimageview (int angle, Bitmap Bitmap) { //Rotate picture action matrix matrix = new Matrix (); Matrix.postrotate (angle); Create a new picture Bitmap Resizedbitmap = Bitmap.createbitmap (Bitmap, 0, 0, bitmap.getwidth (), Bitmap.getheight (), Matrix, true); if (resizedbitmap! = Bitmap && Bitmap! = null &&!bitmap.isrecycled ()) { bitmap.recycle (); bitmap = null; } return resizedbitmap; }
7, SAVEBMPTOSD () implementation will save bitmap to SDcard
public static Boolean Savebmptosd (String dir, Bitmap BM, string filename, int quantity, Boolean recyle) {Boolean ret = true; if (BM = = null) {return false;} File Dirpath = new file (dir); if (!exists (dir)) {dirpath.mkdirs (); } if (!dir.endswith (File.separator)) {dir + = File.separator; } File File = new file (dir + filename); OutputStream outstream = null; try {file.createnewfile (); OutStream = new FileOutputStream (file); Bm.compress (Bitmap.CompressFormat.JPEG, Quantity, OutStream); } catch (Exception e) {e.printstacktrace (); ret = false; } finally {try {if (OutStream! = null) outstream.close (); } catch (IOException e) {e.printstacktrace (); } if (Recyle &&!bm.isrecycled ()) {bm.recycle (); BM = NULL; }} return ret; }
Android allows you to zoom, cut, rotate, and store images