Thumbnail implementation, automatic scaling of uploaded images in JAVA

Source: Internet
Author: User
Import Java. AWT. image; import Java. AWT. image. bufferedimage; import Java. io. file; import Java. io. fileoutputstream; import Java. io. ioexception; import javax. imageIO. imageIO; import com.sun.image.codec.jpeg. export codec; import com.sun.image.codec.jpeg. required imageencoder; /*************************************** **************************************** * thumbnails (common) this Java class can convert JPG, BMP, PNG, and GIF image files to an equal or non-equal ratio. Specific usage method * compresspic (large image path, generate small image path, large image file name, generate small image file name, generate small image width, generate small image height, and perform proportional Scaling (true by default )) */public class compresspicdemo {private file = NULL; // file object private string inputdir; // input graph path private string outputdir; // output graph path private string inputfilename; // input image file name: Private string outputfilename; // output image file name: Private int outputwidth = 100; // default output image width: Private int outputheight = 100; // default output Image Height private Boolean proport Ion = true; // indicates whether to scale proportionally (proportional scaling by default) Public compresspicdemo () {// initialization variable inputdir = ""; outputdir = ""; inputfilename = ""; outputfilename = ""; outputwidth = 100; outputheight = 100;} public void setinputdir (string inputdir) {This. inputdir = inputdir;} public void setoutputdir (string outputdir) {This. outputdir = outputdir;} public void setinputfilename (string inputfilename) {This. inputfilename = inputfilename;} Public void setoutputfilename (string outputfilename) {This. outputfilename = outputfilename;} public void setoutputwidth (INT outputwidth) {This. outputwidth = outputwidth;} public void setoutputheight (INT outputheight) {This. outputheight = outputheight;} public void setwidthandheight (INT width, int height) {This. outputwidth = width; this. outputheight = height;}/** get image size input parameter string path: Image path */publi C Long getpicsize (string path) {file = new file (PATH); Return file. length () ;}// Image Processing Public String compresspic () {try {// obtain the source file = new file (inputdir + inputfilename); If (! File. exists () {return "";} image IMG = ImageIO. read (File); // read the image file locally. If you upload the image, formfile = obtains the formfile submitted by the form, and then // ImageIO. put formfile in the read method. getinputstream () // determines whether the image format is correct if (IMG. getwidth (null) =-1) {system. out. println ("can't read, retry! "+" <Br> "); Return" no ";} else {int newwidth; int newheight; // determines whether it is proportional scaling if (this. proportion = true) {// This is the width and height of the image output by proportional scaling. Double rate1 = (double) IMG. getwidth (null)/(double) outputwidth + 0.1; double rate2 = (double) IMG. getheight (null)/(double) outputheight + 0.1; // double rate = rate1> rate2? Rate1: rate2; newwidth = (INT) (double) IMG. getwidth (null)/rate); newheight = (INT) (double) IMG. getheight (null)/rate);} else {newwidth = outputwidth; // The output image width newheight = outputheight; // output Image Height} bufferedimage tag = new bufferedimage (INT) newwidth, (INT) newheight, bufferedimage. type_int_rgb);/** image. scale_smooth scaling algorithm generates the smoothness of the thumbnail. The priority is higher than the speed. The generated image quality is better but the speed is slow */Tag. getgraphics (). drawimage (IMG. getscaledinstance (newwidth, newheight, image. scale_smooth), 0, 0, null); fileoutputstream out = new fileoutputstream (outputdir + outputfilename); // optional imageencoder can be used to convert other image types; optional imageencoder encoder = required codec. createjpegencoder (out); encoder. encode (TAG); out. close () ;}} catch (ioexception ex) {ex. printstacktrace ();} return "OK";} Public String compresspic (string inputdir, string outputdir, string inputfilename, string outputfilename) {// enter the image path this. inputdir = inputdir; // output graph path this. outputdir = outputdir; // enter the image file name this. inputfilename = inputfilename; // output image file name this. outputfilename = outputfilename; return compresspic ();} Public String compresspic (string inputdir, string outputdir, string inputfilename, string outputfilename, int width, int height, Boolean GP) {// enter the image path this. inputdir = inputdir; // output graph path this. outputdir = outputdir; // enter the image file name this. inputfilename = inputfilename; // output image file name this. outputfilename = outputfilename; // set the length and width of the image to setwidthandheight (width, height); // whether it is an proportional scaling flag this. proportion = gp; return compresspic ();} public static void main (string [] ARGs) {compresspicdemo mypic = new compresspicdemo (); system. out. println ("input image size:" + mypic. getpicsize ("E: \ chris.jpg")/1024 + "kb"); int COUNT = 0; // records the compression time of all images for (INT I = 0; I <3; I ++) {int start = (INT) system. currenttimemillis (); // start time mypic. compresspic ("E: \", "E: \ test \", "chris.jpg", "Chris" + I + ". jpg ", 120,180, false); int end = (INT) system. currenttimemillis (); // End Time int Re = end-start; // However, the processing time of Image Generation count + = Re; system. out. println ("no." + (I + 1) + "used for image compression:" + RE + "millisecond"); system. out. println ("output image size:" + mypic. getpicsize ("E: \ test \ R1" + I + ". jpg ")/1024 +" kb ");} system. out. println ("Total used:" + Count + "millisecond ");}}

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.