话不多少,直接上代码:
public static Bitmap compressImage(InputStream in) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len = 0;
byte[] buffer = new byte[1024];
try {
while((len = in.read(buffer)) != -1){
baos.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}
byte[] data = baos.toByteArray();
Options opts = new Options();
opts.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(data, 0, data.length, opts );
int width = opts.outWidth;
int height = opts.outHeight;
int wCompress = 60;
int hCompress = 60;
int wScale = width/wCompress;
int hScale = height/hCompress;
int scale = Math.max(wScale, hScale);
if(scale <= 0){
scale = 1;
}
opts.inJustDecodeBounds = false;
opts.inSampleSize = scale;
return BitmapFactory.decodeByteArray(data, 0, data.length, opts);
}