Zooming the uploaded ZT Image

Source: Internet
Author: User

After the image is uploaded to the server, the image will be reduced to an icon as needed. We can use the powerful java graphics processing function to scale the uploaded image.
The following program uses the latest ImageIO in jdk1.4 to read and write images. It uses AffineTransform to scale the images.
Import java. io. File;
Import java. awt. image. BufferedImage;
Import java. awt. Image;
Import java. awt. image. AffineTransformOp;
Import javax. imageio. ImageIO;
Import java. awt. geom. AffineTransform;
Public class UploadImg {
/**
* @ Param fromdir original directory of the image
* @ Param todir: directory for storing processed images
* @ Param imgfile Original Image
* @ Param sysimgfile: prefix of the image file name after processing
*
*/
............................
Public boolean CreateThumbnail () throws Exception
{
// Ext is the image format. gif. JPG or. png.
String ext = "";
Double Ratio = 0.0;
File F = new File (fromdir, imgfile );
If (! F. isFile ())
Throw new Exception (F + "is not image file error in CreateThumbnail! ");
// First, determine whether the uploaded image is gif or JPG ImageIO. You can only convert the image to png.
If (isJpg (imgfile )){
Ext = "jpg ";
} Else {
Ext = "png ";
}
File ThF = new File (todir, sysimgfile + "." + ext );
BufferedImage Bi = ImageIO. read (F );
// Assume the maximum image width and height is 120.
Image Itemp = Bi. getScaledInstance (120,120, Bi. SCALE_SMOOTH );
If (Bi. getHeight ()> 120) | (Bi. getWidth ()> 120 )){
If (Bi. getHeight ()> Bi. getWidth ())
Ratio = 120.0/Bi. getHeight ();
Else
Ratio = 120.0/Bi. getWidth ();
}
AffineTransformOp op = new AffineTransformOp (AffineTransform. getScaleInstance (Ratio, Ratio), null );
Itemp = op. filter (Bi, null );
Try {
ImageIO. write (BufferedImage) Itemp, ext, ThF );
} Catch (Exception ex ){
Throw new Exception ("ImageIo. write error in CreatThum.:" + ex. getMessage ());
}
Return (true );
}
}
This program uses Java AWT, although not displayed, but in linux, the program requires X11 windows support. XFree86 needs to be installed.
Or XFree86-Xvfb, add export DISPLAY = hostdomain: 0.0
For this problem, I also had a headache for a long time. I complained a lot about the query on the Internet. Since Java is a write, it runs everywhere, and it seems to be a bit out of name.
Of course, pja vnc or ACME Laboratories can also be used. However, since the program uses the latest class jdk1.4, it is estimated that other alternative products are not available.

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.