Android more reliable image compression

Source: Internet
Author: User
<span id="Label3"></p><p><p>First: Let's look at the mass compression method first:</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">Bitmap compressimage (Bitmap image) {bytearrayoutputstream BAOs</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Bytearrayoutputstream (); Image.compress (Bitmap.CompressFormat.JPEG,</span></span>, baos);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">mass compression method, where 100 means no compression, the compressed data stored in the BAOs</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>options = 100<span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> while</span>(baos.tobytearray (). length/1024>100) {<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">loop to determine if the image is larger than 100kb after compression</span></span>Baos.reset ();<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">reset <span style="color: #008000;">BAOs is empty BAOs</span></span>Image.compress (Bitmap.CompressFormat.JPEG, options, baos);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">this compresses the options% and stores the compressed data in the BAOs</span> .</span>options-= 10;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">each time, it's reduced by ten</span></span><span style="color: #000000;"><span style="color: #000000;">} bytearrayinputstream ISBM</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Bytearrayinputstream (baos.tobytearray ());<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Store the compressed data BAOs in the Bytearrayinputstream</span></span>Bitmap Bitmap = Bitmapfactory.decodestream (isbm,<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span>,<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span>);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">generate images from the Bytearrayinputstream data</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">bitmap; }</span></span></pre><p><p>Second: picture by proportional size compression method (get picture According to Path and compress):</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">Bitmap getimage (String srcpath) {bitmapfactory.options newopts</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">bitmapfactory.options (); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">start reading the picture, this time set the Options.injustdecodebounds back to True</span></span>Newopts.injustdecodebounds =<span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span><span style="color: #000000;"><span style="color: #000000;">; Bitmap Bitmap</span></span>= Bitmapfactory.decodefile (srcpath,newopts);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">return BM is empty at this time</span></span><span style="color: #000000;"><span style="color: #000000;">Newopts.injustdecodebounds</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">false</span></span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>W =<span style="color: #000000;"><span style="color: #000000;">newopts.outwidth; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>h =<span style="color: #000000;"><span style="color: #000000;">newopts.outheight; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">now the mainstream phone is more 800*480 resolution, so the height and width we set to</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">float</span></span>HH = 800f;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">set height to 800f here</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">float</span></span>WW = 480f;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">This sets the width to 480f</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Scaling Ratio. Because it is a fixed scale, only one of the data with high or wide is calculated</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>be = 1;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">be=1 indicates no scaling</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(w > H && w > Ww) {<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">If the width is large, scale according to the fixed width size</span></span>be = (<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>) (newopts.outwidth/<span style="color: #000000;"><span style="color: #000000;">ww); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(w < h && h > Hh) {<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">if height is high, it is scaled according to width fixed size</span></span>be = (<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>) (newopts.outheight/<span style="color: #000000;"><span style="color: #000000;">hh); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(be <= 0<span style="color: #000000;"><span style="color: #000000;">)</span> be</span>= 1<span style="color: #000000;"><span style="color: #000000;">; Newopts.insamplesize</span></span>= be;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">set the zoom ratio</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">re-read the picture and note that the Options.injustdecodebounds has been set back to false at this time</span></span>Bitmap =<span style="color: #000000;"><span style="color: #000000;">bitmapfactory.decodefile (srcpath, newopts); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>Compressimage (bitmap);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">compress the proportions and then compress the Mass.</span></span>}</pre><p><p>Third: image compression by proportional size (according to bitmap image compression):</p></p><pre> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">Bitmap Comp (Bitmap image) {bytearrayoutputstream BAOs</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Bytearrayoutputstream (); Image.compress (Bitmap.CompressFormat.JPEG,</span></span>100<span style="color: #000000;"><span style="color: #000000;">, baos); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(baos.tobytearray (). Length/1024>1024) {<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">determine if the picture is larger than 1M, compress to avoid overflow when generating picture (bitmapfactory.decodestream)</span></span>Baos.reset ();<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">reset <span style="color: #008000;">BAOs is empty BAOs</span></span>Image.compress (Bitmap.CompressFormat.JPEG, baos);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">this compresses 50% and stores the compressed data in the BAOs</span> .</span><span style="color: #000000;"><span style="color: #000000;">} bytearrayinputstream ISBM</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Bytearrayinputstream (baos.tobytearray ()); Bitmapfactory.options newopts</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">bitmapfactory.options (); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">start reading the picture, this time set the Options.injustdecodebounds back to True</span></span>Newopts.injustdecodebounds =<span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span><span style="color: #000000;"><span style="color: #000000;">; Bitmap Bitmap</span></span>= Bitmapfactory.decodestream (isbm,<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">, newopts); Newopts.injustdecodebounds</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">false</span></span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>W =<span style="color: #000000;"><span style="color: #000000;">newopts.outwidth; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>h =<span style="color: #000000;"><span style="color: #000000;">newopts.outheight; </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">now the mainstream phone is more 800*480 resolution, so the height and width we set to</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">float</span></span>HH = 800f;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">set height to 800f here</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">float</span></span>WW = 480f;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">This sets the width to 480f</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Scaling Ratio. Because it is a fixed scale, only one of the data with high or wide is calculated</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>be = 1;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">be=1 indicates no scaling</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(w > H && w > Ww) {<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">If the width is large, scale according to the fixed width size</span></span>be = (<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>) (newopts.outwidth/<span style="color: #000000;"><span style="color: #000000;">ww); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(w < h && h > Hh) {<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">if height is high, it is scaled according to width fixed size</span></span>be = (<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>) (newopts.outheight/<span style="color: #000000;"><span style="color: #000000;">hh); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(be <= 0<span style="color: #000000;"><span style="color: #000000;">)</span> be</span>= 1<span style="color: #000000;"><span style="color: #000000;">; Newopts.insamplesize</span></span>= be;<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">set the zoom ratio</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">re-read the picture and note that the Options.injustdecodebounds has been set back to false at this time</span></span>ISBM =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Bytearrayinputstream (baos.tobytearray ()); Bitmap</span></span>= Bitmapfactory.decodestream (isbm,<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">, newopts); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>Compressimage (bitmap);<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">compress the proportions and then compress the Mass.</span></span>}</pre><p><p></p></p><p><p> Android more reliable image compression </p> </p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.