Android Good image compression method

Source: Internet
Author: User
<span id="Label3"></p><p style="text-align:center"><p style="text-align:center"><span style="font-size:14px"><strong>Android Good image compression method</strong></span></p></p><p><p><span style="font-size:14px"><br></span></p></p><p><p><span style="font-size:14px; color:#cc0000">first, Image quality Compression</span></p></p><p><p><span style="font-size:14px"></span></p></p><pre code_snippet_id="650206" snippet_file_name="blog_20150422_1_5092879" name="code" class="java"><pre code_snippet_id="650206" snippet_file_name="blog_20150422_1_5092879" name="code" class="java">/** * Mass compression method * * @param image * @return * /public static Bitmap compressimage (Bitmap Image) { C6/>bytearrayoutputstream BAOs = new Bytearrayoutputstream (); Image.compress (Bitmap.CompressFormat.JPEG, baos);//quality compression method, here 100 means no compression, the compressed data stored in the BAOs int options =; While (baos.tobytearray (). length/1024 > 100) {//cycle to determine if the image is larger than 100kb after compression, greater than continue compression Baos.reset ();//reset BAOs to empty baos< C11/>image.compress (Bitmap.CompressFormat.JPEG, options, baos);//compress options% here, Store the compressed data in BAOs options- = 10;/ /reduced by ten } bytearrayinputstream ISBM = new Bytearrayinputstream (baos.tobytearray ());// The compressed data BAOs stored in the Bytearrayinputstream Bitmap Bitmap = bitmapfactory.decodestream (isbm, null, null);// Generate a picture of the Bytearrayinputstream data return bitmap; }</pre></pre><br><br><p><p></p></p><p><p><span style="font-size:14px"><br></span></p></p><p><p><span style="font-size:14px"><span style="color:#cc0000">second, compression by proportional size (path to get pictures)</span></span></p></p><p><p><span style="font-size:14px"></span></p></p><pre code_snippet_id="650206" snippet_file_name="blog_20150422_2_5120971" name="code" class="java">/** * Picture by proportional size compression method * * @param srcpath (get picture According to Path and Compress) * @return * */public static Bitmap getimage (strin G Srcpath) {bitmapfactory.options newopts = new Bitmapfactory.options (); Start to read the picture, at this time set the Options.injustdecodebounds back to true newopts.injustdecodebounds = true; Bitmap Bitmap = bitmapfactory.decodefile (srcpath, Newopts);//return BM At this time is empty newopts.injustdecodebounds = false; int w = newopts.outwidth; int h = newopts.outheight; Now mainstream phone more is 800*480 resolution, so the height and width we set to float hh = 800f;//here Set the height for 800f float ww = 480f;//here set Width to 480f//shrink and the Ratio. Because it is a fixed scale, only one of the data can be calculated as high or wide, int be = 1;//be=1 means not to scale if (w > H && w > Ww) {//if the width is large, the size is fixed Put be = (int) (newopts.outwidth/ww); } else if (w < h && h > Hh) {//if height is high, scale is = (int) (newopts.outheight/hh) according to the fixed width size; } If (be <= 0) is = 1; Newopts.insamplesize = Be;//set IndentZoom In//re-read The picture, Note that at this point the options.injustdecodebounds is set back to false bitmap = Bitmapfactory.decodefile (srcpath, newopts) ; Return Compressimage (bitmap);//compress the proportions and then compress the mass again}</pre><br><br><p><p></p></p><p><p><span style="font-size:14px"><br></span></p></p><p><p><span style="font-size:14px"><span style="color:#cc0000">C. Compression by proportional size (Bitmap)</span></span></p></p><p><p><span style="font-size:14px"></span></p></p><pre code_snippet_id="650206" snippet_file_name="blog_20150422_3_821040" name="code" class="java">/** * Picture by proportional size compression method * * @param image (compressed According to Bitmap Picture) * @return * */public static Bitmap Compressscale (B Itmap image) {bytearrayoutputstream BAOs = new Bytearrayoutputstream (); Image.compress (Bitmap.CompressFormat.JPEG, baos); Determine if the picture is larger than 1M, compress to avoid overflow if (baos.tobytearray () when generating the picture (bitmapfactory.decodestream). length/1024 > 1024) { Baos.reset ();//reset BAOs that is empty BAOs image.compress (Bitmap.CompressFormat.JPEG,, baos);//here Compress 50%, Store the compressed data to BAOs medium} Bytearrayinputstream ISBM = new Bytearrayinputstream (baos.tobytearray ()); Bitmapfactory.options newopts = new Bitmapfactory.options (); Start to read the picture, at this time set the Options.injustdecodebounds back to true newopts.injustdecodebounds = true; Bitmap Bitmap = Bitmapfactory.decodestream (isbm, null, newopts); Newopts.injustdecodebounds = false; int w = newopts.outwidth; int h = newopts.outheight; LOG.I (TAG, W + "---------------" + h); Now the mainstream phone is more 800*480 resolution, so high and wide we set to//float HH = 800f;//here Set the height to 800f//float ww = 480f;//here Set width of 480 F Float hh = 512f; float ww = 512f; Scaling Ratio. Because it is a fixed scale, only one of the data can be calculated as high or wide, int be = 1;//be=1 means not to scale if (w > H && w > Ww) {//if the width is large, the size is fixed Put be = (int) (newopts.outwidth/ww); } else if (w < h && h > Hh) {//if height is high, scale is = (int) (newopts.outheight/hh) According to the height fixed size; } If (be <= 0) is = 1; Newopts.insamplesize = be; Set zoom//newopts.inpreferredconfig = config.rgb_565;//reduce the picture from ARGB888 to RGB565//re-read into the picture, note that the Options.injus is now Tdecodebounds set back false ISBM = new Bytearrayinputstream (baos.tobytearray ()); Bitmap = Bitmapfactory.decodestream (isbm, null, newopts); Return Compressimage (bitmap);//compress The proportion size and then carry on the quality Compression//return bitmap; }</pre><br><br><p><p></p></p><p><p><span style="font-size:14px"><br></span></p></p><p><p>Android Good image compression method</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.