Handle picture size, size, and watermark with third-party tools (thumbnailator non-Java own API)

Source: Internet
Author: User

thumbnailator-0.4.8 is currently the latest jar

Code

 PackageYingyue;ImportJava.io.File;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.OutputStream;ImportJavax.imageio.ImageIO;Importnet.coobird.thumbnailator.Thumbnails;Importnet.coobird.thumbnailator.geometry.Positions; Public classhandlerimg {Private StaticString frompic = "From/1600x1200-asia emea.jpg"; Private StaticString toPic = "to/";  Public Static voidMain (string[] args) {Try {            /*Scales the image to a specified size (will follow the original aspect scale)*/Thumbnails.of (frompic). Size (100, 200). ToFile (ToPic+ "/new1.jpg");//to 400*300, follow the original scale or drop to a height of 400*                        /*Zoom out and zoom in at a specified scale*/Thumbnails.of (frompic). Scale (0.2f). ToFile (ToPic+ "/new2_small.jpg");//proportionally ReducedThumbnails.of (frompic).                Scale (2f). ToFile (ToPic+ "/new2_large.jpg");//zoom in on the scale                        /*image size unchanged, compressed picture file size*/Thumbnails.of (frompic).                Scale (1f). Outputquality (0.25f). ToFile (ToPic+ "/new3.jpg");//image size unchanged, compressed image file size outputquality implementation, parameter 1 for the highest quality                        /*picture rotation rotate (angle), positive: Clockwise negative: Counterclockwise*/Thumbnails.of (frompic). Size (1280,1024). Rotate (90). ToFile (ToPic+ "/new4.jpg"); /*Watermark Watermark (location, watermark, transparency)*/Thumbnails.of (frompic). Size (1280,1024). Watermark (Positions.bottom_right,imageio.read (NewFile ("From/ss.png")), 0.8f). Outputquality (0.8f). ToFile (ToPic+ "/new5-1.jpg"); Thumbnails.of (frompic). Size (1280,1024). Watermark (Positions.center,imageio.read (NewFile ("From/ss.png")), 0.5f). Outputquality (0.8f). ToFile (ToPic+ "/new5-2.jpg"); Thumbnails.of (frompic). Size (1280,1024). Watermark (Positions.top_left,imageio.read (NewFile ("From/ss.png")), 0.9f). Outputquality (0.8f). ToFile (ToPic+ "/new5-3.jpg"); /*cropping*/Thumbnails.of (frompic). Sourceregion (Positions.center,400,400). Size (200,200). Keepaspectratio (false). ToFile (ToPic+ "/new6-1.jpg"); //picture lower right 400*400 areaThumbnails.of (frompic). Sourceregion (Positions.bottom_right,400,400). Size (200,200). Keepaspectratio (false). ToFile (ToPic+ "/new6-2.jpg"); //Specify coordinatesThumbnails.of (frompic). Sourceregion (600,500,400,400). Size (200,200). Keepaspectratio (false). ToFile (ToPic+ "/new6-3.jpg"); /*Conversion Image Format*/Thumbnails.of (frompic). Size (1280,1024). OutputFormat ("PNG"). ToFile (ToPic+ "/new7-1"); Thumbnails.of (frompic). Size (1280,1024). OutputFormat ("GIF"). ToFile (ToPic+ "/new7-2"); } Catch(IOException e) {e.printstacktrace (); }    }}

Package Yingyue;

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;

public class Testimg {

private static String Frompic = "From/1600x1200-asia emea.jpg";
private static String ToPic = "to/";
public static void Main (string[] args) {
try {
/* Scale the image to the specified size (will follow the original aspect scale) */
Thumbnails.of (Frompic).
Size (100, 200).
ToFile (ToPic + "/new1.jpg");//Change to 400*300, follow the original scale or drop to a height of 400*

/* Zoom out and zoom in at the specified scale */
Thumbnails.of (Frompic).
Scale (0.2f).
ToFile (ToPic + "/new2_small.jpg");//proportionally reduced
Thumbnails.of (Frompic).
Scale (2f).
ToFile (ToPic + "/new2_large.jpg");//Proportional amplification

/* Picture size unchanged, compressed picture file size */
Thumbnails.of (Frompic).
Scale (1f).
Outputquality (0.25f).
ToFile (ToPic + "/new3.jpg");//image size unchanged, compressed image file size outputquality implementation, parameter 1 for the highest quality

/* Picture rotation rotate (angle), positive: Clockwise negative: counterclockwise */
Thumbnails.of (Frompic)
. Size (1280,1024)
. Rotate (90)
. ToFile (ToPic + "/new4.jpg");

/* Watermark Watermark (location, watermark, transparency) */
Thumbnails.of (Frompic)
. Size (1280,1024)
. Watermark (Positions.bottom_right,imageio.read (New File ("From/ss.png")), 0.8f)
. Outputquality (0.8f)
. ToFile (ToPic + "/new5-1.jpg");

Thumbnails.of (Frompic)
. Size (1280,1024)
. Watermark (Positions.center,imageio.read (New File ("From/ss.png")), 0.5f)
. Outputquality (0.8f)
. ToFile (ToPic + "/new5-2.jpg");

Thumbnails.of (Frompic)
. Size (1280,1024)
. Watermark (Positions.top_left,imageio.read (New File ("From/ss.png")), 0.9f)
. Outputquality (0.8f)
. ToFile (ToPic + "/new5-3.jpg");

/* Crop */
Thumbnails.of (Frompic)
. Sourceregion (positions.center,400,400)
. Size (200,200)
. Keepaspectratio (False)
. ToFile (ToPic + "/new6-1.jpg");

Picture lower right 400*400 area
Thumbnails.of (Frompic)
. Sourceregion (positions.bottom_right,400,400)
. Size (200,200)
. Keepaspectratio (False)
. ToFile (ToPic + "/new6-2.jpg");

Specify coordinates
Thumbnails.of (Frompic)
. Sourceregion (600,500,400,400)
. Size (200,200)
. Keepaspectratio (False)
. ToFile (ToPic + "/new6-3.jpg");

/* Convert image Format */
Thumbnails.of (Frompic)
. Size (1280,1024)
. OutputFormat ("PNG")
. ToFile (ToPic + "/new7-1");

Thumbnails.of (Frompic)
. Size (1280,1024)
. OutputFormat ("GIF")
. ToFile (ToPic + "/new7-2");

} catch (IOException e) {
E.printstacktrace ();
}
}
}

Handle picture size, size, and watermark with third-party tools (thumbnailator non-Java own API)

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.