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