3 Kinds of image compression processing methods in Android

Source: Internet
Author: User

This article mainly introduces 3 kinds of image compression processing methods in Android, this article explains the quality compression method, obtains the thumbnail, the picture scaling three kinds of methods and gives the example code separately, needs the friend to be possible to refer to under

The existence of pictures in Android:

1: File form: Binary form exists with hard disk.

2: The form of the stream: binary form exists in memory.

The form of 3:bitmap

Three different forms of distinction:

File form and flow form: does not affect the size of the picture. In other words, if the image on your phone's SD card is read into memory in the form of streaming, the size in memory is the size of the original.

Note: It is not a bitmap form.

Bitmap: The memory of the picture will become larger.

The following is the form of the code:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5 86 87 88 89 90 91 92 93 94 /** * Image Compression Method Summary */ /* Image compression Method 01: Quality compression method/private Bitmap Compressimage (Bitmap beforbitmap) { //can capture memory buffers Data and converts it into a byte array. Bytearrayoutputstream BOS = new Bytearrayoutputstream (); if (Beforbitmap!= null) {//First parameter: format of picture compression, second parameter: Compression ratio, third parameter: Compressed data stored in BOS beforbitmap.compress (Compressformat.jpeg, , BOS); int options = 100; Loop to determine whether the compressed picture is greater than 100kb, if greater than, continue to compress, otherwise do not compress while (Bos.tobytearray () length/1024 >) {bos.reset ();//set to empty//compress Optio ns% beforbitmap.compress (compressformat.jpeg, Options, BOS); Reduce the options = 10 each time;  }//Read the data from the BOS and store it in the bytearrayinputstream bytearrayinputstream bis = new Bytearrayinputstream (Bos.tobytearray ()) ; Convert data to picture Bitmap Afterbitmap = Bitmapfactory.decodestream (bis); return afterbitmap; return null;  /* Picture compression method 02: Get Thumbnail/public Bitmap getthumbnail (int id) {//get original Bitmap Beforebitmap = Bitmapfactory.decoderes Ource (Mcontext.getresources (), id); Wide int w = mcontext.getresources (). Getdimensionpixeloffset (R.dimen.IMAGE_W); High int h = mcontext.getresources (). Getdimensionpixelsize (R.dimen.image_h);  //Get thumbnail Bitmap afterbitmap = thumbnailutils. Extractthumbnail (Beforebitmap, W, h); return afterbitmap;  }  /** * image compression * * @param ID * The size of the picture to be manipulated * @param newwidth * Picture specified width * @param newheight * Picture specified height * @return */Public Bitmap compressbitmap (int id, double newwidth, double newheight) {//Get original image Bitmap Beforebitmap = Bitmapfactory.d Ecoderesource (Mcontext.getresources (), id); The original width and height of the picture float beforewidth = Beforebitmap.getwidth (); float beforeheight = Beforebitmap.getheight ();  //compute wide and high scaling rate float scalewidth = 0; float ScaleHeight = 0; if (Beforewidth > Beforeheight) {scalewidth = ((float) newwidth)/beforewidth = ScaleHeight = ((float) newheight)/Be Foreheight; else {ScaleWidth = ((float) newwidth)/beforeheight; scaleheight = ((float) newheight)/beforewidth;}  //Matrix objects Matrix matrix = new Matrix (); Zoom picture Action Scaling Matrix.postscale (ScaleWidth, ScaleHeight); Create a new Bitmap clip image from the original image Bitmap Afterbitmap = Bitmap.createbitmap (beforebitmap, 0, 0, (int) beforewidth, (int) Beforehei Ght, Matrix, True); return afterbitmap;  }
Related Article

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.