Java generates JPG thumbnail

xiaoxiao2021-03-06  134

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 it as follows:

Import java.io.file;

Import java.io.fileoutputstream;

Import java.awt.graphics;

Import Java.awt.Image;

Import java.awt.image.bufferedImage;

Import com.sun.jpegcodec;

Import com.sun.jpegimageencod;

Public class jpgtest {

Public void jpgtset () throws exception {

File _file = new file ("/ order005-0001.jpg"); // read the 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 the source map

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 diagram

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 " *" ;

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 problems to explain:

First, there are only three formats of JPG (JPEG), GIF, PNG.

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. JavaMail is used in the Builder mode, gentleman into every part of a message, then merge into a complete mail object, so that each component must be in memory, if you send a hundredgaphorn attachment, then When Part is affirmed, I have rewritten the BodyPart constructor, let him associate him and a temporary file, then save the part instead of constructing in memory, such aid of an attachment (the hard disk can be released ) Can be sent. Finally, if you have a higher requirements for image processing, you may wish to pay attention to the open source project. For example, Jmagick, you can use Jmagick to implement image replication, information acquisition, bevel, effects, combination, change, plus border, rotation, slice, change format, dequler, and so on.

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

New Post(0)