Java using thumbnailator to manipulate pictures

Source: Internet
Author: User

Thumbnailator is a Java class library for creating image thumbnails, cropping, rotating, adding watermarks, and creating thumbnail images from simple code, or directly to a picture of an entire catalog.

Thumbnailator simple operation code for the picture is as follows:

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.geometry.Positions;/** *  * @authorYaohucaizi *  */ Public classthumbnailatortest {/**     *      * @param args     * @throws IOException     */     Public Static void Main(string[] args)throwsIOException {Thumbnailatortest thumbnailatortest =New thumbnailatortest(); Thumbnailatortest.test1(); Thumbnailatortest.test2(); Thumbnailatortest.test3(); Thumbnailatortest.test4(); Thumbnailatortest.Test5(); Thumbnailatortest.Test6(); Thumbnailatortest.Test7(); Thumbnailatortest.Test8(); Thumbnailatortest.Test9(); }/*** Specify size to scale     *      * @throws IOException     */    Private void test1()throwsIOException {/** Size (width,height) If the picture shape factor 200 small, high than 300 small, unchanged* If the picture shape factor 200 small, high than 300, the height shrinks to 300, the picture proportion is unchanged if the picture shape Factor 200 big, the height is smaller than 300, the horizontal shrinks to 200, the picture proportion does not change* If the picture shape factor 200 large, high than 300, the picture is scaled down, horizontal is 200 or higher         */Thumbnails. of("Images/test.jpg").size( $, -).ToFile("C:/image_200x300.jpg"); Thumbnails. of("Images/test.jpg").size(2560,2048).ToFile("C:/image_2560x2048.jpg"); }/*** Scale by scale     *      * @throws IOException     */    Private void test2()throwsIOException {/*** Scale (proportional)         */Thumbnails. of("Images/test.jpg"). Scale(0.25f).ToFile("C:/image_25%.jpg"); Thumbnails. of("Images/test.jpg"). Scale(1.10f).ToFile("C:/image_110%.jpg"); }/*** Scale is not scaled by the specified size     *      * @throws IOException     */    Private void test3()throwsIOException {/*** Keepaspectratio (FALSE) is scaled by default         */Thumbnails. of("Images/test.jpg").size( -, -).Keepaspectratio(false)                .ToFile("C:/image_120x120.jpg"); }/*** Rotation     *      * @throws IOException     */    Private void test4()throwsIOException {/*** Rotate (angle), positive: Clockwise negative: Counterclockwise         */Thumbnails. of("Images/test.jpg").size( the,1024x768).Rotate( -).ToFile("C:/image+90.jpg"); Thumbnails. of("Images/test.jpg").size( the,1024x768).Rotate(- -).ToFile("C:/iamge-90.jpg"); }/*** Watermark     *      * @throws IOException     */    Private void Test5()throwsIOException {/*** Watermark (position, watermark map, transparency)         */Thumbnails. of("Images/test.jpg").size( the,1024x768).Watermark(Positions.Bottom_right, ImageIO.Read(NewFile ("Images/watermark.png")),0.5f).outputquality(0.8f).ToFile("C:/image_watermark_bottom_right.jpg"); Thumbnails. of("Images/test.jpg").size( the,1024x768).Watermark(Positions.CENTER, ImageIO.Read(NewFile ("Images/watermark.png")),0.5f).outputquality(0.8f).ToFile("C:/image_watermark_center.jpg"); }/*** Crop     *      * @throws IOException     */    Private void Test6()throwsIOException {/*** Image Center 400*400 area         */Thumbnails. of("Images/test.jpg").sourceregion(Positions.CENTER, -, -).size( $, $).Keepaspectratio(false).ToFile("C:/image_region_center.jpg");/*** Picture Right bottom 400*400 area         */Thumbnails. of("Images/test.jpg").sourceregion(Positions.Bottom_right, -, -).size( $, $).Keepaspectratio(false).ToFile("C:/image_region_bootom_right.jpg");/*** Specify coordinates         */Thumbnails. of("Images/test.jpg").sourceregion( -, -, -, -).size( $, $).Keepaspectratio(false).ToFile("C:/image_region_coord.jpg"); }/*** Conversion Image format     *      * @throws IOException     */    Private void Test7()throwsIOException {/*** OutputFormat (image format)         */Thumbnails. of("Images/test.jpg").size( the,1024x768).OutputFormat("PNG")                .ToFile("C:/image_1280x1024.png"); Thumbnails. of("Images/test.jpg").size( the,1024x768).OutputFormat("GIF")                .ToFile("C:/image_1280x1024.gif"); }/*** Output to OutputStream     *      * @throws IOException     */    Private void Test8()throwsIOException {/*** Tooutputstream (Stream object)         */OutputStream OS =NewFileOutputStream ("C:/image_1280x1024_outputstream.png"); Thumbnails. of("Images/test.jpg").size( the,1024x768).Tooutputstream(OS); }/*** Output to BufferedImage     *      * @throws IOException     */    Private void Test9()throwsIOException {/*** Asbufferedimage () return bufferedimage         */BufferedImage thumbnail = Thumbnails. of("Images/test.jpg").size( the,1024x768).Asbufferedimage(); ImageIO.Write(Thumbnail,"JPG",NewFile ("C:/image_1280x1024_bufferedimage.jpg")); }}


Full demo Download: Thumbnailator



Java using thumbnailator to manipulate 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.