Java Image Upload compression processing

Source: Internet
Author: User
Tags bmp image

Link: http://www.javaeye.com/topic/266585

1. Image Format

Java apivery well. The com.sun.image.codec.jpeg.mongocodecand com.sun.image.codec.jpeg. mongoimageencoder classes automatically solve the type conversion problem,Convert BMP to JPG, PNG to JPG, and GIF to JPG.,However, the GIF-to-GIF function is not yet available.

2. Image Quality Problems

Bufferedimage tag = new bufferedimage (INT) newwidth, (INT) newheight, bufferedimage. type_int_rgb );

// The image. scale_smooth scaling algorithm generates a thumbnail. The smoothness is higher than the smoothness.

Tag. getgraphics (). drawimage (IMG. getscaledinstance (newwidth,
Newheight, image. scale_smooth), 0, 0, null );

3. compression speed

It takes 2-3 seconds to compress a 36 mb bmp image (8192*6144) into a JPG 5 kb image (160*120. It takes only 17 seconds to batch process 100 (1027*768) BMP images and convert them to (120*80) JPG images.

4. Select the compression mode based on your preferences.

Proportional or according to the specified size

// 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 ))

The following is the source code. If you think it can be improved, I hope you can provide some comments !!

/*** Implement the thumbnail to convert the image (JPG, BMP, PNG, GIF, etc.) into the desired size */package COM. joewalker. test; 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; /*************************************** **************************** * ************ Thumbnail class (common) This Java class can be used to convert JPG, BMP, PNG, GIF image files, converts the values of equal to or non-equal ratios. 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 boolea N proportion = 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. input Filename = 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 ;}/* * Obtain image size * input parameter string path: Image path */public 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); // determines whether the image format is correct if (IMG. getwidth (null) =-1) {system. out. println ("can't read, retry! "+" "); 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. the scale_smooth scaling algorithm generates the smoothness of the thumbnail. * The priority is higher than the speed. The 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 () ;}// main test // compresspic (large image path, generate small image path, large image file name, generate small image file name, generate small image width, generate the height of the small image. Do you want to perform proportional Scaling (true by default)? Public static void main (string [] Arg) {compresspicdemo mypic = new compresspicdemo (); system. out. println ("input image size:" + mypic. getpicsize ("E: \ 1.jpg")/1024 +" kb "); int COUNT = 0; // records the compression time of all images for (INT I = 0; I <100; I ++) {int start = (INT) system. currenttimemillis (); // start time mypic. compresspic ("E: \", "E: \ test \", "1.jpg"," R1 "+ I + ". jpg ", 120,120, true); 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 ");}}
Related Article

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.