1. 程式人生 > >android -------- 壓縮圖片檔案工具類

android -------- 壓縮圖片檔案工具類

專案中常常遇到檔案壓縮問題,上傳檔案大小限制

今天簡單的分享一點乾貨,檔案壓縮,圖片壓縮,壓縮Bitmap

主要通過尺寸壓縮和質量壓縮,以達到清晰度最優

 

效果圖

        

 

 

原始碼地址: https://gitee.com/zhangqie/android-util/tree/tool-model/

 

工具類程式碼

public class CompressHelper {
    private static volatile CompressHelper INSTANCE;

    
private Context context; /** * 最大寬度,預設為720 */ private float maxWidth = 720.0f; /** * 最大高度,預設為960 */ private float maxHeight = 960.0f; /** * 預設壓縮後的方式為JPEG */ private Bitmap.CompressFormat compressFormat = Bitmap.CompressFormat.JPEG; /** * 預設的圖片處理方式是ARGB_8888
*/ private Bitmap.Config bitmapConfig = Bitmap.Config.ARGB_8888; /** * 預設壓縮質量為80 */ private int quality = 80; /** * 儲存路徑 */ private String destinationDirectoryPath; /** * 檔名字首 */ private String fileNamePrefix; /** * 檔名 */ private String fileName;
public static CompressHelper getDefault(Context context) { if (INSTANCE == null) { synchronized (CompressHelper.class) { if (INSTANCE == null) { INSTANCE = new CompressHelper(context); } } } return INSTANCE; } private CompressHelper(Context context) { this.context = context; destinationDirectoryPath = context.getCacheDir().getPath() + File.pathSeparator + FileUtil.FILES_PATH; } /** * 壓縮成檔案 * @param file 原始檔案 * @return 壓縮後的檔案 */ public File compressToFile(File file) { return BitmapUtil.compressImage(context, Uri.fromFile(file), maxWidth, maxHeight, compressFormat, bitmapConfig, quality, destinationDirectoryPath, fileNamePrefix, fileName); } /** * 壓縮為Bitmap * @param file 原始檔案 * @return 壓縮後的Bitmap */ public Bitmap compressToBitmap(File file) { return BitmapUtil.getScaledBitmap(context, Uri.fromFile(file), maxWidth, maxHeight, bitmapConfig); } /** * 採用建造者模式,設定Builder */ public static class Builder { private CompressHelper mCompressHelper; public Builder(Context context) { mCompressHelper = new CompressHelper(context); } /** * 設定圖片最大寬度 * @param maxWidth 最大寬度 */ public Builder setMaxWidth(float maxWidth) { mCompressHelper.maxWidth = maxWidth; return this; } /** * 設定圖片最大高度 * @param maxHeight 最大高度 */ public Builder setMaxHeight(float maxHeight) { mCompressHelper.maxHeight = maxHeight; return this; } /** * 設定壓縮的字尾格式 */ public Builder setCompressFormat(Bitmap.CompressFormat compressFormat) { mCompressHelper.compressFormat = compressFormat; return this; } /** * 設定Bitmap的引數 */ public Builder setBitmapConfig(Bitmap.Config bitmapConfig) { mCompressHelper.bitmapConfig = bitmapConfig; return this; } /** * 設定壓縮質量,建議80 * @param quality 壓縮質量,[0,100] */ public Builder setQuality(int quality) { mCompressHelper.quality = quality; return this; } /** * 設定目的儲存路徑 * @param destinationDirectoryPath 目的路徑 */ public Builder setDestinationDirectoryPath(String destinationDirectoryPath) { mCompressHelper.destinationDirectoryPath = destinationDirectoryPath; return this; } /** * 設定檔案字首 * @param prefix 字首 */ public Builder setFileNamePrefix(String prefix) { mCompressHelper.fileNamePrefix = prefix; return this; } /** * 設定檔名稱 * @param fileName 檔名 */ public Builder setFileName(String fileName) { mCompressHelper.fileName = fileName; return this; } public CompressHelper build() { return mCompressHelper; } } }

 

使用

  File oldFile = CompressHelper.getDefault(getApplicationContext()).compressToFile(file);

 

自定義屬性使用

File newFile = new CompressHelper.Builder(this)
                .setMaxWidth(720)  // 預設最大寬度為720
                .setMaxHeight(960) // 預設最大高度為960
                .setQuality(80)    // 預設壓縮質量為80
                .setFileName(yourFileName) // 檔名稱
                .setCompressFormat(CompressFormat.JPEG) // 設定預設壓縮為jpg格式
                .setDestinationDirectoryPath(Environment.getExternalStoragePublicDirectory(
                        Environment.DIRECTORY_PICTURES).getAbsolutePath())//路徑
                .build()
                .compressToFile(oldFile);

 

該案例參考了: