Android more reliable image compression

Source: Internet
Author: User

First: Let's look at the mass compression method first:

    PrivateBitmap 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 = -;  while(Baos.tobytearray (). Length/1024x768> -) {//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-=Ten;//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; }  

Second: Picture by proportional size compression method (get picture according to Path and compress):

    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.}

Third: Image compression by proportional size (according to bitmap image compression):

    PrivateBitmap Comp (Bitmap image) {Bytearrayoutputstream BAOs=NewBytearrayoutputstream (); Image.compress (Bitmap.CompressFormat.JPEG, -, BAOs); if(Baos.tobytearray (). Length/1024x768>1024x768) {//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.}

Android more reliable image compression

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.