1. 程式人生 > >一個圖片處理工具類

一個圖片處理工具類

/**
 * 圖片處理工具類
 */

public class BitMapUtils {
    /**
     * 對指定路徑圖片壓縮改變其檔案大小
     * @param file
     * @param bitmap
     */
    public static void compression(File file,Bitmap bitmap){
        BufferedOutputStream bos = null;
        try {
            bos = new BufferedOutputStream(new FileOutputStream(file));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //第一個引數
        bitmap.compress(Bitmap.CompressFormat.JPEG, 30, bos);
        try {
            bos.flush();
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     *
     * @param file
     * @return  獲取file檔案大小
     * @throws Exception
     */
    public static long getFileSize(File file) throws Exception {
        long size = 0;
        if (file.exists()) {
            FileInputStream fis = null;
            fis = new FileInputStream(file);
            size = fis.available();
        } else {
            file.createNewFile();
            Log.e("獲取檔案大小", "檔案不存在!");
        }
        return size;
    }
    /**
     * 根據圖片路勁壓縮返回bitmap
     * @param srcPath
     * @return
     */
    public static Bitmap compressImageFromFile(String srcPath) {
        BitmapFactory.Options newOpts = new BitmapFactory.Options();
        newOpts.inJustDecodeBounds = true;//只讀邊,不讀內容
        Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);

        newOpts.inJustDecodeBounds = false;
        int w = newOpts.outWidth;
        int h = newOpts.outHeight;
        float hh = 800f;//
        float ww = 480f;//
        int 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;//設定取樣率

        newOpts.inPreferredConfig = Bitmap.Config.ARGB_8888;//該模式是預設的,可不設
        newOpts.inPurgeable = true;// 同時設定才會有效
        newOpts.inInputShareable = true;//。當系統記憶體不夠時候圖片自動被回收

        bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
//      return compressBmpFromBmp(bitmap);//原來的方法呼叫了這個方法企圖進行二次壓縮
        //其實是無效的,大家儘管嘗試
        return bitmap;
    }
    /**
     * 獲取bitmap所佔記憶體大小不同於檔案大小後者是流的形式
     * @param bitmap
     * @return
     */
    public static int getBitmapSize(Bitmap bitmap){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){     //API 19
            return bitmap.getAllocationByteCount();
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1){//API 12
            return bitmap.getByteCount();
        }
        return bitmap.getRowBytes() * bitmap.getHeight();                //earlier version
    }

    /**
     * bitmap轉為檔案儲存返回file檔案
     * @param bitmap
     * @param filepath
     * @return
     */
    public static File saveBitmapFile(Bitmap bitmap, String filepath){
        File file=new File(filepath);//將要儲存圖片的路徑
        try {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
            bos.flush();
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }

}