An example of 3 methods for Android image compression _android

Source: Internet
Author: User

Android Image compression method:

First: Quality compression method:

Copy Code code as follows:

Private Bitmap Compressimage (Bitmap image) {

Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
Image.compress (Bitmap.CompressFormat.JPEG, BAOs);//mass compression method, where 100 means no compression, the compressed data stored in the BAOs
int options = 100;
while (Baos.tobytearray (). length/1024>100) {//loops to determine if the picture is larger than 100kb after compression, greater than continue compression
Baos.reset ()//reset BAOs Clear BAOs
Options-= 10;//is reduced by 10 each time
Image.compress (Bitmap.CompressFormat.JPEG, Options, BAOs)//Here compress the options% and store the compressed data in the BAOs

}
Bytearrayinputstream ISBM = new Bytearrayinputstream (Baos.tobytearray ())//The compressed data BAOs stored in the Bytearrayinputstream
Bitmap Bitmap = Bitmapfactory.decodestream (ISBM, NULL, NULL);//To generate a picture of bytearrayinputstream data
return bitmap;
}


Second: The picture by the proportional size compression method (obtains the picture according to the path and compresses):

Copy Code code as follows:

Private Bitmap getimage (String srcpath) {
Bitmapfactory.options newopts = new Bitmapfactory.options ();
Start reading into the picture and set options.injustdecodebounds back to True
Newopts.injustdecodebounds = true;
Bitmap Bitmap = Bitmapfactory.decodefile (srcpath,newopts);//This time return bm is empty

Newopts.injustdecodebounds = false;
int w = newopts.outwidth;
int h = newopts.outheight;
Now mainstream phones are more 800*480 resolution, so high and wide we set to
float hh = 800f;//Set height to 800f here
float ww = 480f;//set width to 480f here
Scaling ratio. Because it is a fixed scaling scale, only one of the data is high or wide to calculate
int be = 1;//be=1 means no scaling
if (W > H && w > ww) {//If the width is large zoom based on width fixed 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;//Set Scale
Re-read the picture, note that the options.injustdecodebounds has been set back to false at this time
Bitmap = Bitmapfactory.decodefile (Srcpath, newopts);
Return Compressimage (bitmap);//compress a good proportional size and then mass compress
}

Picture proportional compression, I saw an algorithm, said relatively fast.
be = (int) ((w/standard_width + h/standard_height)/2);
Conclusion Two: Picture proportional compression Multiple is (width compression multiple + height compression multiple)/2.


Third: The picture by the proportional size compression method (according to bitmap picture compression):

Copy Code code as follows:

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 greater than 1M, compress to avoid overflow when generating picture (Bitmapfactory.decodestream)
Baos.reset ()//reset BAOs Clear BAOs
Image.compress (Bitmap.CompressFormat.JPEG, BAOs), Compress 50%, store the compressed data in the BAOs
}
Bytearrayinputstream ISBM = new Bytearrayinputstream (Baos.tobytearray ());
Bitmapfactory.options newopts = new Bitmapfactory.options ();
Start reading into the picture and set 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 mainstream phones are more 800*480 resolution, so high and wide we set to
float hh = 800f;//Set height to 800f here
float ww = 480f;//set width to 480f here
Scaling ratio. Because it is a fixed scaling scale, only one of the data is high or wide to calculate
int be = 1;//be=1 means no scaling
if (W > H && w > ww) {//If the width is large zoom based on width fixed 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;//Set Scale
Newopts.inpreferredconfig = config.rgb_565;//Lower picture from ARGB888 to RGB565
Re-read the picture, 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 a good proportional size and then mass compress
}

Related Article

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.