編輯:關於Android編程
Android圖片壓縮幾種方式總結
圖片壓縮在Android開發中很常見也很重要,防止圖片的OOM也是壓縮的重要原因。
首先看下Bitmap圖片文件的大小的決定因素:
Bitmap所占用的內存 = 圖片長度 x 圖片寬度 x 一個像素點占用的字節數。3個參數,任意減少一個的值,就達到了壓縮的效果。
接下來看下Bitmap圖片的幾種格式的特點:
ALPHA_8
表示8位Alpha位圖,即A=8,一個像素點占用1個字節,它沒有顏色,只有透明度
ARGB_4444
表示16位ARGB位圖,即A=4,R=4,G=4,B=4,一個像素點占4+4+4+4=16位,2個字節
ARGB_8888
表示32位ARGB位圖,即A=8,R=8,G=8,B=8,一個像素點占8+8+8+8=32位,4個字節
RGB_565
表示16位RGB位圖,即R=5,G=6,B=5,它沒有透明度,一個像素點占5+6+5=16位,2個字節
如果進行圖片格式的壓縮的話,一般情況下都是ARGB_8888轉為RGB565進行壓縮。
寫了一個工具類,基本上列舉了android上圖片的幾種基本壓縮方式:
1.質量壓縮
2.采樣率壓縮
3.尺寸壓縮
4.Matrix壓縮
5.圖片格式的壓縮,例如PNG和JPG保存後的圖片大小是不同的
public class Utils {
/**
* 采樣率壓縮
*
* @param bitmap
* @param sampleSize 采樣率為2的整數倍,非整數倍四捨五入,如4的話,就是原圖的1/4
* @return 尺寸變化
*/
public static Bitmap getBitmap(Bitmap bitmap, int sampleSize) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = sampleSize;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] bytes = baos.toByteArray();
Bitmap bit = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
Log.i("info", "圖片大小:" + bit.getByteCount());//2665296 10661184
return bit;
}
/**
* 圖片質量壓縮
*
* @param bitmap
* @param quality
* @return 尺寸不變,質量變小
*/
public static Bitmap compressByQuality(Bitmap bitmap, int quality) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
byte[] bytes = baos.toByteArray();
Bitmap bit = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
Log.i("info", "圖片大小:" + bit.getByteCount());//10661184
return bit;
}
/**
* 圖片質量壓縮
*
* @param src
* @param maxByteSize
* @return
*/
public static Bitmap compressByQuality(Bitmap src, long maxByteSize) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int quality = 100;
src.compress(Bitmap.CompressFormat.JPEG, quality, baos);
while (baos.toByteArray().length > maxByteSize && quality > 0) {
baos.reset();
src.compress(Bitmap.CompressFormat.JPEG, quality -= 5, baos);
}
if (quality < 0) return null;
byte[] bytes = baos.toByteArray();
Bitmap bit = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
return bit;
}
public static Bitmap compressByFormat(Bitmap bitmap, int format) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] bytes = baos.toByteArray();
Bitmap bit = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
Log.i("info", "圖片大小:" + bit.getByteCount());//10661184
return bit;
}
/**
* Matrix縮放
*
* @param bitmap
* @param scaleWidth
* @param scaleHeight
* @return 尺寸和大小變化
*/
public static Bitmap getBitmapBySize(Bitmap bitmap, float scaleWidth, float scaleHeight) {
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap bit = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
Log.i("info", "圖片大小:" + bit.getByteCount());
return bit;
}
/**
* 按照圖片格式配置壓縮
*
* @param path
* @param config ALPHA_8,ARGB_4444,ARGB_8888,RGB_565;
* @return RGB_565比ARGB_8888節省一半內存
*/
public static Bitmap getBitmapByFormatConfig(String path, Bitmap.Config config) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = config;
Bitmap bitmap = BitmapFactory.decodeFile(path, options);
Log.i("info", "圖片大小:" + bitmap.getByteCount());
return bitmap;
}
/**
* 指定大小縮放
*
* @param bitmap
* @param width
* @param height
* @return
*/
public static Bitmap getBitmapByScaleSize(Bitmap bitmap, int width, int height) {
Bitmap bit = Bitmap.createScaledBitmap(bitmap, width, height, true);
Log.i("info", "圖片大小:" + bit.getByteCount());
return bit;
}
/**
* 通過保存格式壓縮
*
* @param bitmap
* @param format JPEG,PNG,WEBP
* @return
*/
public static Bitmap getBitmapByFormat(Bitmap bitmap, Bitmap.CompressFormat format) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(format, 100, baos);
byte[] bytes = baos.toByteArray();
Bitmap bit = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
Log.i("info", "圖片大小:" + bit.getByteCount());
return bit;
}
/**
* 文件加載壓縮
*
* @param filePath
* @param inSampleSize
* @return
*/
public static Bitmap getBitmap(String filePath, int inSampleSize) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);//此時不耗費和占用內存
options.inSampleSize = inSampleSize;
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filePath, options);
}
public static Bitmap getBitmap(String filePath) {
return BitmapFactory.decodeFile(filePath);
}
public static Bitmap view2Bitmap(View view) {
if (view == null) return null;
Bitmap ret = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(ret);
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null) {
bgDrawable.draw(canvas);
} else {
canvas.drawColor(Color.WHITE);
}
view.draw(canvas);
return ret;
}
public static void saveBitmap(Bitmap bitmap) {
File file = new File(Environment.getExternalStorageDirectory() + "/img.jpg");
try {
FileOutputStream fileOutputStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void saveBitmap(Bitmap bitmap,Bitmap.CompressFormat format) {
File file = new File(Environment.getExternalStorageDirectory() + "/img.jpg");
try {
FileOutputStream fileOutputStream = new FileOutputStream(file);
bitmap.compress(format, 100, fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
Android Zxing二維碼掃描圖片拉伸的解決方法
二維碼掃描,Android Zxing圖片拉伸解決。Zxing是google提供的二維碼掃描工程默認是橫屏的 轉換成豎屏後圖片出現拉伸 這裡提供解決
Android Gson使用入門及GsonFormat插件的使用
Gson 是 Google 官方提供的用來在 Java 對象和 JSON 之間進行互相轉換的Java類庫。我之前在使用Eclipse開發Android的時候,並沒有經常使
如何更高效的使用MVP以及官方MVP架構解析
關於presenter一直持有Activity對象導致的內存洩漏問題只要用過mvp這個問題可能很多人都知道。寫mvp的時候,presenter會持有view,如果pres
魅藍metal能root嗎 魅藍metal root教程說明
由於這次的魅藍metal采用的是基於阿裡開發的虛擬機核心的flymeOS,雖然能兼容普遍的安卓應用,但很可惜是不能root的,因此也失去了很多搞基的樂趣。下