话不多少,直接上代码:
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);
}
本文提供了一种使用Java实现的图片压缩方法,通过调整图片尺寸来减少内存占用并保持较好的视觉效果。该方法首先读取原始图片数据,然后计算压缩比例,最后生成压缩后的Bitmap对象。
157

被折叠的 条评论
为什么被折叠?



