Code example of generating JPG and compressed pictures as JPG files using java

Jpg format image generated by Java:

The following is the generated picture

Java compressed images are JPG thumbnails. When we want to upload data to share with you on the website, we will encounter such a problem. With the popularity of high-resolution DC, the uploaded image capacity will be large. For example, the files from 3 million pixel DC are basically no less than 600k In order to facilitate management, you may not want to modify it with ACDSee every time and upload it directly to the server. However, this approach is not so easy in the client's view. 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 coming! We can handle a large picture in the class and narrow it down. The premise is jdk1 4. Only in this way can it be processed. Do as follows:

The process is very simple. Read the file order005-0001 from the local disk Jpg (2032 * 1524) becomes the image object SRC, then constructs the target file tag, sets the length and width of the tag to half of the source image, encodes the tag, outputs it to the file stream out, and finally closes the file stream.

There are still some problems to be explained: first, at present, it can only support three formats: JPG (JPEG), GIF and PNG. Second, the capacity of the source graph is limited. It's best not to exceed 1m, otherwise the error of insufficient memory will be thrown. However, I have tried the 1.8m source graph and it can succeed, but it's also easy to throw insufficient memory. To quote an elder: image operation itself is intensive operation, which requires a lot of memory to store pixel values. I tried with VC. There is also a problem with 4m images, and the larger the compression ratio, the larger the memory required to restore the images to bitmap in memory. The solution is to rewrite the coding class, first open a certain memory, then write the coding section by section to the temporary file, and then read it section by section when outputting. Or use NiO's memory image to operate. Because JavaMail adopts the builder mode, each part of an e-mail is formed and then merged into a complete e-mail object, so that each component must be formed into memory. If you send an attachment of more than 100 megabytes, the memory must overflow when constructing the part, so I rewritten the structure of the bodypart to associate it with a temporary file, Then save the part in a temporary file instead of constructing it in memory, so that attachments of any size (limited to the size of the hard disk) can be sent.

Finally, if you have higher requirements for image processing, you might as well pay attention to the open source project. For example, jmagick can be used to realize the functions of image reproduction, information acquisition, bevel, special effects, combination, size change, border addition, rotation, slicing, format change, color removal, etc.

PS: some old java code will report errors when compiled under JDK, such as this package com sun. image. codec. JPEG does not exist.

Jpegimageencoder class is a private class of Sun company

It usually appears in such code snippets:

Rewrite to:

All use unified imageio to read and write image format files. There is no need to use the outdated implementation class jpegimageencoder class.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>