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. 100 means no compression, and the compressed data is stored in the BAOs.
int options = 100;
while (Baos.tobytearray (). length/1024>100) {//loop inference assumes that the picture is larger than 100kb after compression, which is 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 lower each 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: Picture by proportional size compression method (get picture from 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;
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 width to 480f
Scaling ratio. Because it is a fixed scale, it can be calculated only with one data of high or wide
int be = 1;//be=1 means no scaling
if (W > H && w > ww) {//Assuming a large width, scale according to the fixed width size
be = (int) (NEWOPTS.OUTWIDTH/WW);
} else if (W < h && h > hh) {//assuming 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
Read the picture again, note that this time the options.injustdecodebounds is set back to False
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) {//inference assumes that 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 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;
Today mainstream phones are much 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 fixed scale scaling. Only one of the high or wide data can be used to calculate
int be = 1;//be=1 means no scaling
if (W > H && w > ww) {//Assuming a large width, scale according to the fixed width size
be = (int) (NEWOPTS.OUTWIDTH/WW);
} else if (W < h && h > hh) {//assuming 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
Read the picture again, note that this time the options.injustdecodebounds is 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
}

Android Image compression method

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.