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