Java generates JPG thumbnail

xiaoxiao2021-03-06  41

In any comprehensive website, we often need to upload some picture information. However, with the popularity of high resolution DC, the uploaded picture capacity will be large, such as 3 million pixel DCs out of 600K. For convenience of management, everyone may not be willing to modify it with ACDSEE each time, and upload directly to the server. But this approach is not so easy in the client, and users who dial-up Internet are simply a dream, although you can set wide and high in the image area! The problem of solving the problem! We can handle a big picture in the class and zoom it. The premise is JDK1.4, so that it can be processed. Do: import java.io.file; import java.io.fileoutputStream; import java.awt.graphics; import java.awt.image; import java.awt.image.bufferedimage; import com.sun.image.codec .jpeg.jpegcodec; import com.sun.image.codec.jpeg.jpegimageEncoder; public class jpgtest {public void jpgtset () throws exception {file _file = new file ("/ order005-0001.jpg"); // Read File image src = javax.Imageio.Imageio.read (_file); // Construct Image object int wideth = src.getWidth (null); // Get source map wide int Height = src.getHeight (null); // Get source BUFFEREDIMAGE TAG = New BufferedImage (Wideth / 2, Height / 2, BufferedImage.Type_INT_RGB); tag.getgraphics (). DrawImage (src, 0, 0, wideth / 2, height / 2, null); // Draw reduction After the picture fileOutputStream out = new fileoutputstream ("newfile.jpg"); // Output to the file stream JPEGIMAGEENCODER ENCODER = JPEGCODEC.CREATEJPEGENCODER (OUT); Encoder.Encode (tag); // Near JPEG encoding //system.out.print (Width " *" height); out.close ();}} The process is very simple, and the file ORDER005-0001 is read from the local disk. JPG (2032 * 1524), becomes an Image object SRC, then constructs the target file TAG, set the length of the TAG as half of the source map, encoding the TAG, output to the file stream OUT, and finally turn off the file stream.

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

New Post(0)