Image code processing on the Android website

Source: Internet
Author: User

1     /**2 * Get compressed picture (Guenter picture loading corresponding code)3      * 4      * @paramRes5      * @paramresId6      * @paramReqwidth7 * Minimum width required for picture compression size8      * @paramReqheight9 * Required image compression size min. heightTen      * @return One      */ A      Public StaticBitmap Decodesampledbitmapfromresource (Resources res, -             intResId,intReqwidth,intreqheight) { -  the         //first do not load the picture, only get the picture size -         FinalBitmapfactory.options Options =Newbitmapfactory.options (); -         //When Injustdecodebounds is set to true, pictures are not loaded to get picture size information only -Options.injustdecodebounds =true; +         //only the picture information is saved to the options object, and the Decode method does not return the bitmap object - Bitmapfactory.decoderesource (res, resId, options); +  A         //when calculating the compression ratio, such as insamplesize=4, the picture is compressed into the original atOptions.insamplesize =calculateinsamplesize (Options, Reqwidth, - reqheight); -  -         //when Injustdecodebounds is set to False, Bitmapfactory.decode ... It will return the picture object. -Options.injustdecodebounds =false; -         //gets the compressed picture object using the calculated scale value in         returnBitmapfactory.decoderesource (res, resId, options); -     } to  +     /** - * Calculate Compression scale value (Guenter picture load corresponding code) the      *  *      * @paramOptions $ * Parse image configuration informationPanax Notoginseng      * @paramReqwidth - * Minimum width required for picture compression size the      * @paramReqheight + * Required image compression size min. height A      * @return the      */ +      Public Static intcalculateinsamplesize (bitmapfactory.options Options, -             intReqwidth,intreqheight) { $         //Save Picture Original width high value $         Final intHeight =Options.outheight; -         Final intwidth =Options.outwidth; -         //Initialize compression scale to 1 the         intInsamplesize = 1; - Wuyi         //when the image width is high value any one larger than the desired compressed picture width high value, enter the cyclic computing system the         if(Height > Reqheight | | width >reqwidth) { -  Wu             Final intHalfheight = HEIGHT/2; -             Final intHalfwidth = WIDTH/2; About  $             //the compression scale value increases by twice times per cycle, -             //until half of the original width and height is divided by the compression value, which is greater than the desired width and height value -              while((halfheight/insamplesize) >=Reqheight -&& (halfwidth/insamplesize) >=reqwidth) { AInsamplesize *= 2; +             } the         } -         returninsamplesize; $}

Image code processing on the Android website

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.