Android allows scaling, cropping, rotating, and storing images,
During development, when we need a large image and some small images, we only need to scale the image in different proportions through code, which greatly saves resources, the size of the installation package is reduced. In addition to scaling, we often perform other operations on images, such as cropping, rotating, and storing images.
In this way, we can write common components for processing images for convenient development. The following section describes the BitmapUtil component used to process images. The case page is as follows:
1,PassBitmapFactoryObtain Bitmap
Bitmap bm=BitmapFactory.decodeStream(InputStream is );
2. createBitmap () method of Bimap
Bitmap newbm = Bitmap.createBitmap( Bitmap s, int x, int y, int w, int h, Matrix m, boolean f);
This method allows bitmap scaling, cropping, and rotating.
Parameter description:
Bitmap s: the original Bitmap to be processed
Int x, y: Start Coordinate
Int w: the width of the graph to be captured.
Int h: the width of the graph to be captured.
Matrix m Matrix, mainly used for plane scaling, translation, and Rotation
Boolean f: whether to ensure the equality
Returned value: returns the processed Bitmap.
You can scale the image proportionally, crop the image proportionally, and process the circular image as follows:
1. readBitmapById () method
/*** Convert the 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. ininputretriable = true; InputStream is = context. getResources (). openRawResource (resId); return BitmapFactory. decodeStream (is, null, opt );}
2. Use the scaleImage () method to scale the Image Based on the specified width and height
Execution result
/*** Scale the image ** @ param bm to scale the image * @ param newWidth width * @ param newHeight height * @ return processed image */public static 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 (scaleW Idth, scaleHeight); Bitmap newbm = Bitmap. createBitmap (bm, 0, 0, width, height, matrix, true); if (bm! = Null &! Bm. isRecycled () {bm. recycle (); // destroy the original image bm = null;} return newbm ;}
3. imageCrop () method
Execution result
/*** Crop an image based on a certain aspect ratio * @ param bitmap the image to be cropped * @ param num1 long edge ratio * @ param num2 short edge ratio * @ param isRecycled? reclaim the cropped image * @ return */public static Bitmap imageCrop (Bitmap bitmap, int num1, int num2, boolean isRecycled) {if (bitmap = null) {return null;} int w = bitmap. getWidth (); // obtain the image width, 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 (); // reclaim the original image bitmap = null;} return bmp ;}
4. toRoundCorner () converts an image to a rounded corner.
Execution result
/*** Convert an image to a rounded corner * @ param bitmap the bitmap to be converted * @ param pixels the Radian of the rounded corner * @ return bitmap to the rounded corner */public static Bitmap toRoundCorner (Bitmap bitmap, int pixels) {Bitmap output = Bitmap. createBitmap (bitmap. getWidth (), bitmap. getHeight (), Config. ARGB_8888); Canvas canvas = new Canvas (output); final int color = 0xff0000242; final Paint paint = new Paint (); final Rect rect = new Rect (0, 0, bitmap. getWidth (), bitmap. ge THeight (); 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 porterduduxfermode (Mode. SRC_IN); canvas. drawBitmap (bitmap, rect, rect, paint); if (bitmap! = Null &&! Bitmap. isRecycled () {bitmap. recycle () ;}return output ;}
5. The toRoundBitmap () method crops the image into a circle.
Execution result
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 rotate Images
Execution result
/*** Rotate the image ** @ param angle Rotation angle * @ param bitmap the Bitmap to be processed * @ return Bitmap after processing */public static Bitmap rotaingImageView (int angle, Bitmap bitmap) {// rotating image action Matrix matrix = new Matrix (); matrix. postRotate (angle); // create a new image 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. The saveBmpToSd () Implementation saves 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; }