In the past to do the mobile phone upload pictures, direct access to the camera to capture the original image upload, the original size is generally 1~2m, so upload 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, and then try to change a picture compression method. The test is also good, the compression effect is ideal, mainly distortion is not obvious. Share it here.
Directly below the code:
/** * Upload the server when the image is called the following method compressed after saving to the temporary folder image compressed less than 200KB, distortion is not obvious * * @param path * @return * @throws ioexception */public static Bit Map Revitionimagesize (String path) throws IOException {Bufferedinputstream in = new Bufferedinputstream (new FileInputStream (new File)); 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 fraction of the size of the original picture//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;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Learning image compression, high compression and low distortion