Thumbnailator compressing pictures

Source: Internet
Author: User

Thumbnailator is a rare processing of pictures of the third-party toolkit, it is simple to make people can not believe that Java itself has to deal with image compression method, but the code is too long to be painful, at the end of the article will give the implementation of Java itself, to do the contrast, Has manifested the strong thumbnailator. The previous period of time to do a relatively simple business, need to pass the user's identity information, related contract information and other related pictures (do a business to pass so many pictures, I am the user affirmation say goodbye, can not bear spit groove), in fact, the picture upload do not need to display a large map, only need thumbnails only, The original image is required on a separate audit page. Said this background, that the cancellation picture too large transmission too slow, seriously affect the experience (so to speak looks tall, in fact, we deployed four nodes, and the user qualification requirements are strict, the user volume will not be very large ...) ), take the time to solve the problem of sheet compression, we found this thumbnailator tool kit, today by the way the main usage of this write down, later encountered directly to take, the original image with the goddess-Scarlett Johansson bar, hahaha.

Okay, last picture.

The rest of the compressed or processed images are placed in the demo directory, at the end of the text will give an address

Here convenient is directly converted to files, in fact, can be transferred to the stream for transmission (I was nonsense ...)

I. Thumbnailator processing of pictures

1.Thumbnailator compresses the image to a specified size.

1 String result = "src/main/resources/images/";  thumbnails.of (Result + "sijili.jpg"). Size ($). ToFile (Result + "image_200x300.jpg");

2.Thumbnailator compresses the image at a specified scale.

String result = "src/main/resources/images/";
Thumbnails.of (Result + "sijili.jpg"). Scale (0.5f). ToFile (Result + "image_0.5f.jpg");

3.Thumbnailator rotate the picture.

String result = "src/main/resources/images/"+ "sijili.jpg"). Scale (1.0f). Rotate (+). ToFile (Result + "Image_ R180.jpg ");

4.Thumbnailator format the image for conversion.

String result = "src/main/resources/images/"+ "sijili.jpg"). Scale (1.0f). OutputFormat ("PNG"). ToFile (Result + "Image_sijiali.png");

5.Thumbnailator crop the picture.

String result = "src/main/resources/images/"+ "sijili.jpg"). Scale (1.0f). Sourceregion (Positions.center, 400  ToFile (Result + "Image_center.png"+ "Sijili.jpg"). Sourceregion (+, +, +). Scale (1.0f  + "image_100_4.jpg");

6.Thumbnailator Add a watermark to the image.

String result = "src/main/resources/images/"//  watermark parameter 1. Position 2. Watermark Picture 3. Transparency Thumbnails.of ( Result + "sijili.jpg"). Scale (1.0f). Watermark (Positions.center, Imageio.read (new File (result + "warter.jpg") ), 0.25f). ToFile (Result + "image_warter.jpg");

7.Thumbnailator Add a text watermark to the image.

String result = "src/main/resources/images/"new bufferedimage (+=  Bi.creategraphics (); G.setcolor (Color.light_gray); G.drawrect (0, 0, ten); Char [] data = "Liudtest"0, Data.length, 5, +); // watermark parameter 1. Position 2. Watermark 3. Transparency Thumbnails.of (Result + "sijili.jpg"). Scale (1.0f). Watermark (Positions.center, Bi, 1.0f). ToFile (Result + "Image_warter _liud.jpg ");

Thumbnailator on the picture to add a text watermark, not recommended, you can first deal with the text into a picture after the watermark, see the code above the lengthy also does not conform to Thumbnailator style is not.

Two. Java native processing of images

String result = "src/main/resources/images/"new FileOutputStream (result + "sijili_out.jpg"  = Imageio.read (new File (result + "sijili.jpg"new bufferedimage, BUFFEREDIMAGE.TYPE_INT_RGB);        Bufferedimage.getgraphics (). DrawImage (image.getscaledinstance (null"jpg", OS); O S.close ();

About the image processing is summed up here, the address of the code is placed on my GitHub, the address is https://github.com/liujava/myDailyNote, welcome to download, and put forward comments.

Thumbnailator compressing pictures

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.