Turn from (http://rensanning.iteye.com/blog/1545708)
Thumbnailator is a more streamlined thumbnail generation library for the Java interface. 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.
Thumbnailator-0.4.8.jar:http://files.cnblogs.com/files/haha12/thumbnailator-0.4.8jar%e5%8c%85.rar
The original image is as follows:
1. Scale with the specified size
Java code
- Size (width, height)
- /*
- * If the picture shape factor 200 small, high than 300 small, unchanged
- * If the picture shape factor 200 small, high ratio 300, high shrink to 300, picture proportion unchanged
- * If the picture shape factor 200, the height is 300 small, the cross shrinks to 200, the picture proportion does not change
- * If the picture shape factor 200 large, high than 300, the picture is scaled down, the horizontal is 200 or 300
- */
- Thumbnails.of ("images/a380_1280x1024.jpg")
- . Size (max. )
- . ToFile ("c:/a380_200x300.jpg");
- Thumbnails.of ("images/a380_1280x1024.jpg")
- . Size (2560, 2048)
- . ToFile ("c:/a380_2560x2048.jpg");
2. Scale by scale
Java code
- Scale (proportional)
- Thumbnails.of ("images/a380_1280x1024.jpg")
- . Scale (0.25f)
- . ToFile ("c:/a380_25%.jpg");
- Thumbnails.of ("images/a380_1280x1024.jpg")
- . Scale (1.10f)
- . ToFile ("c:/a380_110%.jpg");
3, do not scale, specify size to zoom
Java code
- Keepaspectratio (false) is scaled by default
- Thumbnails.of ("images/a380_1280x1024.jpg")
- . Size ($, $ )
- . Keepaspectratio (false)
- . ToFile ("c:/a380_200x200.jpg");
4. Rotation
Java code
- Rotate (angle), positive: clockwise negative: counterclockwise
- Thumbnails.of ("images/a380_1280x1024.jpg")
- . Size (for all, 1024x768)
- . Rotate (+)
- . ToFile ("c:/a380_rotate+90.jpg");
- Thumbnails.of ("images/a380_1280x1024.jpg")
- . Size (for all, 1024x768)
- . Rotate (-)
- . ToFile ("c:/a380_rotate-90.jpg");
5. Watermark
Java code
- Watermark (location, watermark, transparency)
- Thumbnails.of ("images/a380_1280x1024.jpg")
- . Size (for all, 1024x768)
- . Watermark (Positions.bottom_right, Imageio.read (new File ("Images/watermark.png")), 0.5f)
- . outputquality (0.8f)
- . ToFile ("c:/a380_watermark_bottom_right.jpg");
- Thumbnails.of ("images/a380_1280x1024.jpg")
- . Size (for all, 1024x768)
- . Watermark (Positions.center, Imageio.read (new File ("Images/watermark.png")), 0.5f)
- . outputquality (0.8f)
- . ToFile ("c:/a380_watermark_center.jpg");
6, cutting
Java code
- Sourceregion ()
- Image Center 400*400 Area
- Thumbnails.of ("images/a380_1280x1024.jpg")
- . sourceregion (Positions.center, I , I)
- . Size ($, $ )
- . Keepaspectratio (false)
- . ToFile ("c:/a380_region_center.jpg");
- Picture lower right 400*400 area
- Thumbnails.of ("images/a380_1280x1024.jpg")
- . sourceregion (Positions.bottom_right, I , I)
- . Size ($, $ )
- . Keepaspectratio (false)
- . ToFile ("c:/a380_region_bootom_right.jpg");
- Specify coordinates
- Thumbnails.of ("images/a380_1280x1024.jpg")
- . sourceregion (+---- )
- . Size ($, $ )
- . Keepaspectratio (false)
- . ToFile ("c:/a380_region_coord.jpg");
7. Conversion image format
Java code
- OutputFormat (image format)
- Thumbnails.of ("images/a380_1280x1024.jpg")
- . Size (for all, 1024x768)
- . OutputFormat ("PNG")
- . ToFile ("c:/a380_1280x1024.png");
- Thumbnails.of ("images/a380_1280x1024.jpg")
- . Size (for all, 1024x768)
- . OutputFormat ("gif")
- . ToFile ("c:/a380_1280x1024.gif");
8, output to OutputStream
Java code
- Tooutputstream (Stream object)
- OutputStream OS = new FileOutputStream ("c:/a380_1280x1024_outputstream.png");
- Thumbnails.of ("images/a380_1280x1024.jpg")
- . Size (for all, 1024x768)
- . Tooutputstream (OS);
9, output to BufferedImage
Java code
- Asbufferedimage () return bufferedimage
- BufferedImage thumbnail = thumbnails.of ("images/a380_1280x1024.jpg")
- . Size (for all, 1024x768)
- . Asbufferedimage ();
- Imageio.write (thumbnail, "jpg", new File ("c:/a380_1280x1024_bufferedimage.jpg"));
It should be noted that for CMYK mode images, due to the JDK bug, not currently able to handle, the following exception:
Reference javax.imageio.IIOException:Unsupported Image Type
At Com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal (jpegimagereader.java:1063)
At Com.sun.imageio.plugins.jpeg.JPEGImageReader.read (jpegimagereader.java:1034)
At Javax.imageio.ImageReader.read (imagereader.java:940)
At Net.coobird.thumbnailator.tasks.io.FileImageSource.read (Unknown Source)
Reference javax.imageio.IIOException:Incompatible Color Conversion
At Com.sun.imageio.plugins.jpeg.JPEGImageReader.checkColorConversion (jpegimagereader.java:973)
ICC profile
References java.lang.IllegalArgumentException:Numbers of source Raster bands and source color space components does not match
At Java.awt.image.ColorConvertOp.filter (colorconvertop.java:482)
At Com.sun.imageio.plugins.jpeg.JPEGImageReader.acceptPixels (jpegimagereader.java:1268)
At Com.sun.imageio.plugins.jpeg.JPEGImageReader.readImage (Native Method)
At Com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal (jpegimagereader.java:1236)
At Com.sun.imageio.plugins.jpeg.JPEGImageReader.read (jpegimagereader.java:1039)
At Javax.imageio.ImageReader.read (imagereader.java:939)
These problems can be solved jai.create () instead of Imageio.read (). The memory overflow oom problem for HD graphs can only be converted using ImageMagick.
Java generated thumbnail thumbnailator (reprint)