Java generated thumbnail thumbnailator (reprint)

Source: Internet
Author: User

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
  1. Size (width, height)
  2. /*
  3. * If the picture shape factor 200 small, high than 300 small, unchanged
  4. * If the picture shape factor 200 small, high ratio 300, high shrink to 300, picture proportion unchanged
  5. * If the picture shape factor 200, the height is 300 small, the cross shrinks to 200, the picture proportion does not change
  6. * If the picture shape factor 200 large, high than 300, the picture is scaled down, the horizontal is 200 or 300
  7. */
  8. Thumbnails.of ("images/a380_1280x1024.jpg")
  9. . Size (max. )
  10. . ToFile ("c:/a380_200x300.jpg");
  11. Thumbnails.of ("images/a380_1280x1024.jpg")
  12. . Size (2560, 2048)
  13. . ToFile ("c:/a380_2560x2048.jpg");



2. Scale by scale

Java code
    1. Scale (proportional)
    2. Thumbnails.of ("images/a380_1280x1024.jpg")
    3. . Scale (0.25f)
    4. . ToFile ("c:/a380_25%.jpg");
    5. Thumbnails.of ("images/a380_1280x1024.jpg")
    6. . Scale (1.10f)
    7. . ToFile ("c:/a380_110%.jpg");



3, do not scale, specify size to zoom

Java code
    1. Keepaspectratio (false) is scaled by default
    2. Thumbnails.of ("images/a380_1280x1024.jpg")
    3. . Size ($, $ )
    4. . Keepaspectratio (false)
    5. . ToFile ("c:/a380_200x200.jpg");



4. Rotation

Java code
  1. Rotate (angle), positive: clockwise negative: counterclockwise
  2. Thumbnails.of ("images/a380_1280x1024.jpg")
  3. . Size (for all, 1024x768)
  4. . Rotate (+)
  5. . ToFile ("c:/a380_rotate+90.jpg");
  6. Thumbnails.of ("images/a380_1280x1024.jpg")
  7. . Size (for all, 1024x768)
  8. . Rotate (-)
  9. . ToFile ("c:/a380_rotate-90.jpg");




5. Watermark

Java code
  1. Watermark (location, watermark, transparency)
  2. Thumbnails.of ("images/a380_1280x1024.jpg")
  3. . Size (for all, 1024x768)
  4. . Watermark (Positions.bottom_right, Imageio.read (new File ("Images/watermark.png")), 0.5f)
  5. . outputquality (0.8f)
  6. . ToFile ("c:/a380_watermark_bottom_right.jpg");
  7. Thumbnails.of ("images/a380_1280x1024.jpg")
  8. . Size (for all, 1024x768)
  9. . Watermark (Positions.center, Imageio.read (new File ("Images/watermark.png")), 0.5f)
  10. . outputquality (0.8f)
  11. . ToFile ("c:/a380_watermark_center.jpg");




6, cutting

Java code
  1. Sourceregion ()
  2. Image Center 400*400 Area
  3. Thumbnails.of ("images/a380_1280x1024.jpg")
  4. . sourceregion (Positions.center, I , I)
  5. . Size ($, $ )
  6. . Keepaspectratio (false)
  7. . ToFile ("c:/a380_region_center.jpg");
  8. Picture lower right 400*400 area
  9. Thumbnails.of ("images/a380_1280x1024.jpg")
  10. . sourceregion (Positions.bottom_right, I , I)
  11. . Size ($, $ )
  12. . Keepaspectratio (false)
  13. . ToFile ("c:/a380_region_bootom_right.jpg");
  14. Specify coordinates
  15. Thumbnails.of ("images/a380_1280x1024.jpg")
  16. . sourceregion (+---- )
  17. . Size ($, $ )
  18. . Keepaspectratio (false)
  19. . ToFile ("c:/a380_region_coord.jpg");




7. Conversion image format

Java code
  1. OutputFormat (image format)
  2. Thumbnails.of ("images/a380_1280x1024.jpg")
  3. . Size (for all, 1024x768)
  4. . OutputFormat ("PNG")
  5. . ToFile ("c:/a380_1280x1024.png");
  6. Thumbnails.of ("images/a380_1280x1024.jpg")
  7. . Size (for all, 1024x768)
  8. . OutputFormat ("gif")
  9. . ToFile ("c:/a380_1280x1024.gif");



8, output to OutputStream

Java code
    1. Tooutputstream (Stream object)
    2. OutputStream OS = new FileOutputStream ("c:/a380_1280x1024_outputstream.png");
    3. Thumbnails.of ("images/a380_1280x1024.jpg")
    4. . Size (for all, 1024x768)
    5. . Tooutputstream (OS);



9, output to BufferedImage

Java code
    1. Asbufferedimage () return bufferedimage
    2. BufferedImage thumbnail = thumbnails.of ("images/a380_1280x1024.jpg")
    3. . Size (for all, 1024x768)
    4. . Asbufferedimage ();
    5. 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)

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.