来自 子墨博客 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>
根据具体需求也可以两种压缩方式结合使用