Android Image compression method

Source: Internet
Author: User

First: Let's look at the mass compression method first
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 = 100;
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
Image.compress (Bitmap.CompressFormat.JPEG, Options, BAOs);//compress options% here and store the compressed data in BAOs
Options-= 10;//10 reduction per time
}
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: Image compression by proportional size (get pictures and compress according to path)
Private Bitmap getimage (String srcpath) {
Bitmapfactory.options newopts = new Bitmapfactory.options ();
Start reading the picture, this time 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
Scaling 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 and note that the Options.injustdecodebounds has been set back to false at this time
Bitmap = Bitmapfactory.decodefile (Srcpath, newopts);
Return Compressimage (bitmap);//compress the proportions and then compress the mass
}

Third: Picture by proportional size compression method (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 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, this time 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
Scaling 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 and note that the Options.injustdecodebounds has been set back to false at this time
ISBM = new Bytearrayinputstream (Baos.tobytearray ());
Bitmap = Bitmapfactory.decodestream (ISBM, NULL, newopts);
Return Compressimage (bitmap);//compress the proportions and then compress the mass
}

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.