Upload image zoom processing

xiaoxiao2021-03-06  133

Upload image zoom processing

Board bridge JDon.com

After the picture is uploaded to the server, the picture will shrink into an icon according to the situation, we can use the Java powerful graphics processing function to scale up the uploaded image.

The following program uses the latest imageio in JDK1.4 to read and write the picture. Use the AffineTransform to scale.

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 version of the original directory * @Param Todir processed picture storage directory * @Param imgfile Original image * @Param sysimgfile after processing image file name prefix ** / ....... ................... Public Boolean Createthumbnail () throws exception {// EXT is the picture format GIF JPG or pngstring 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 judge the uploaded picture is GIF or JPG imageio only Can convert GIF to pngif (ISJPG (IMGFILE)) {ext = "jpg";} else {ext = "PNG";} File THF = New File (TODIR, SYSIMGFILE "." Ext); bufferedImage bi = imageio. READ (F); // Assume that the picture is high to 120 120Image 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 (); elseratio = 120.0 / bi.getwidth (); }Affinetransformop op = New affinet ransformOp (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);}}

The program uses Java's AWT, running in Linux, there is two ways:

(1) JDK1.4 Previous version: Need to install XFree86 and XFree86-XVFB, join export display = hostdomain: 0.0

(2) JDK 1.4 later, in the execution of the command java, the parameter is executed - DJAVA.AWT.HEADLESS = TRUE, indicating that this is a headless server without the keyboard without a display, a server that is hosted by the aircraft. About server-side graphics support, there are many open source packages: PJA VNC or ACME Laboratories.

转载请注明原文地址:https://www.9cbs.com/read-127038.html

New Post(0)