1. 程式人生 > >Android 之 圖片壓縮

Android 之 圖片壓縮

來自 子墨部落格 http://blog.csdn.net/elinavampire

從圖片的壓縮方式區分:質量壓縮和尺寸壓縮。

質量壓縮是在保持畫素的前提下改變圖片的位深及透明度等,來達到壓縮圖片的目的,經過它壓縮的圖片檔案大小會有改變,但是匯入成bitmap後佔得記憶體是不變的。因為要保持畫素不變,所以它就無法無限壓縮,到達一個值之後就不會繼續變小了。顯然這個方法並不適用與縮圖,其實也不適用於想通過壓縮圖片減少記憶體的適用,僅僅適用於想在保證圖片質量的同時減少檔案大小的情況而已

尺寸壓縮是壓縮圖片的畫素,一張圖片所佔記憶體的大小 圖片型別*寬*高,通過改變三個值減小圖片所佔的記憶體,防止OOM,當然這種方式可能會使圖片失真

質量壓縮:

<code class="hljs cs has-numbering"><span class="hljs-keyword">public</span> Bitmap <span class="hljs-title">compressImage</span>(Bitmap image,<span class="hljs-keyword">int</span> imageSize) {  

        ByteArrayOutputStream baos = <span class="hljs-keyword">new</span> ByteArrayOutputStream();  
        image.compress(Bitmap.CompressFormat.JPEG, <span class="hljs-number">100</span>, baos);<span class="hljs-comment">//質量壓縮方法,這裡100表示不壓縮,把壓縮後的資料存放到baos中  </span>
        <span class="hljs-keyword">int</span> options = <span class="hljs-number">100</span>;  
        <span class="hljs-keyword">while</span> ( baos.toByteArray().length / <span class="hljs-number">1024</span>>imageSize) {  <span class="hljs-comment">//迴圈判斷如果壓縮後圖片是否大於100kb,大於繼續壓縮         </span>
            baos.reset();<span class="hljs-comment">//重置baos即清空baos  </span>
            image.compress(Bitmap.CompressFormat.JPEG, options, baos);<span class="hljs-comment">//這裡壓縮options%,把壓縮後的資料存放到baos中  </span>
            options -= <span class="hljs-number">10</span>;<span class="hljs-comment">//每次都減少10  </span>
        }  
        ByteArrayInputStream isBm = <span class="hljs-keyword">new</span> ByteArrayInputStream(baos.toByteArray());<span class="hljs-comment">//把壓縮後的資料baos存放到ByteArrayInputStream中  </span>
        Bitmap bitmap = BitmapFactory.decodeStream(isBm, <span class="hljs-keyword">null</span>, <span class="hljs-keyword">null</span>);<span class="hljs-comment">//把ByteArrayInputStream資料生成圖片  </span>
        <span class="hljs-keyword">return</span> bitmap;  
    } </code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li></ul>

尺寸壓縮:

<code class="hljs avrasm has-numbering">public void scalePic(int reqWidth,int reqHeight) {
        BitmapFactory<span class="hljs-preprocessor">.Options</span> options = new BitmapFactory<span class="hljs-preprocessor">.Options</span>()<span class="hljs-comment">;</span>
        options<span class="hljs-preprocessor">.inJustDecodeBounds</span> = true<span class="hljs-comment">;</span>
        BitmapFactory<span class="hljs-preprocessor">.decodeResource</span>(getResources(), R<span class="hljs-preprocessor">.mipmap</span><span class="hljs-preprocessor">.demo</span>, options)<span class="hljs-comment">;</span>
        options<span class="hljs-preprocessor">.inSampleSize</span> = PhotoUtil<span class="hljs-preprocessor">.calculateInSampleSize</span>(options, reqWidth,reqHeight)<span class="hljs-comment">;</span>
        options<span class="hljs-preprocessor">.inJustDecodeBounds</span> = false<span class="hljs-comment">;</span>
        bitmap = BitmapFactory<span class="hljs-preprocessor">.decodeResource</span>(getResources(), R<span class="hljs-preprocessor">.mipmap</span><span class="hljs-preprocessor">.demo</span>, options)<span class="hljs-comment">;</span>

        postInvalidate()<span class="hljs-comment">;</span>
    }</code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li></ul>
<code class="hljs java has-numbering"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> <span class="hljs-title">calculateInSampleSize</span>(BitmapFactory.Options options,<span class="hljs-keyword">int</span> reqWidth, <span class="hljs-keyword">int</span> reqHeight) {
        <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> height = options.outHeight;
        <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> width = options.outWidth;
        <span class="hljs-keyword">int</span> inSampleSize = <span class="hljs-number">1</span>;
        <span class="hljs-keyword">if</span> (height > reqHeight || width > reqWidth) {
            <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> heightRatio = Math.round((<span class="hljs-keyword">float</span>) height / (<span class="hljs-keyword">float</span>) reqHeight);
            <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> widthRatio = Math.round((<span class="hljs-keyword">float</span>) width / (<span class="hljs-keyword">float</span>) reqWidth);
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }
        <span class="hljs-keyword">return</span> inSampleSize;
    }</code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li></ul>

根據具體需求也可以兩種壓縮方式結合使用