Android學習之圖片壓縮,壓縮程度高且失真度小

來源:互聯網
上載者:User

標籤:turn   nsa   factor   path   roi   表示   bsp   壓縮   als   

        曾經在做手機上傳圖片的時候。直接擷取相機拍攝的原圖上傳,原圖大小一般1~2M。因此上傳一張都比較浪費資源,有些情境還須要圖片多張上傳,所以近期查看了好多前輩寫的關於圖片處理的資料。然後試著改了一個圖片壓縮的方法。經測試用著還不錯。壓縮效果非常理想,主要是失真不明顯。在這裡分享一下。

        以下直接上代碼:

/** * 上傳server時把圖片調用以下方法壓縮後 儲存到暫時目錄 圖片壓縮後小於200KB。失真度不明顯 *  * @param path * @return * @throws IOException */public static Bitmap revitionImageSize(String path) throws IOException {BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(path)));BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;BitmapFactory.decodeStream(in, null, options);in.close();int i = 0;Bitmap bitmap = null;// options.inJustDecodeBounds=true那麼將不返回實際的bitmap對象,不給其分配記憶體空間可是能夠得到一些解碼邊界資訊即圖片大小等資訊// outHeight(圖片原始高度)和 outWidth(圖片的原始寬度)// inSampleSize表示縮圖大小為原始圖片大小的幾分之中的一個// options.outWidth >> i(右移運算子)表示:outWidth/(2^i)while (true) {if ((options.outWidth >> i <= 2000)&& (options.outHeight >> i <= 2000)) {in = new BufferedInputStream(new FileInputStream(new File(path)));options.inSampleSize = (int) Math.pow(2.0D, i); // 冪運算 i為幾次方options.inJustDecodeBounds = false;bitmap = BitmapFactory.decodeStream(in, null, options);break;}i += 1;}return bitmap;}


Android學習之圖片壓縮,壓縮程度高且失真度小

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.