Java picture thumbnail clipping watermark scaling rotary compression to format-thumbnailator image processing

Source: Internet
Author: User

Objective

Java development often encountered in the processing of images, JDK also provides the corresponding tool classes, but it is cumbersome to deal with, Thumbnailator is a good image processing of open-source Java class Library, processing effect is far better than the Java API, The process is simplified from the classes in which the API provides existing image files and image objects, and two or three lines of code can generate processed pictures from existing images and allow fine-tuning of how the images are generated while maintaining the minimum amount of code that needs to be written. It also supports batch processing of all pictures of a directory, and the following is a way to share with you the various processing methods of Thumbnailator in Java (related jar packages can be downloaded at the bottom).

Test picture

1. Test picture 1024px-768px.jpg:

2. Image as a watermark:

Create a picture file based on a path
Package Thumbnaillatortest;import Java.awt.image.bufferedimage;import Java.io.file;import Java.io.FileOutputStream ; Import Java.io.ioexception;import Java.io.outputstream;import Javax.imageio.imageio;import Net.coobird.thumbnailator.thumbnails;import Net.coobird.thumbnailator.thumbnails.builder;import Net.coobird.thumbnailator.geometry.positions;import Org.junit.test;public class Thumbtest {@Testpublic void Testhandlepicture () throws ioexception{//Create a picture file (here is a picture of 1024x768px) and the processed picture file ("Frompic=new Test picture 1024px-768px.jpg "); File Topic=new file ("picture/result picture. jpg"); File Waterpic=new file ("picture/watermark picture. jpg");//image as a watermark

Note: The above code program is not finished and is appended by the paste code below.

Scales the image to a specified size (will follow the original aspect scale)
The picture is shrunk and placed by the specified size (will follow the original aspect scale)///Here the picture is pressed into 400x500 thumbnail thumbnails.of (frompic). Size (400,500). ToFile (ToPic);//Changed to 400*300, Follow the original scale or drop to a height of 400*

The effect is as follows:

Zoom out and zoom in at a specified scale
Zoom in and Out Thumbnails.of (frompic). Scale (0.2f). ToFile (ToPic);//scaled down Thumbnails.of (frompic). scale (2f);//Proportional amplification

The narrowing effect is as follows:

Zoom in because it is too large to show the effect again.

Scale by a specified size (does not follow the original scale)
Scales Thumbnails.of (Frompic) by the specified size, not proportionally. Size (+). Keepaspectratio (False). ToFile (ToPic);//or Thumbnails.of ( frompic). Forcesize (100,100). ToFile (ToPic);

The effect is as follows:

Rotate picture
Rotate the picture, rotate (angle), positive is clockwise, negative is counterclockwise thumbnails.of (frompic). Size (200,200). Rotate (ToFile);

The effect is as follows:

Image size unchanged, compressed picture file size
Picture size unchanged, compressed picture file size outputquality Implementation, parameter 1 is the highest quality thumbnails.of (frompic). Scale (1f). Outputquality (0.25f). ToFile (ToPic);

The effect is that the size of the picture from the original 2M more, changed to more than 10 K, the picture is still more clear.

Add a watermark to a picture
Add a watermark to the picture, watermark (position, watermark, transparency) Positions.center is added to the Middle Thumbnails.of (frompic). Size (400,400). Watermark ( Positions.center,imageio.read (Waterpic), 0.5f). Outputquality (0.8f). ToFile (ToPic);

The effect is as follows:

Picture clipping

1.

Use Sourceregion () to achieve the picture clipping//Picture Center 300*300 Area, Positions.center represents the center, there are many other places to choose Thumbnails.of (Frompic). Sourceregion ( positions.center,300,300). Size (300,300). ToFile (ToPic);

The effect is as follows:

2.

The area thumbnails.of (frompic). Sourceregion (positions.top_center,300,300). Size (300,300). ToFile (ToPic) in the 300*300 area of the picture. );

The effect is as follows:

3.

Thumbnails.of (frompic). Sourceregion (0,0,200,200). Size (300,300). ToFile (ToPic);

The effect is as follows:

Convert picture format
Convert the picture format with OutputFormat (image format), keep the original size unchanged thumbnails.of (frompic). Scale (1f). OutputFormat ("PNG"). ToFile ("picture/ PNG format picture. png ");

The effect is that the format is changed from JPG to PNG and the image size unchanged.

Output as file stream OutputStream
Output as file stream Outputstreamoutputstream os=new fileoutputstream (toPic); Thumbnails.of (frompic). Size (120,120). Tooutputstream (OS);

Output into BufferedImage
Output Bufferedimage,asbufferedimage () returns Bufferedimagebufferedimage Bi=thumbnails.of (frompic). Size (120,120). Asbufferedimage (); Imageio.write (bi, "JPG", toPic);


Compress to the specified picture size, keep the picture intact, and cut out the extra part
Compress to the specified picture size (for example: Horizontal 400 high 300), keep the picture is not deformed, the extra part is cropped off (this is the code of the Netizen that is cited) bufferedimage image = Imageio.read (frompic); builder<bufferedimage> Builder = Null;int ImageWidth = Image.getwidth (); int imageheitht = Image.getheight (); if ( FLOAT) 300/400! = (float) imagewidth/imageheitht) {if (ImageWidth > imageheitht) {image = Thumbnails.of (frompic). Heig HT (+). Asbufferedimage ();} else {image = Thumbnails.of (frompic). Width (+). Asbufferedimage ();} Builder = thumbnails.of (image). Sourceregion (Positions.center, +). Size (400, 300); else {builder = thumbnails.of (image). Size (400, 300); Builder.outputformat ("JPG"). ToFile (ToPic);

Test over!
} }


Related Jar Downloads

Thumbnailator free Download

reprint Please indicate-java my life (Chen Leixing) Original source: http://blog.csdn.net/chenleixing/article/details/44685817

Finally, seriously read the netizens, the great gods, if there is a feeling I this program ape has a place to say wrong or wrong or you have a good proposal or suggestions or ideas, but also look You kindness alms n seconds to leave your valuable text (message), so that you, I, and the vast number of programs apes grow and Progress faster ....

Java picture thumbnail clipping watermark scaling rotary compression to format-thumbnailator image processing

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.