Daily Summary-Image Compression

Source: Internet
Author: User

Use the Android system to compress images. The Code is as follows:

 1 private String path = "/sdcard/img.jpg"; 2 File f = new File(path); 3 Bitmap bm = PictureUtil.getSmallBitmap(this,path); 4 FileOutputStream fos; 5     try { 6         fos = new FileOutputStream(new File("/sdcard/", "small_" + f.getName())); 7         bm.compress(Bitmap.CompressFormat.JPEG, 90, fos); 8     } catch (FileNotFoundException e) { 9         e.printStackTrace();10         }        

The getsmallbitmap method obtains the Image Based on the path and compresses the image to return bitmap for display.

 1     /** 2      * 计算图片的缩放值 3      *  4      * @param options 5      * @param reqWidth 6      * @param reqHeight 7      * @return 8      */ 9     public static int calculateInSampleSize(BitmapFactory.Options options,10             int reqWidth, int reqHeight) {11         // Raw height and width of image12         final int height = options.outHeight;13         final int width = options.outWidth;14         int inSampleSize = 1;15 16         if (height > reqHeight || width > reqWidth) {17 18             // Calculate ratios of height and width to requested height and19             // width20             final int heightRatio = Math.round((float) height21                     / (float) reqHeight);22             final int widthRatio = Math.round((float) width / (float) reqWidth);23 24             // Choose the smallest ratio as inSampleSize value, this will25             // guarantee26             // a final image with both dimensions larger than or equal to the27             // requested height and width.28             inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;29         }30 31         return inSampleSize;32     }33 34     35 36     37     /**38      * 根据路径获得图片并压缩返回bitmap用于显示39      * 40      * @param imagesrc41      * @return42      */43     public static Bitmap getSmallBitmap(Context context ,String filePath) {44         final BitmapFactory.Options options = new BitmapFactory.Options();45         options.inJustDecodeBounds = true;46         BitmapFactory.decodeFile(filePath, options);47 48         DisplayMetrics dm = context.getResources().getDisplayMetrics();49         50         // Calculate inSampleSize51         options.inSampleSize = calculateInSampleSize(options, dm.widthPixels, dm.heightPixels);52 53         // Decode bitmap with inSampleSize set54         options.inJustDecodeBounds = false;55 56         return BitmapFactory.decodeFile(filePath, options);57     }

 

Daily Summary-Image Compression

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.