Jsp code for generating thumbnails

Source: Internet
Author: User

Jsp tutorial to generate a thumbnail code program

Java. io. File file = new java. io. File (saveurl); // read the uploaded File
String newurl = request. getRealPath ("/") + url + "min _" + filename + "." + ext; // New thumbnail storage address
Image src = javax. imageio. ImageIO. read (file); // construct an Image object
Float tagsize = 200;
Int old_w = src. getWidth (null); // Obtain the source image width.
Int old_h = src. getHeight (null );
Int new_w = 0;
Int new_h = 0; // Obtain the source image length.
Int temps tutorial ize;
Float tempdouble;
If (old_w> old_h ){
Tempdouble = old_w/tagsize;
} Else {
Tempdouble = old_h/tagsize;
    }
New_w = Math. round (old_w/tempdouble );
New_h = Math. round (old_h/tempdouble); // calculate the length and width of the new graph.
BufferedImage tag = new BufferedImage (new_w, new_h, BufferedImage. TYPE_INT_RGB );
Tag. getGraphics (). drawImage (src, new_w, new_h, null); // draw the reduced graph
FileOutputStream newimage = new FileOutputStream (newurl); // output to the File Stream
Required imageencoder encoder = required codec. createJPEGEncoder (newimage );
Encoder. encode (tag); // near JPEG encoding
   
Newimage. close ();

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.