3 examples of how to compress Android images

Source: Internet
Author: User

Android Image compression method:

First: Mass compression method:


private Bitmap Compressimage (Bitmap image) {

Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
image.compress (Bitmap.CompressFormat.JPEG, BAOs);//Quality compression method, here 100 means no compression, the compressed data stored in the BAOs
int options = +;
while (Baos.tobytearray (). length/1024>100) {//loop to determine if the picture is larger than 100kb after compression, greater than continue compression
Baos.reset ();//Reset BAOs is empty BAOs
Options-= 10;//each time it is reduced by ten
image.compress (Bitmap.CompressFormat.JPEG, Options, BAOs);//compress options% here and store the compressed data in BAOs

        }
Bytearrayinputstream ISBM = new Bytearrayinputstream (Baos.tobytearray ());//Store compressed data BAOs into Bytearrayinputstream
Bitmap Bitmap = bitmapfactory.decodestream (ISBM, NULL, NULL);//Generate pictures of Bytearrayinputstream data
return bitmap;
    }


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


private Bitmap getimage (String srcpath) {
bitmapfactory.options newopts = new Bitmapfactory.options ();
///start reading the picture and set the Options.injustdecodebounds back to True
newopts.injustdecodebounds = true;
Bitmap Bitmap = Bitmapfactory.decodefile (srcpath,newopts);//return BM to empty at this time

newopts.injustdecodebounds = false;
int w = newopts.outwidth;
int h = newopts.outheight;
//Now the mainstream phone is more 800*480 resolution, so the height and width we set to
float hh = 800f;//Here Set the height to 800f
float ww = 480f;//here set width to 480f
//Zoom ratio. Because it is a fixed scale, only one of the data with high or wide is calculated
int be = 1;//be=1 means no scaling
If (W > H && w > ww) {//If the width is large, zoom according to the fixed width size
be = (int) (NEWOPTS.OUTWIDTH/WW);
} else if (W < h && h > hh) {//if height is scaled according to width fixed size
be = (int) (NEWOPTS.OUTHEIGHT/HH);
        }
if (be <= 0)
be = 1;
newopts.insamplesize = be;//Setting the zoom ratio
//re-read the picture, note that the options.injustdecodebounds is now set back to False
bitmap = Bitmapfactory.decodefile (Srcpath, newopts);
return Compressimage (bitmap);//compress the proportions and then compress the mass
    }

Picture proportional compression, I see an algorithm that says faster.
be = (int) ((w/standard_width + h/standard_height)/2);
Conclusion Two: Picture proportional compression multiplier is (width compression multiple + height compression multiple)/2.


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


Private Bitmap Comp (Bitmap image) {

Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
image.compress (Bitmap.CompressFormat.JPEG, BAOs);
if (Baos.tobytearray (). length/1024>1024) {//Judge if the picture is larger than 1M, compress to avoid overflow when generating the picture (Bitmapfactory.decodestream)
Baos.reset ();//Reset BAOs is empty BAOs
image.compress (Bitmap.CompressFormat.JPEG, 50%, BAOs);//compress the compressed data into BAOs .
        }
Bytearrayinputstream ISBM = new Bytearrayinputstream (Baos.tobytearray ());
bitmapfactory.options newopts = new Bitmapfactory.options ();
///start reading the picture and 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;
//Now the mainstream phone is more 800*480 resolution, so the height and width we set to
float hh = 800f;//Here Set the height to 800f
float ww = 480f;//here set width to 480f
//Zoom ratio. Because it is a fixed scale, only one of the data with high or wide is calculated
int be = 1;//be=1 means no scaling
If (W > H && w > ww) {//If the width is large, zoom according to the fixed width size
be = (int) (NEWOPTS.OUTWIDTH/WW);
} else if (W < h && h > hh) {//if height is scaled according to width fixed size
be = (int) (NEWOPTS.OUTHEIGHT/HH);
        }
if (be <= 0)
be = 1;
newopts.insamplesize = be;//Setting the zoom ratio
newopts.inpreferredconfig = config.rgb_565;//lower picture from ARGB888 to RGB565
//re-read 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 Compressimage (bitmap);//compress the proportions and then compress the mass
    }

3 examples of how to compress Android images

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.