Androidで画像を圧縮する方法について

2608 ワード

現在、*品質圧縮-画像サイズを変更しない2つの圧縮画像方式が存在します.*比例圧縮-ピクセル上で圧縮することに相当します.
画像には*file-ディスクの3つの形式があります.*stream-ネットワーク転送、メモリ.*bitmap-メモリ.
bigmapのメモリサイズはピクセル単位で計算されるwidth*heightなので、Androidに写真を表示する必要がある場合は、メモリ消費が大きすぎてアプリが終了しないように比例圧縮しなければなりません.
 /**
     *      
     *
     * @param path       
     * @param width    (  )
     * @param height   (  )
     * @return
     */
    public Bitmap ratioCompress(String path, float width, float height) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.RGB_565;//          ,             
        options.inJustDecodeBounds = true;//          Bitmap          

        Bitmap bitmap = BitmapFactory.decodeFile(path, options);//     Bitmap   

        int outWidth = options.outWidth;
        int outHeight = options.outHeight;

        /**
         *      
         */
        int ratio = 1;//   ,1      
        if (outWidth > outHeight && outWidth > width) {//        
            ratio = (int) (outWidth / width);
        } else if (outWidth < outHeight && outHeight > height) {//        
            ratio = (int) (outHeight / height);
        }
        if (ratio <= 0) {
            ratio = 1;
        }

        options.inSampleSize = ratio;//     
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(path, options);//  
    }

简単じゃないかO(∩∩)Oハハ~