First: Let's look at the mass compression method first:
Java code
- 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>) { //cycle to determine if the image is larger than 100kb after compression, /c2>
- Baos.reset (); //Reset BAOs to Empty BAOs
- Image.compress (Bitmap.CompressFormat.JPEG, Options, BAOs); //Compress the options% here and store the compressed data in the BAOs
- options = Ten; Each time, it's reduced by ten
- }
- Bytearrayinputstream ISBM = new Bytearrayinputstream (Baos.tobytearray ()); Store the compressed data BAOs in the Bytearrayinputstream
- Bitmap Bitmap = Bitmapfactory.decodestream (ISBM, null, null); Generate images from the Bytearrayinputstream data
- return bitmap;
- }
Second: Picture by proportional size compression method (get picture according to Path and compress):
Java code
- 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 NULL 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; //Set height to 800f here
- float ww = 480f; //Set width to 480f here
- //Zoom ratio. Because it is a fixed scale, only one of the data with high or wide is calculated
- int be = 1; Be=1 indicates 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; //Set zoom ratio
- //re-read the picture, note that the options.injustdecodebounds is now set back to False
- Bitmap = Bitmapfactory.decodefile (Srcpath, newopts);
- return Compressimage (bitmap); //compress the proportional size before compressing the mass.
- }
Third: Image compression by proportional size (according to bitmap image compression):
Java code
- Private Bitmap Comp (Bitmap image) {
- Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
- Image.compress (Bitmap.CompressFormat.JPEG, BAOs);
- if (Baos.tobytearray (). Length/ 1024>) {//Judging if the picture is greater than 1M, Compress to avoid overflow when creating pictures (Bitmapfactory.decodestream)
- Baos.reset (); //Reset BAOs to Empty BAOs
- Image.compress (Bitmap.CompressFormat.JPEG, BAOs); This compresses 50% and stores the compressed data in the 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;
- //Now the mainstream phone is more 800*480 resolution, so the height and width we set to
- float hh = 800f; //Set height to 800f here
- float ww = 480f; //Set width to 480f here
- //Zoom ratio. Because it is a fixed scale, only one of the data with high or wide is calculated
- int be = 1; Be=1 indicates 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; //Set zoom ratio
- //re-read the picture, note that the options.injustdecodebounds is now set back to False
- ISBM = New Bytearrayinputstream (Baos.tobytearray ());
- Bitmap = Bitmapfactory.decodestream (ISBM, null, newopts);
- return Compressimage (bitmap); //compress the proportional size before compressing the mass.
- }
Android more reliable image compression