Code helper class for compressing images in Java

Source: Internet
Author: User

 

Package cn.com. images; import Java. AWT. graphics; import Java. AWT. image; import Java. AWT. image. bufferedimage; import Java. io. file; import Java. io. ioexception; import Java. math. bigdecimal; import Java. math. mathcontext; import Java. util. arraylist; import javax. imageIO. imageIO;/***** operate images ** @ author chenzheng_java * @ since 2011/7/29 **/public class imagehelper {Private Static imagehelper = NULL; public static imagehelper getimagehelper () {If (imagehelper = NULL) {imagehelper = new imagehelper ();} return imagehelper ;} /***** scale the image according to the specified scale ** @ Param sourceimagepath * Source Address * @ Param destinationpath * after the image size is changed * @ Param scale, such as 1.2 */public static void scaleimage (string sourceimagepath, string destinationpath, double scale, string format) {file = new file (sourceimagepath); bufferedimage; try {bufferedimage = ImageIO. read (File); int width = bufferedimage. getwidth (); int Height = bufferedimage. getheight (); width = parsedoubletoint (width * scale); Height = parsedoubletoint (height * scale); image = bufferedimage. getscaledinstance (width, height, image. scale_smooth); bufferedimage outputimage = new bufferedimage (width, height, bufferedimage. type_int_rgb); graphics = outputimage. getgraphics (); graphics. drawimage (image, 0, 0, null); graphics. dispose (); ImageIO. write (outputimage, format, new file (destinationpath);} catch (ioexception e) {system. out. println ("An error occurred while compressing the image using the scaleimage method"); E. printstacktrace ();}} /***** scale the image to the specified height or width * @ Param sourceimagepath Image Source Address * @ Param destinationpath: compress the image address * @ Param width: The scaled width * @ Param height: The scaled height * @ Param auto determines whether to automatically keep the original aspect ratio of the image. Example * @ Param Format: Image Format: JPG */public static void scaleimagewithparams (string sourceimagepath, string destinationpath, int width, int height, Boolean auto, string format) {try {file = new file (sourceimagepath); bufferedimage = NULL; bufferedimage = ImageIO. read (File); If (auto) {arraylist <integer> paramsarraylist = getautowidthandheight (bufferedimage, width, height); width = paramsarraylist. get (0); Height = paramsarraylist. get (1); system. out. println ("Auto Adjust ratio, width =" + width + "Height =" + height);} image = bufferedimage. getscaledinstance (width, height, image. scale_default); bufferedimage outputimage = new bufferedimage (width, height, bufferedimage. type_int_rgb); graphics = outputimage. getgraphics (); graphics. drawimage (image, 0, 0, null); graphics. dispose (); ImageIO. write (outputimage, format, new file (destinationpath);} catch (exception e) {system. out. println ("An error occurred while compressing the image using scaleimagewithparams"); E. printstacktrace () ;}}/*** converts data of the double type to an int type. The rounding principle ** @ Param sourcedouble * @ return */Private Static int parsedoubletoint (double sourcedouble) {int result = 0; Result = (INT) sourcedouble; return result ;} /***** @ Param bufferedimage the image object to be scaled * @ Param width_scale the width to be scaled * @ Param height_scale the height to be scaled * @ return a set, the first element is the width, and the second element is the height */Private Static arraylist <integer> getautowidthandheight (bufferedimage, int width_scale, int height_scale) {arraylist <integer> arraylist = new arraylist <integer> (); int width = bufferedimage. getwidth (); int Height = bufferedimage. getheight (); double scale_w = getdot2decimal (width_scale, width); system. out. println ("getautowidthandheight width =" + width + "scale_w =" + scale_w); double scale_h = getdot2decimal (height_scale, height); If (scale_w <scale_h) {arraylist. add (parsedoubletoint (scale_w * width); arraylist. add (parsedoubletoint (scale_w * Height);} else {arraylist. add (parsedoubletoint (scale_h * width); arraylist. add (parsedoubletoint (scale_h * Height);} return arraylist ;} /***** returns the three digits after the decimal point of two numbers A/B * @ Param A * @ Param B * @ return */public static double getdot2decimal (int, int B) {bigdecimal bigdecimal_1 = new bigdecimal (a); bigdecimal bigdecimal_2 = new bigdecimal (B); bigdecimal bigdecimal_result = values (bigdecimal_2, new mathcontext (4 )); double double1 = new double (bigdecimal_result.tostring (); system. out. println ("the double after division is:" + double1); Return double1 ;}}

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.