Java generates JPG thumbnail

zhaozj2021-02-17  151

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); // Construction image object int wideth = src.getWidth (null); // Get source map wide int Height = src.getHeight (NULL); // Get the source map long bufferedimage tag = new bufferedimage (wideth / 2, height / 2, bufferedimage.Type_INT_RGB); tag.getgraphics (). DrawImage (src, 0, 0, wideth / 2, height / 2 NULL); // Draw a reduced picture fileOutputStream out = new fileoutputstream ("newfile.jpg"); // Output to the file stream JPEGIMAGEENCODER ENCODER = JPEGCODEC.CREATEJPEGENCODER (OUT); Encoder.Encode (tag); // JPEG code //system.out.print(Width "8" ; out.close ();}} The process is very simple, from the local disk read file order005-0001.jpg (2032 * 1524), becomes an image object SRC, then construct the target file TAG, set the length of the TAG to half the source map, encoded TAG, output To the file stream OUT, close the file stream. There are still some questions that need to be explained: First, only three types of JPG (JPEG), GIF, PNG can be supported. Second, for the capacity of the source map, it is best not to exceed 1M, otherwise it will throw insufficient errors, but I tried 1.8M source map, which can be successful, but it is easy to throw instructions. Quote a senior: The image operation itself is a intensive operation, which requires a lot of memory amplifies. I tried it with VC, and the image of 4M also has problems, and the more compressed than large pictures are restored to Bitmap in memory. The larger. The method of solving can rewrote the encoding class, open a certain amount of memory first, and then write it in a temporary file, and then read it again. Or use the NIO's memory image to operate.

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

New Post(0)