Java Picture processing thumbnails framework

Source: Internet
Author: User

To set the zoom ratio of the picture or the quality of the picture

First step: Importing MAVEN's jar package

<dependency>    <groupId>net.coobird</groupId>    <artifactid>thumbnailator</ Artifactid>    <version>0.4.8</version></dependency>

Step two: Get a Builder<file> object

 Public Static Builder<file> of (String ... files) {    "cannot specify NULL for input files." );    " Cannot specify an empty array for input files. " );     return builder.ofstrings (arrays.aslist (Files));} ......

Step three: Set the picture scale

    • Scale by builder<file> object (double scale) method
    • Size by builder<file> object (int width, int height) method
    • By builder<file> object's height (int height) or width (int width) method

Fourth step: Set picture quality ratio

Outputquality (double quality) through the Builder<file> object

Second, get the information of the picture

First step: Get the BufferedImage object by Thumbnails.builder<file> 's Asbufferedimage () method and load the photos into memory

Step Two:

Gets the height of the picture by getheight (null) of the BufferedImage object

Get the width of a picture by getwidth (null) of the BufferedImage object

Small example:

     Public Static BooleanThumbnailscompresspic (String inputfile, String outputFile,intSizefloatquality) {File input=NewFile (inputfile); Try{Thumbnails.builder<File> Filebuilder = thumbnails.of (input). Scale (1.0). Outputquality (1.0); BufferedImage src=Filebuilder.asbufferedimage (); if(Src.getheight (NULL) > Size | | Src.getwidth (NULL) >size) {Thumbnails.builder<File> Builder =thumbnails.of (input); Builder.size (size, size); //take the largest size into size, and then zoombuilder.outputquality (Quality). ToFile (OutputFile); } Else{thumbnails.of (input). Scale (1.0). Outputquality (Quality). ToFile (OutputFile); }            return true; } Catch(IOException e) {logger.error (E.getmessage (), E); }        return false; }

Java Picture processing thumbnails framework

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.