public static Bitmap Revitionimagesize (String path) throws IOException {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;while (True) {if (options.outwidth >> i <=) && (options.outheight >> i &L t;=) {in = new Bufferedinputstream (new FileInputStream (path)); options.insamplesize = (int) Math.pow (2.0D , i); options.injustdecodebounds = False;bitmap = Bitmapfactory.decodestream (in, null, options); i + = 1;} return bitmap;}
Image compression by proportional size (get pictures by path and compress)
public static Bitmap GetImage (String srcpath) throws ioexception{bitmapfactory.options newopts = new Bitmapfactor Y.options (); Start reading into the picture, at this point set the Options.injustdecodebounds back to true newopts.injustdecodebounds = true; Bitmap Bitmap = Bitmapfactory.decodefile (srcpath,newopts);//return BM At this time is empty newopts.injustdecodebounds = false; int w = newopts.outwidth; int h = newopts.outheight; Today the mainstream mobile phone is more 800*480 resolution. So high and wide we set to float hh = 800f;//Here Set the height to 800f float ww = 480f;//Here Set the width to 480f//zoom ratio.Because it is a fixed scale, it is possible to calculate with only one data of high or wide, int be = 1;//be=1 means not to scale if (W > H && w > ww) {//Assuming wide width Fixed size scaling be = (int) (NEWOPTS.OUTWIDTH/WW); } else if (W < h && h > hh) {//if height is assumed to be high, scale be = (int) (NEWOPTS.OUTHEIGHT/HH) According to the fixed width size; } if (be <= 0) is = 1; Newopts.insamplesize = be;//Set scaling//Read the picture again, notice that the options.injustdecodebounds is set back to false bitmap = Bitmapf Actory.decodefile (Srcpath, newopts); Return bitmap;//compress a good scale and then mass compress}
Third: Picture by proportional size compression method (according to bitmap image compression):
public static Bitmap Comp (Bitmap image) {Bytearrayoutputstream BAOs = new Bytearrayoutputstream (); Image.compress (Bitmap.CompressFormat.JPEG, BAOs); if (Baos.tobytearray (). length/1024>1024) {//inference assumes that the picture is larger than 1M, compression avoids overflow when generating the picture (Bitmapfactory.decodestream) baos.res ET ();//reset BAOs to Empty BAOs image.compress (Bitmap.CompressFormat.JPEG, BAOs);//compress 50% here, and store the compressed data in BAOs} B Ytearrayinputstream ISBM = new Bytearrayinputstream (Baos.tobytearray ()); Bitmapfactory.options newopts = new Bitmapfactory.options (); Start reading into the picture, at this point set the Options.injustdecodebounds back to true newopts.injustdecodebounds = true; Bitmap Bitmap = Bitmapfactory.decodestream (ISBM, NULL, newopts); Newopts.injustdecodebounds = false; int w = newopts.outwidth; int h = newopts.outheight; Today the mainstream phone is more 800*480 resolution, so the height and width we set to float hh = 800f;//Here Set the height for 800f float ww = 480f;//Here Set the width to 480f//zoom ratio.Because it is a fixed scale, it can be calculated only with one data of high or wide, and int be = 1;//be=1 is not scaled if (W > H && w > ww) {//If width is large, size is scaled according to width be = (int) (NEWOPTS.OUTWIDTH/WW); } else if (W < h && h > hh) {//if height is assumed to be high, scale be = (int) (NEWOPTS.OUTHEIGHT/HH) According to the fixed width size; } if (be <= 0) is = 1; Newopts.insamplesize = be;//Set the zoom//read again in the picture. Note that the options.injustdecodebounds is now set back to false ISBM = new Bytearrayinputstream (Baos.tobytearray ()); Bitmap = Bitmapfactory.decodestream (ISBM, NULL, newopts); Return bitmap;//compress a good scale and then mass compress}
Breakthrough based on path and compression return bitmap for display
public static Bitmap Getsmallbitmap (String filePath) throws ioexception{final bitmapfactory.options Options = new BITMAPF Actory. Options (); options.injustdecodebounds = true; Bitmapfactory.decodefile (FilePath, options); options.insamplesize = calculateinsamplesize (options, 480, +); o Ptions.injustdecodebounds = False;return bitmapfactory.decodefile (filePath, options);} /** * Calculate the zoom value of the picture * * @param options * @param reqwidth * @param reqheight * @return */public static int calculateinsample Size (bitmapfactory.options options,int reqwidth, int reqheight) {final int height = options.outheight;final int width = op Tions.outwidth;int insamplesize = 1;if (height > Reqheight | | width > reqwidth) {final int heightratio = Math.Round ( (float) height/(float) reqheight), final int widthRatio = Math.Round ((float) width/(float) reqwidth); insamplesize = Heig Htratio < WidthRatio? Heightratio:widthratio;} return insamplesize;}
Android Image Compression Method Daquan