three common image compression methods in Android application development are: Mass compression method, proportional compression method (obtain picture according to path and compress) and proportional compression method (according to bitmap image compression). First, mass compression methodPrivateBitmap compressimage (Bitmap image) {Bytearrayoutputstream BAOs=NewBytearrayoutputstream (); Image.compress (Bitmap.CompressFormat.JPEG,, BAOs);//mass compression method, where 100 means no compression, the compressed data stored in the BAOs intoptions = 100; while(Baos.tobytearray (). length/1024>100) {//loop to determine if the image is larger than 100kb after compressionBaos.reset ();//Reset BAOs is empty BAOsImage.compress (Bitmap.CompressFormat.JPEG, Options, BAOs);//this compresses the options% and stores the compressed data in the BAOs .Options-= 10;//each time, it's reduced by ten} bytearrayinputstream ISBM=NewBytearrayinputstream (Baos.tobytearray ());//Store the compressed data BAOs in the BytearrayinputstreamBitmap Bitmap = Bitmapfactory.decodestream (ISBM,NULL,NULL);//generate images from the Bytearrayinputstream data returnbitmap; The picture is compressed by proportional size (the image is obtained by the path and compressed)PrivateBitmap getimage (String srcpath) {bitmapfactory.options newopts=Newbitmapfactory.options (); //start reading the picture, this time set the Options.injustdecodebounds back to TrueNewopts.injustdecodebounds =true; Bitmap Bitmap= Bitmapfactory.decodefile (srcpath,newopts);//return BM is empty at this timeNewopts.injustdecodebounds=false; intW =Newopts.outwidth; inth =Newopts.outheight; //now the mainstream phone is more 800*480 resolution, so the height and width we set to floatHH = 800f;//set height to 800f here floatWW = 480f;//This sets the width to 480f//scaling ratio. Because it is a fixed scale, only one of the data with high or wide is calculated intbe = 1;//be=1 indicates no scaling if(W > H && w > WW) {//If the width is large, scale according to the fixed width sizebe = (int) (Newopts.outwidth/ww); } Else if(W < h && h > hh) {//if height is high, it is scaled according to width fixed sizebe = (int) (Newopts.outheight/hh); } if(Be <= 0) be= 1; Newopts.insamplesize= be;//set the zoom ratio//re-read the picture and note that the Options.injustdecodebounds has been set back to false at this timeBitmap =bitmapfactory.decodefile (Srcpath, newopts); returnCompressimage (bitmap);//compress the proportions and then compress the mass.The picture is compressed by proportional size (compressed according to bitmap image)PrivateBitmap Comp (Bitmap image) {Bytearrayoutputstream BAOs=NewBytearrayoutputstream (); Image.compress (Bitmap.CompressFormat.JPEG,100, BAOs); if(Baos.tobytearray (). length/1024>1024) {//determine if the picture is larger than 1M, compress to avoid overflow when generating picture (Bitmapfactory.decodestream)Baos.reset ();//Reset BAOs is empty BAOsImage.compress (Bitmap.CompressFormat.JPEG, BAOs);//this compresses 50% and stores the compressed data in the BAOs .} bytearrayinputstream ISBM=NewBytearrayinputstream (Baos.tobytearray ()); Bitmapfactory.options newopts=Newbitmapfactory.options (); //start reading the picture, this time set the Options.injustdecodebounds back to TrueNewopts.injustdecodebounds =true; Bitmap Bitmap= Bitmapfactory.decodestream (ISBM,NULL, newopts); Newopts.injustdecodebounds=false; intW =Newopts.outwidth; inth =Newopts.outheight; //now the mainstream phone is more 800*480 resolution, so the height and width we set to floatHH = 800f;//set height to 800f here floatWW = 480f;//This sets the width to 480f//scaling ratio. Because it is a fixed scale, only one of the data with high or wide is calculated intbe = 1;//be=1 indicates no scaling if(W > H && w > WW) {//If the width is large, scale according to the fixed width sizebe = (int) (Newopts.outwidth/ww); } Else if(W < h && h > hh) {//if height is high, it is scaled according to width fixed sizebe = (int) (Newopts.outheight/hh); } if(Be <= 0) be= 1; Newopts.insamplesize= be;//set the zoom ratio//re-read the picture and note that the Options.injustdecodebounds has been set back to false at this timeISBM =NewBytearrayinputstream (Baos.tobytearray ()); Bitmap= Bitmapfactory.decodestream (ISBM,NULL, newopts); returnCompressimage (bitmap);//compress the proportions and then compress the mass.}
Three common image compression methods in Android application development