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
}