java generate JPG thumbnails

Source: Internet
Author: User
Thumbnails on any comprehensive website, we often need to upload some picture data. However, with the popularization of high resolution DC, upload the picture capacity will be very large, such as 3 million pixel DC out of the file basically less than 600K. To manage conveniently, you may not want to use ACDSee to modify it every time, and upload it directly to the server. But it's not as easy as it looks to the client, and it's a nightmare for dial-up users, although you can set up wide and high! in the picture area.

The solution to the problem is coming! We can handle a larger picture in a class and shrink it.
The premise is that JDK1.4 needs to be done in order to be processed. Do 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.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 into file
Image src = javax.imageio.ImageIO.read (_file); Constructing an Image Object
int wideth=src.getwidth (NULL); Get Source image width
int height=src.getheight (NULL); Get source Diagram length
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 smaller figure
FileOutputStream out=new FileOutputStream ("newfile.jpg"); Output to file stream
JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (out);
Encoder.encode (tag); Near-JPEG coding
System.out.print (width+ "*" +height);
Out.close ();
}
}

The process is simple, read the file from the local disk order005-0001.jpg (2032*1524), into the Image object SRC, and then construct the target file tag, set the length of the tag as the source of the half, the tag to encode, output to the file stream out, and finally close the file flow.

There are also some questions to note:
First, only JPG (JPEG), GIF, PNG Three formats are currently supported.
Second, for the source map capacity of limited system, it is best not to exceed 1M, otherwise it will throw out the memory error, but I tested the source map of 1.8M, can be successful, but also very easy to throw out memory.

Quote a predecessor: The image operation itself is a dense operation, requiring a large amount of memory to store pixel values. I tried with VC, 4M image also has problems, and the more compression than the large picture in memory to restore to bitmap when the memory is larger. Solution, you can rewrite the encoding class, first open a certain amount of memory, and then a paragraph of the code to write to the temporary file, the output of a paragraph read out. Or use the memory image of NiO to operate. JavaMail because of the builder mode, Sir, each part of a message is merged into a full mail object, so that each widget is generated into memory first, and if you send a hundreds of megabytes of attachments, then the memory overflow is definitely in the construction parts, So I rewrote the structure of BodyPart, let him associate with a temporary file, and then save part instead of in memory with a temporary file, so that any attachment of any size (the hard drive can be placed to the limit) can be sent.

Finally, if you have a higher demand for image processing, you might want to focus on open source projects. For example Jmagick, you can use Jmagick to achieve image reproduction, information acquisition, bevel, special effects, combinations, resize, add borders, rotate, slice, change format, go color and so on.




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.