Java Generate picture thumbnails

Source: Internet
Author: User

 Packagecom.ares.image.test;ImportJava.awt.Color;ImportJava.awt.Graphics;ImportJava.awt.Image;ImportJava.awt.image.BufferedImage;ImportJava.io.File;Importjava.io.IOException;Importjava.util.Arrays;ImportJavax.imageio.ImageIO;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory;ImportCom.ares.slf4j.test.Slf4jUtil;/*** <p>title:imageutil </p> * <p>description: </p> * <p>email: [Email protected] </p&  Gt * @authorAres * @date October 28, 2014 morning 10:24:26*/ Public classImageutil {Static{slf4jutil.buildslf4jutil (). LOADSLF4J (); }    PrivateLogger log =Loggerfactory.getlogger (GetClass ()); Private StaticString default_prevfix = "Thumb_"; Private StaticBoolean Default_force =false; /*** <p>Title:thumbnailImage</p> * <p>description: Generate thumbnails based on image path </p> *@paramImagePath Original picture path *@paramw thumbnail width *@paramh Thumbnail Height *@paramPrevfix generate a prefix for thumbnails *@paramwhether force forces a thumbnail to be generated according to the width height (if false, the optimal scale thumbnail is generated)*/     Public voidThumbnailimage (File Imgfile,intWintH, String Prevfix,BooleanForce ) {        if(Imgfile.exists ()) {Try {                //ImageIO supported image types: [BMP, BMP, jpg, jpg, wbmp, JPEG, PNG, PNG, JPEG, wbmp, GIF, GIF]String types =arrays.tostring (Imageio.getreaderformatnames ()); String suffix=NULL; //get picture suffix                if(Imgfile.getname (). IndexOf (".") >-1) {suffix= Imgfile.getname (). substring (Imgfile.getname (). LastIndexOf (".") + 1); }//type and picture suffix all lowercase, and then determine if the suffix is legal                if(Suffix = =NULL|| Types.tolowercase (). IndexOf (Suffix.tolowercase ()) < 0) {Log.error ("Sorry, the image suffix is illegal. The standard image suffix is {}. "+types); return ; } log.debug ("Target image ' size, width:{}, height:{}.", w,h); Image img=Imageio.read (Imgfile); if(!Force ) {                    //find the most appropriate thumbnail scale based on the original image and the required thumbnail scale                    intwidth = img.getwidth (NULL); intHeight = img.getheight (NULL); if(width*1.0)/w < (height*1.0)/h) {                        if(Width >W) {h= Integer.parseint (NewJava.text.DecimalFormat ("0"). Format (Height * w/(width*1.0))); Log.debug ("Change image ' height, width:{}, height:{}.", w,h); }                    } Else {                        if(Height >h) {W= Integer.parseint (NewJava.text.DecimalFormat ("0"). Format (Width * h/(height*1.0))); Log.debug ("Change image ' width, width:{}, height:{}.", w,h); }}} bufferedimage bi=NewBufferedImage (W, H, Bufferedimage.type_int_rgb); Graphics g=Bi.getgraphics (); G.drawimage (IMG,0, 0, W, H, Color.light_gray,NULL);                G.dispose (); String P=Imgfile.getpath (); //Save the picture in the original directory with a prefixImageio.write (bi, suffix,NewFile (P.substring (0,p.lastindexof (file.separator)) + File.separator + Prevfix +imgfile.getname ())); } Catch(IOException e) {log.error ("Generate thumbnail image failed.", E); }        }Else{Log.warn ("The image is not exist."); }    }         Public voidThumbnailimage (String ImagePath,intWintH, String Prevfix,BooleanForce ) {File Imgfile=NewFile (ImagePath);    Thumbnailimage (Imgfile, W, H, Prevfix, Force); }         Public voidThumbnailimage (String ImagePath,intWintHBooleanForce )    {thumbnailimage (ImagePath, W, H, Default_prevfix, Default_force); }         Public voidThumbnailimage (String ImagePath,intWinth)    {thumbnailimage (ImagePath, W, H, Default_force); }         Public Static voidMain (string[] args) {NewImageutil (). Thumbnailimage ("Imgs/tulips.jpg", 100, 150); }}

Effect: (The original picture is the computer "Pictures" folder under the Tulip picture, the size of 1024x768, the resulting picture for 200*150 size, maintain a 4:3 ratio)

Java Generate picture thumbnails

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.