Java uses Thumbnailator image processing wrapper class

Source: Internet
Author: User
Tags string format

Thumbnailator is a very good image open source tool that can do a good job of picture processing.

By simplifying the thumbnail process from the API providing thumbnails of existing image files and image objects, two or three lines of code can generate thumbnails from existing images and allow fine-tuning of thumbnail generation while preserving the amount of code that needs to be written to the minimum. It also supports batch generation of thumbnails based on a single directory.

Pom.xml Configuration in Maven:

<dependency><groupId>net.coobird</groupId><artifactId>thumbnailator</artifactId> <version>0.4.8</version></dependency>

The following is the code for the wrapper class:

Package Com.sun4j.app.util;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.position;public Class thumbnailatorutils {/** * Specify size to zoom * If the picture shape factor width Small, height is small, unchanged if the picture shape factor width is small, high than height, high zoom to height, picture proportion is unchanged * if the picture shape factor width large, Height is small, narrow to width, picture ratio is unchanged * If the picture shape factor width large, high height than height, the picture proportionally reduced, horizontal width or height for height * * @param source * input source * @param o Utput * Output Source * @param width * Width * @param height * High */public static void Imgthumb (String s Ource, String output, int width, int height) {try {thumbnails.of (source). Size (width, height). ToFile (output);} catch (Ioex Ception e) {e.printstacktrace ();}}  /** * Specify size to scale * * @param source * Input source * @param output * source * @param width * Width * @param Height * High */public static void Imgthumb (FiLe source, String output, int width, int height) {try {thumbnails.of (source). Size (width, height). ToFile (output);} catch (I Oexception e) {e.printstacktrace ();}}  /** * Scale Scaling * * @param source * Input source * @param output * source * @param width * Width * @param Height * High */public static void Imgscale (string source, string output, double scale) {try {thumbnails.of (SOURC e). scale. ToFile (output);} catch (IOException e) {e.printstacktrace ();}} public static void Imgscale (File source, String output, double scale) {try {thumbnails.of (source). Scale (scale). ToFile ( Output);} catch (IOException e) {e.printstacktrace ();}} /** * Do not scale, specify size to zoom * * @param source * Input Sources * @param output * source * @param width * Width * @param height * High * @param keepaspectratio * is scaled by default, with a value of false not scaled */public static void Imgnosc Ale (string source, string output, int width, int height, Boolean keepaspectratio) {try {thumbnails.of (SOURCE). Size (width, height). Keepaspectratio (Keepaspectratio). ToFile (output); catch (IOException e) {e.printstacktrace ();}} public static void Imgnoscale (File source, String output, int width, int height, Boolean keepaspectratio) {try {Thumbnails . of (source). Size (width, height). Keepaspectratio (Keepaspectratio). ToFile (output); catch (IOException e) {e.printstacktrace ();}}  /** * Rotation, positive: clockwise negative: Counterclockwise * * @param source * Input source * @param output * source * @param width * Width * @param height * high * @param rotate * angle */public static void Imgrotate (string source, String OUTPU t, int width, int height, double rotate) {try {thumbnails.of (source). Size (width, height). Rotate (rotate). ToFile (output);} catch (IOException e) {e.printstacktrace ();}} public static void Imgrotate (File source, String output, int width, int height, double rotate) {try {thumbnails.of (source) . Size (width, height). Rotate (rotate). ToFile (output); catch (IOException e) {e.printstacktrace ();}}/** * Watermark * * @param source * Input source * @param output * Input source * @param width * Width * @param hei Ght * High * @param position * Watermark Location positions.bottom_right o.5f * @param watermark * Watermark Image Address * @param transparency * Transparency 0.5f * @param quality * picture quality 0.8f */public static void Imgwatermark (Stri ng source, string output, int width, int height, Position Position, String watermark, float transparency, float quality) { try {thumbnails.of (source). Size (width, height). Watermark (position, Imageio.read (new File (watermark)), transparency) . outputquality (0.8f). ToFile (output);} catch (IOException e) {e.printstacktrace ();}} public static void Imgwatermark (File source, string output, int width, int height, Position Position, String Watermark, FL Oat transparency, float quality) {try {thumbnails.of (source). Size (width, height). Watermark (position, imageio.read (new File (watermark)), transparency). Outputquality (0.8f). ToFile (output); Catch (IOException e) {E.printstacktrace ();}} /** * Crop Picture * * @param source * Input source * @param output * source * @param position * Crop position * @par AM x * Crop area x * @param y * crop area y * @param width * Width * @param height * Height * @para M Keepaspectratio * is scaled by default, with a value of false not scaled */public static void Imgsourceregion (string source, String OUTPU T, Position Position, int x, int y, int width, int height, Boolean keepaspectratio) {try {thumbnails.of (source). Sourceregi On (position, x, y). Size (width, height). Keepaspectratio (Keepaspectratio). ToFile (output); catch (IOException e) {e.printstacktrace ();}} public static void Imgsourceregion (File source, String output, Position Position, int x, int y, int width, int height, boo Lean Keepaspectratio) {try {thumbnails.of (source). sourceregion (position, x, y). Size (width, height). Keepaspectratio ( Keepaspectratio). ToFile (output);} catch (IOException e) {e.printstacktrace ();}} /** * Crop by coordinates * * @paraM source * Input source * @param output * source * @param x * start x coordinate * @param y * start y coordinate * @ PARAM x1 * end x Coordinate * @param y1 * End y coordinate * @param width * Width * @param height * Height * @param keepaspectratio * is scaled by default, with a value of false not scaled */public static void Imgsourceregion (string source, string output, int x, int y, int x1, int y1, int width, int height, Boolean keepaspectratio) {try {thumbnails.of (source). Sourcere Gion (x, y, x1, y1). Size (width, height). Keepaspectratio (Keepaspectratio). ToFile (output); catch (IOException e) {e.printstacktrace ();}} public static void Imgsourceregion (File source, String output, int x, int y, int x1, int y1, int width, int height, Boolea n keepaspectratio) {try {thumbnails.of (source). sourceregion (x, y, x1, y1). Size (width, height). Keepaspectratio ( Keepaspectratio). ToFile (output);} catch (IOException e) {e.printstacktrace ();}} /** * Convert Image format * * @param source * Input source * @param output *           Output SOURCE * @param width * Width * @param height * Height * @param format * Picture type, GIF, PNG, JPG */public static void Imgformat (string source, string output, int width, int height, string format) {try {thumbnails.of (sou RCE). Size (width, height). OutputFormat (format). ToFile (output); catch (IOException e) {e.printstacktrace ();}} public static void Imgformat (File source, string output, int width, int height, string format) {try {thumbnails.of (source) . Size (width, height). OutputFormat (format). ToFile (output);} catch (IOException e) {e.printstacktrace ();}}  /** * Output to OutputStream * * @param source * Input source * @param output * source * @param width * Width * @param height * High * @return Tooutputstream (Stream object) */public static OutputStream Imgoutputstream (String source, St Ring output, int width, int height) {outputstream os = null;try {os = new FileOutputStream (output); Thumbnails.of (source). Size (width, height). tooutputstream (OS); catch (IoexceptiOn e) {e.printstacktrace ();} return OS;} public static OutputStream Imgoutputstream (File source, String output, int width, int height) {outputstream OS = null;try {OS = new FileOutputStream (output); Thumbnails.of (source). Size (width, height). tooutputstream (OS); catch (IOException e) {e.printstacktrace ();} return OS;} /** * Output to BufferedImage * * @param source * Input source * @param output * source * @param width * Width * @param height * High * @param format * Picture type, GIF, PNG, JPG * @return bufferedimage */public static Buffe Redimage imgbufferedimage (string source, string output, int width, int height, string format) {BufferedImage buf = null;tr y {buf = thumbnails.of (source). Size (width, height). asbufferedimage (); Imageio.write (buf, format, new File (output));} catch (IOException e) {e.printstacktrace ();} return BUF;} public static BufferedImage Imgbufferedimage (File source, string output, int width, int height, string format) {Bufferedim Age buf = null;try {buf =Thumbnails.of (source). Size (width, height). asbufferedimage (); Imageio.write (buf, format, new File);} catch (IOException e) {e.printstacktrace ();} return buf;}}

Using this kind of basic can complete the image of crop, thumbnail, watermark, image format conversion, output stream objects and other operations!

Reference Address: http://rensanning.iteye.com/blog/1545708



Java uses Thumbnailator image processing wrapper class

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.