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);//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
Image.compress (Bitmap.CompressFormat.JPEG, Options, BAOs)//Here compress the options% and store the compressed data in the BAOs
Options-= 10;//is reduced by 10 each time
}
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):
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
}
Third: The picture by the proportional size compression method (according to bitmap picture 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 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
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
}