android 圖片壓縮方法:
第一:質量壓縮法:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//質量壓縮方法,這里100表示不壓縮,把壓縮后的數據存放到baos中
int options = 100;
while ( baos.toByteArray().length / 1024>100) { //循環判斷如果壓縮后圖片是否大于100kb,大于繼續壓縮
baos.reset();//重置baos即清空baos
options -= 10;//每次都減少10
image.compress(Bitmap.CompressFormat.JPEG, options, baos);//這里壓縮options%,把壓縮后的數據存放到baos中
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把壓縮后的數據baos存放到ByteArrayInputStream中
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream數據生成圖片
return bitmap;
}
圖片比例壓縮時, 我看到一個算法,說比較快。。
be = (int) ((w / STANDARD_WIDTH + h/ STANDARD_HEIGHT) / 2);
結論二:圖片比例壓縮倍數 就是 (寬度壓縮倍數+高度壓縮倍數)/2..
第三:圖片按比例大小壓縮方法(根據Bitmap圖片壓縮):
新聞熱點
疑難解答
圖片精選