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.