圖片壓縮那些事

//判斷照片角度
    private final static int getDegress(String path) {
        int degree = 0;
        try {
            ExifInterface exifInterface = new ExifInterface(path);
            int orientation = exifInterface.getAttributeInt(
                    ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                degree = 90;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                degree = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                degree = 270;
                break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return degree;
    }

    /**
     * rotate the bitmap 
     * @param bitmap
     * @param degress
     * @return
     */
    private static Bitmap rotateBitmap(Bitmap bitmap, int degress) {
        if (bitmap != null) {
            Matrix m = new Matrix();
            m.postRotate(degress); 
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
            return bitmap;
        }
        return bitmap;
    }

    /**
     * 壓縮指定路徑圖片,並將其保存在緩存目錄中,經過isDelSrc斷定是否刪除源文件,並獲取到緩存後的圖片路徑
     * @param context
     * @param srcPath
     * @param isDelSrc
     * @return
     */
    public final static String compressBitmap(Context context, String srcPath, boolean isDelSrc) {
        Bitmap bitmap = compressImage(srcPath);
        File srcFile = new File(srcPath);
        String desPath = "";
        int degree = getDegress(srcPath);
        try {
            if (degree != 0) bitmap = rotateBitmap(bitmap, degree);
            File file = new File("新路徑"+srcFile.getName());
            desPath = file.getPath();
            FileOutputStream  fos = new FileOutputStream(file);
            if(srcPath.toLowerCase().endsWith("png"))
                bitmap.compress(Bitmap.CompressFormat.PNG, 50, fos);//把壓縮後的數據存放到baos中
            else
                bitmap.compress(Bitmap.CompressFormat.JPEG, 50, fos);
            fos.close();
            if (isDelSrc) srcFile.deleteOnExit();
        } catch (Exception e) {
            Log.e("BitmapHelper-->compressBitmap", e.getMessage()+"");
        }
        return desPath;
    }

    /**
     * 基於質量的壓縮算法, 此方法未 解決壓縮後圖像失真問題
     * <br> 可先調用比例壓縮適當壓縮圖片後,再調用此方法可解決上述問題
     * @param bts
     * @param maxBytes 壓縮後的圖像最大大小 單位爲byte
     * @return
     */
    public final static Bitmap compressImage(Bitmap image, long maxBytes) {
        try {
            int options = 100;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            image.compress(Bitmap.CompressFormat.JPEG, options, baos);//質量壓縮方法,這裏100表示不壓縮,把壓縮後的數據存放到baos中
            while ( baos.toByteArray().length/1024 > maxBytes) {//循環判斷若是壓縮後圖片是否大maxByteskb,大於繼續壓縮
                baos.reset();//重置baos即清空baos
                options -= 10;//每次都減小10
                image.compress(Bitmap.CompressFormat.JPEG, options, baos);//這裏壓縮options%,把壓縮後的數據存放到baos中
            }
            byte[] bts = baos.toByteArray();
            Bitmap bmp = BitmapFactory.decodeByteArray(bts, 0, bts.length);
            return bmp;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 按照圖片的比例進行壓縮
     * @param srcPath
     * @return
     */
    public final static Bitmap compressImage(String srcPath) {
        BitmapFactory.Options newOpts = new BitmapFactory.Options();
        //開始讀入圖片,此時把options.inJustDecodeBounds 設回true了
        newOpts.inJustDecodeBounds = true;
        Bitmap bitmap = BitmapFactory.decodeFile(srcPath,newOpts);
        newOpts.inJustDecodeBounds = false;
        int w = newOpts.outWidth;
        int h = newOpts.outHeight;
        float reqHeight = 800f;//這裏設置高度爲800f
        float reqWidth = 480f;//這裏設置寬度爲480f
        //縮放比。因爲是固定比例縮放,只用高或者寬其中一個數據進行計算便可
        int inSampleSize = 1;
        if (h > reqHeight || w > reqWidth) {
            final int heightRatio = Math.round((float) h/ (float) reqHeight);
            final int widthRatio = Math.round((float) w / (float) reqWidth);
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }
        if (inSampleSize <= 0)
            inSampleSize = 1;
        newOpts.inSampleSize = inSampleSize;//設置縮放比例
        //從新讀入圖片,注意此時已經把options.inJustDecodeBounds 設回false了
        bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
        if(bitmap != null && !srcPath.toLowerCase().endsWith("png"))//png格式的圖片不能進行質量壓縮
            bitmap = compressImage(bitmap,100);//壓縮比如例大小後再進行質量壓縮
        return bitmap;
    }
相關文章
相關標籤/搜索