Google's use of Java language with pictures Thumbnailator

REVIEW: In Java to create high quality thumbnail images can be a very difficult task, or slow page loads high picture quality and other reasons the need to deal with the picture, but do not want to write too much code too much trouble to deal with pictures , then you can use Thumbnailator - Thumbnailator is a single JAR file, do not rely on external libraries, the development and deployment simple and easy.

1.Thumbnailator can provide those functions

  1. Image cropping
  2. Modify pixels
  3. Reduce / expand
  4. Compression quality
  5. Rotation
  6. Watermarked
  7. Convert image formats
  8. other……

2.Thumbnailator how the introduction of the project

  The maven pom.xml add the following dependency:

<!-- https://mvnrepository.com/artifact/net.coobird/thumbnailator -->
<dependency>
    <groupId>net.coobird</groupId>
    <artifactId>thumbnailator</artifactId>
    <version>0.4.8</version>
</dependency>

  Thumbnailator-0.4.8.jar can download the package, into the project lib below;

3.Thumbnailator how to use

package com.tao.springboot;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Thumbnailator {
    public static void main(String[] args) throws IOException {
        // 需要压缩的图片
        File inFile = new File("C:/images/test.jpg");
        // 压缩完的存放地址
        String outFile = "C: /images/test.jpg " ;
         // image cropping 
        Thumbnails.of (inFile) .sourceRegion ( 0 , 0 , 400 , 400 ) .size ( 200 , 200 ) .toFile (the outFile);
         // modify pixel 
        Thumbnails. of (inFile) .size ( 200 is , 200 is ) .toFile (the outFile);
         // reduction 
        Thumbnails.of (inFile) .scale ( 0.5f ) .toFile (the outFile);
         // enlarged 
        Thumbnails.of (inFile) .scale ( 2F) .toFile (the outFile);
         // compression quality
        Thumbnails.of(inFile).outputQuality(0.5f).toFile(outFile);
        // 旋转
        Thumbnails.of(inFile).rotate(90).toFile(outFile);
        // 图片水印
        File waterFile = new File("C:/images/water.jpg");
        Thumbnails.of(inFile).watermark(Positions.BOTTOM_CENTER, ImageIO.read(waterFile),0.5f).toFile(outFile);
        // 文字水印
        BufferedImage bi = new BufferedImage(80,30,BufferedImage.TYPE_INT_BGR);
        Graphics2D g = bi.createGraphics();
        g.setColor(Color.lightGray);
        g.drawRect(0,0,50,10);
        g.drawString("文字水印", 20,20);
        Thumbnails.of(inFile).watermark(Positions.BOTTOM_LEFT, bi, 0.5f).toFile(outFile);
        // 转化图片格式
        Thumbnails.of(inFile).outputFormatType("png").toFile(outFile);
    }

}

4. References

github Address: https://github.com/coobird/thumbnailator

maven Address: https://mvnrepository.com/artifact/net.coobird/thumbnailator

Guess you like

Origin www.cnblogs.com/i-tao/p/10963014.html