Java uses Thumbnailator-0.4.8.jar to generate thumbnails

Source: Internet
Author: User

Scene One: Image size unchanged, modify picture file type

use:

thumbnails.of ("F:\\image\\img_20131229_114806.png") . Scale (1f)

. OutputFormat ("jpg")

. ToFile ("f:\\image\\output\\img_20131229_114806");

Note:OutputFormat: The picture format of the output. Note that the ToFile () method does not have a suffix for the file type after the method is used, otherwise a picture of IMG_20131229_114806.jpg.jpg is generated.


Scene Two: Image size unchanged, compressed picture file size

Use:

thumbnails.of ("F:\\image\\img_20131229_114806.png") . Scale (1f)

. Outputquality (0. 25f)

. OutputFormat ("jpg")

. ToFile ("f:\\image\\output\\img_20131229_114806");

Note: outputquality: Output picture quality, Range: 0.0~1.0,1 for highest quality. Note the image format that you output when you use this method must be a JPG (that is, OutputFormat ("JPG"). Other formats I have not tried, interested in themselves can try). Otherwise if the output PNG format picture, then this method effect invalid "This actually should be considered a bug".


Scene Three: Compress to the specified picture size (for example: Horizontal 400 high 300), do not maintain the picture scale

Use:

thumbnails.of ("F:\\image\\img_20131229_114806.png")

. forcesize (+)

. ToFile ("f:\\image\\output\\img_20131229_114806");


Scene Four: Compress to the specified picture size ( For example: Horizontal 400 high), keep the picture intact, the extra part cut off

Use:

String ImagePath = "F:\\image\\img_20131229_114806.jpg";

BufferedImage image = Imageio.read (new File (ImagePath));

builder<bufferedimage> Builder = null;   

int imagewidth = Image.getwidth ();

int imageheitht = Image.getheight ();

if (float) + /- (float) imagewidth/imageheitht) {

if (ImageWidth > Imageheitht) {

image = Thumbnails.of (imagePath). Height (+). Asbufferedimage ();

} Else {

image = Thumbnails.of (imagePath). Width (N). Asbufferedimage ();

}

Builder = thumbnails.of (image). Sourceregion (Positions.center, .) . Size ( );

} Else {

builder = thumbnails.of (image). Size ( +);

}

Builder.outputformat ("jpg"). ToFile ("f:\\image\\output\\img_20131229_114806");

This is a bit more complicated than the size () method (since the height ratio is not necessarily 4/3, so the compressed picture is either a zero or a high of 300) and cannot be used with the Forcesize () method. First judge the cross-height ratio, determine whether the horizontal 400 compression or high 300 compression, after compression by the center of the 400*300 area to cut, so that the resulting picture is 400*300 cropped thumbnail.

when using the size () or Forcesize () method, if the picture is smaller than the specified size (such as size (400, 300) and the picture is 40*30), it is stretched to the specified size.  


Java uses Thumbnailator-0.4.8.jar to generate thumbnails

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.