Once in the mobile phone to upload pictures. Directly capture the camera's original image upload, the original size is generally 1~2m. Therefore, upload a more than a waste of resources, some scenes also need to upload more than one picture, so recently looked at a lot of predecessors written about the image processing information. Then try to change a picture compression method. The test is good. The compression effect is very ideal, mainly distortion is not obvious. Share it here.
The following code is directly on:
/** * Upload the server when the image is called the following method compressed after saving to the temporary directory image compression less than 200KB. Distortion is not obvious * * @param path * @return * @throws ioexception */public static Bitmap revitionimagesize (String path) throws Ioexc eption {Bufferedinputstream in = new Bufferedinputstream (new FileInputStream (path)); Bitmapfactory.options Options = new Bitmapfactory.options (); options.injustdecodebounds = true; Bitmapfactory.decodestream (in, null, options); In.close (); int i = 0; Bitmap Bitmap = null;//Options.injustdecodebounds=true then will not return the actual Bitmap object, do not allocate memory space but can get some decoding boundary information, such as picture size information// Outheight (image original height) and outwidth (original width of the picture)//Insamplesize represents a thumbnail size of a fraction of the original picture size of one//Options.outwidth >> I (right shift operator) Represents: outwidth/(2^i) while (true) {if ((options.outwidth >> i <=) && (options.outheight >> i <= ) {in = new Bufferedinputstream (new FileInputStream (path)), options.insamplesize = (int) Math.pow (2.0D, i) ; The power operation I is several square options.injustdecodebounds = False;bitmap = Bitmapfactory.decodestream (in, null, options); i + = 1;} return bitmap;}
Android Learning image compression, high compression and low distortion