1. 程式人生 > >Android圖片壓縮:按等比例壓縮並且質量壓縮

Android圖片壓縮:按等比例壓縮並且質量壓縮

/**
     * 圖片按比例大小壓縮方法
     *
     * @param srcPath (根據路徑獲取圖片並壓縮)
     * @return
     */
    public static File getimage(String srcPath) {
        BitmapFactory.Options newOpts = new BitmapFactory.Options();
        // 開始讀入圖片,此時把options.inJustDecodeBounds 設回true了,只讀取圖片的大小,不分配記憶體
        newOpts.inJustDecodeBounds = true
; Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);// 此時返回bm為空 newOpts.inJustDecodeBounds = false; int w = newOpts.outWidth; int h = newOpts.outHeight; // 現在主流手機比較多是1920*1080解析度,所以高和寬我們設定為 float hh = 1920f;// 這裡設定高度為1920f float ww = 1080f;// 這裡設定寬度為1080f
// 縮放比。由於是固定比例縮放,只用高或者寬其中一個數據進行計算即可 int be = 1;// be=1表示不縮放 if (w > h && w > ww) {// 如果寬度大的話根據寬度固定大小縮放 be = (int) (newOpts.outWidth / ww); } else if (w < h && h > hh) {// 如果高度高的話根據寬度固定大小縮放 be = (int) (newOpts.outHeight / hh); } if
(be <= 0) be = 1; newOpts.inSampleSize = be;// 設定縮放比例 // 重新讀入圖片,注意此時已經把options.inJustDecodeBounds 設回false了 bitmap = BitmapFactory.decodeFile(srcPath, newOpts); //檢測並旋轉圖片 Bitmap bitmap1=changeImageLocate(srcPath,bitmap); return compressImage(bitmap1);// 壓縮好比例大小後再進行質量壓縮 }
//檢測並旋轉圖片
    public static Bitmap changeImageLocate(String filepath, Bitmap bitmap) {
        //根據圖片的filepath獲取到一個ExifInterface的物件
        int degree = 0;
        try {
            ExifInterface exif = new ExifInterface(filepath);
            if (exif != null) {
                // 讀取圖片中相機方向資訊
                int ori = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
                Log.e("degree========ori====", ori + "");

                // 計算旋轉角度
                switch (ori) {
                    case ExifInterface.ORIENTATION_ROTATE_90:
                        degree = 90;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_180:
                        degree = 180;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_270:
                        degree = 270;
                        break;
                    default:
                        degree = 0;
                        break;
                }
                Log.e("degree============", degree + "");
                if (degree != 0) {
                    Log.e("degree============", "degree != 0");
                    // 旋轉圖片
                    Matrix m = new Matrix();
//                    m.setScale(0.5f,0.5f);
                    m.postRotate(degree);

                    bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);

                    return bitmap;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;
    }
//質量壓縮
public static File compressImage(Bitmap bitmap) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 質量壓縮方法,這裡100表示不壓縮,把壓縮後的資料存放到baos中
        int options = 90;

        while (baos.toByteArray().length / 1024 > 300) { // 迴圈判斷如果壓縮後圖片是否大於100kb,大於繼續壓縮
            baos.reset(); // 重置baos即清空baos
            bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);// 這裡壓縮options%,把壓縮後的資料存放到baos中
            options -= 10;// 每次都減少10
        }
        File dirFile = new File(Environment.getExternalStorageDirectory() + "儲存圖片的路徑");
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }

        File file = new File(dirFile.getPath(), System.currentTimeMillis() + ".jpg");
        try {
            FileOutputStream fos = new FileOutputStream(file);
            try {
                fos.write(baos.toByteArray());
                fos.flush();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        recycleBitmap(bitmap);
        return file;
    }
//回收Bitmap
 public static void recycleBitmap(Bitmap... bitmaps) {
        if (bitmaps == null) {
            return;
        }
        for (Bitmap bm : bitmaps) {
            if (null != bm && !bm.isRecycled()) {
                bm.recycle();
            }
        }
    }

只需直接呼叫getimage(srcPath)即可。