In some websites, we often need to upload images. However, with the popularization of high-resolution DC, the size of uploaded images will be very large. For example, the size of files generated by 3 million pixel DC cannot exceed 600 KB.
For ease of management, you may not want to use ACDSee to modify it every time and upload it directly to the server. However, this approach is not so easy on the client. It is a nightmare for dial-up Internet users, although you can set wide and high in the image area!
The solution to the problem is now! We can process a large image in the class and narrow it down.
The premise is that jdk1.4 is required for processing. Follow these steps:
|
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. Unzip codec;
Import com.sun.image.codec.jpeg. encode imageencoder;
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 an image object
Int Wideth = SRC. getwidth (null); // obtain the source image width.
Int Height = SRC. getheight (null); // obtain the source image length.
Bufferedimage tag = new bufferedimage (Wideth/2, height/2, bufferedimage. type_int_rgb );
Tag. getgraphics (). drawimage (SRC, Wideth/2, height/2, null); // draw the reduced Graph
Fileoutputstream out = new fileoutputstream ("newfile.jpg"); // output to the file stream
Required imageencoder encoder = required codec. createjpegencoder (out );
Encoder. encode (TAG); // near JPEG encoding
// System. Out. Print (width + "*" + height );
Out. Close ();
}
} |
The process is very simple, from reading the file order005-0001.jpg (2032*1524) to the image object SRC, then construct the target file tag, set the length and width of the tag to half of the source image, encode the tag, and output it to the file stream out, close the file stream.
There are also some issues to note:
First, only JPG (JPEG), GIF, and PNG formats are supported.
Second, there is a limit on the source image capacity. It is best not to exceed 1 MB. Otherwise, the error of insufficient memory will be thrown. However, I have tested the source image of 1.8m and it can be successful, however, it is easy to throw insufficient memory.
Referencing a predecessor: Image operations are intensive operations and require a large amount of memory to store pixel values. I tried it with VC. There are also problems with 4 m images, and the larger the compression ratio of images, the larger the memory size required for restoring them to bitmap in the memory. To solve this problem, you can rewrite the encoding class, first open a certain amount of memory, then write a piece of encoding into a temporary file, and then read it out after the output. Or use the memory image of NiO.
Because javamail adopts the builder mode, it is merged into every part of an email into a complete email object. In this way, each component must be converted into memory, if you send an attachment of up to MB, the memory will definitely overflow when the part is constructed, so I will rewrite the bodypart structure to associate it with a temporary file, then save the part with a temporary file instead of creating it in the memory, so that all attachments of any size (the hard disk can be placed below the limit) can be sent.
Finally, if you have higher requirements for image processing, consider the open-source project. For example, jmagick can be used to achieve image copying, information acquisition, diagonal angle, special effects, combination, size change, border adding, rotation, slicing, format change, color removal, and other functions.