Package com.funs.compressImg.utils;
Import Java.io.ByteArrayInputStream;
Import Java.io.ByteArrayOutputStream;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapFactory;
/** Picture Compression Helper class, there are three different ways to compress pictures:
* 1, Quality compression method
* 2, according to the path by proportional size compression
* 3, according to bitmap proportional size compression
*/
public class Imagecompressutil {
/**
* Mass compression method
* @param image Compressed resource picture (BITMAP)
* @param aimsize image compression target size, in kilobytes
*/
public static Bitmap Compressimage (Bitmap image, int aimsize) {
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;//Defines the compression scale, initially not compressed
while ((Baos.tobytearray (). length/1024) > Aimsize) {//cycle to determine if the picture is larger than the destination size, if it is greater then continue to compress
Baos.reset ();//reset BAOs, i.e. empty BAOs
Image.compress (Bitmap.CompressFormat.JPEG, Options, BAOs);//compress the image options% and store the compressed data in BAOs
Options-= 10;//options reduces each time by 10
}
Bytearrayinputstream Bais = new Bytearrayinputstream (Baos.tobytearray ());//Store compressed data BAOs into Bytearrayinputstream
Bitmap Bitmap = Bitmapfactory.decodestream (Bais, NULL, NULL);//Generate pictures of Bytearrayinputstream data
return bitmap;
}
/**
* Scale the width of the resource picture proportionally and then compress it by mass. Note: Images can only be scaled down and not enlarged
* @param imgpath Resource picture path
* @param aimwidth the desired width to reduce the image to px
* @param aimheight the desired height to reduce the image to px
* @param aimsize image compression target size, in kilobytes
*/
public static Bitmap GetImage (String imgpath, float aimwidth, float aimheight,int aimsize) {
Bitmapfactory.options opts = new Bitmapfactory.options ();
Start to read the picture, at this time set Options.injustdecodebounds to True, only to the width of the higher parameter, do not pass the real data
Opts.injustdecodebounds = true;
Bitmap Bitmap = Bitmapfactory.decodefile (Imgpath, opts);//The Bitmap returned at this time is empty
Opts.injustdecodebounds = false;
int width = opts.outwidth;
int height = opts.outheight;
Scaling ratio, due to a fixed scale reduction, only one of the high or wide one of the data to calculate, the value of 1 means not to shrink
int ratio = 1;
if (Width > Height && width > aimwidth) {
Ratio = (int) (width/aimwidth);
}else if (height > width && height > aimheight) {
Ratio = (int) (height/aimheight);
}
if (ratio <= 0) {
Ratio = 1;
}
Set scale down
Opts.insamplesize = ratio;
Re-read the picture and note that the Options.injustdecodebounds has been set back to false at this time
Bitmap = Bitmapfactory.decodefile (Imgpath, opts);
Return Compressimage (bitmap, aimsize);
}
/**
* Scale the width of the resource picture proportionally and then compress it by mass. Note: Images can only be scaled down and not enlarged
* @param image resource picture (BITMAP)
* @param aimwidth the desired width to reduce the image to px
* @param aimheight the desired height to reduce the image to px
* @param aimsize image compression target size, in kilobytes
*/
public static Bitmap Comp (Bitmap image, float aimwidth, float aimheight,int aimsize) {
Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
Image.compress (Bitmap.CompressFormat.JPEG, BAOs);
if (Baos.tobytearray () length/1024) > 1024) {//Determine if the picture is greater than 1M, if it is, compress it to avoid creating the picture ()
Baos.reset ();//reset BAOs, that is, empty baos.
Image.compress (Bitmap.CompressFormat.JPEG, 50%, BAOs);//compress the compressed data into BAOs.
}
Bytearrayinputstream Bais = new Bytearrayinputstream (Baos.tobytearray ());
Bitmapfactory.options opts = new Bitmapfactory.options ();
Start reading the picture, this time set the Options.injustdecodebounds back to True
Opts.injustdecodebounds = true;
Bitmap Bitmap = Bitmapfactory.decodestream (Bais, NULL, opts);
Opts.injustdecodebounds = false;
int width = opts.outwidth;
int height = opts.outheight;
Scaling ratio, due to a fixed scale, only one of the high or wide data to calculate, 1 means not to shrink
int ratio = 1;
if (Width > Height && width > aimwidth) {
Ratio = (int) (width/aimwidth);
}else if (height > width && height > aimheight) {
Ratio = (int) (height/aimheight);
}
if (ratio <= 0) {
Ratio = 1;
}
Opts.insamplesize = ratio;//Set scale down
Re-read the picture and note that the Options.injustdecodebounds has been set back to false at this time
Bais = new Bytearrayinputstream (Baos.tobytearray ());
Bitmap = Bitmapfactory.decodestream (Bais, NULL, opts);
Return Compressimage (bitmap, aimsize);
}
}
Android---------Compress picture size and size