使用Thumbnailtor实现图片的裁剪,缩放

pom.xml

<!-- thumbnail generation library for Java -->
<dependency>
  <groupId>net.coobird</groupId>
  <artifactId>thumbnailator</artifactId>
<version>0.4.8</version></dependency>
package team.soi.utils;

import net.coobird.thumbnailator.Thumbnails;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

/**
 * 图片工具类:<br/>
 * 使用Thumbnailtor实现图片的裁剪,缩放等。<br/>
 * 代码基于hoojo的工具类修改,主要是因为其代码使用了底层的API,具有平台依赖性。
 *
 * @author Soi 2015-10-15
 * @email [email protected]
 * @blog http://my.oschina.net/u/1580674
 */
public class ImageUtils {

    /**
     * 图像文件的格式
     */
    private static final String DEFAULT_IMAGE_FORMAT = ".jpg";
    /**
     * 默认文件存放路径
     */
    private static final String DEFAULT_FILE_PATH = "./temp-";

    private static Image image = null;

    private ImageUtils() {

    }

    /**
     * 图片压缩质量枚举<br/>
     * MAX-1.0f<br/>
     * HIGH-0.75f<br/>
     * MEDIUM-0.5f<br/>
     * LOW-0.25f<br/>
     */
    public enum ImageQuality {

        MAX(1.0f), HIGH(0.75f), MEDIUM(0.5f), LOW(0.25f);

        final Float QUALITY;

        ImageQuality(Float quality) {
            this.QUALITY = quality;
        }
    }

    /**
     * <b>function:</b> 通过目标对象的大小和标准(指定)大小计算出图片缩小的比例
     *
     * @param targetWidth    目标的宽度
     * @param targetHeight   目标的高度
     * @param standardWidth  标准(指定)宽度
     * @param standardHeight 标准(指定)高度
     * @return 最小的合适比例
     * @author hoojo
     * @createDate 2012-2-6 下午04:41:48
     */
    public static double getScaling(double targetWidth, double targetHeight, double standardWidth, double standardHeight) {
        double widthScaling = 0d;
        double heightScaling = 0d;
        if (targetWidth > standardWidth) {
            widthScaling = standardWidth / (targetWidth * 1.00d);
        } else {
            widthScaling = 1d;
        }
        if (targetHeight > standardHeight) {
            heightScaling = standardHeight / (targetHeight * 1.00d);
        } else {
            heightScaling = 1d;
        }
        return Math.min(widthScaling, heightScaling);
    }

    /**
     * 将Image的宽度、高度缩放到指定width、height,并保存在savePath目录<br/>
     *
     * @param width       缩放的宽度
     * @param height      缩放的高度
     * @param savePath    保存目录
     * @param targetImage 即将缩放的目标图片
     * @return 图片保存路径、名称
     * @throws IOException
     * @author Soi
     */
    public static String resize(int width, int height, String savePath, Image targetImage) throws IOException {
        return resize(width, height, ImageQuality.MAX.QUALITY, savePath, targetImage);
    }

    /**
     * <b>function:</b> 可以设置图片缩放质量,并且可以根据指定的宽高缩放图片
     *
     * @param width       缩放的宽度
     * @param height      缩放的高度
     * @param quality     图片压缩质量,最大值是1; 使用枚举值:{@link ImageQuality}
     *                    Some guidelines: 0.75 high quality、0.5  medium quality、0.25 low quality
     * @param savePath    保存目录
     * @param targetImage 即将缩放的目标图片
     * @return 图片保存路径、名称
     * @throws IOException
     * @author hoojo
     * @createDate 2012-2-7 上午11:01:27
     */
    public static String resize(int width, int height, Float quality, String savePath, Image targetImage) throws IOException {
        width = Math.max(width, 1);
        height = Math.max(height, 1);
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        image.getGraphics().drawImage(targetImage, 0, 0, width, height, null);

        if (savePath == null || "".equals(savePath)) {
            savePath = DEFAULT_FILE_PATH + System.currentTimeMillis() + DEFAULT_IMAGE_FORMAT;
        }

        if (quality == null || quality <= 0) {
            quality = ImageQuality.MAX.QUALITY;
        }

        Thumbnails.of(image).size(width, height).outputQuality(quality).toFile(new File(savePath));

        return savePath;
    }

    public static String resize(final int width, final int height, final String savePath, final String targetFile) throws IOException {
        return resize(width, height, savePath, new File(targetFile));
    }

    public static String resize(final int width, final int height, final String savePath, final File targetFile) throws IOException {
        return resize(width, height, savePath, new FileInputStream(targetFile));
    }

    public static String resize(final int width, final int height, final String savePath, InputStream inputStream) throws IOException {
        image = ImageIO.read(inputStream);
        int[] size = getSize(width, height, image);
        return resize(size[0], size[1], savePath, image);
    }

    /**
     * <b>function:</b> 将一个网络图片文件按照指定的比例进行缩放
     *
     * @param scale     缩放比例
     * @param savePath  保存文件路径、名称
     * @param targetURL 本地图片文件
     * @return 新的文件名称
     * @throws IOException
     * @author hoojo
     * @createDate 2012-2-7 上午10:30:56
     */
    public static String resize(final float scale, final String savePath, final URL targetURL) throws IOException {
        image = ImageIO.read(targetURL);
        int[] size = getSize(scale, image);
        return resize(size[0], size[1], savePath, image);
    }


    /**
     * <b>function:</b> 按照固定宽度进行等比缩放本地图片
     *
     * @param width      固定宽度
     * @param savePath   保存路径、名称
     * @param targetFile 本地目标文件
     * @return 返回保存路径
     * @throws IOException
     * @author hoojo
     * @createDate 2012-2-7 上午10:49:56
     */
    public static String resize(final int width, final String savePath, final File targetFile) throws IOException {
        image = ImageIO.read(targetFile);
        int[] size = getSize(width, image);
        return resize(size[0], size[1], savePath, image);
    }

    /**
     * <b>function:</b> 按照固定宽度进行等比缩放网络图片
     *
     * @param width     固定宽度
     * @param savePath  保存路径、名称
     * @param targetURL 本地目标文件
     * @return 返回保存路径
     * @throws IOException
     * @author hoojo
     * @createDate 2012-2-7 上午10:50:52
     */
    public static String resize(final int width, final String savePath, final URL targetURL) throws IOException {
        image = ImageIO.read(targetURL);
        int[] size = getSize(width, image);
        return resize(size[0], size[1], savePath, image);
    }

    /**
     * <b>function:</b> 按照固定高度进行等比缩放本地图片
     *
     * @param height     固定高度
     * @param savePath   保存路径、名称
     * @param targetFile 本地目标文件
     * @return 返回保存路径
     * @throws IOException
     * @author hoojo
     * @createDate 2012-2-7 上午10:51:17
     */
    public static String resizeByHeight(final int height, final String savePath, final File targetFile) throws IOException {
        image = ImageIO.read(targetFile);
        int[] size = getSizeByHeight(height, image);
        return resize(size[0], size[1], savePath, image);
    }

    /**
     * <b>function:</b> 按照固定高度进行等比缩放网络图片
     *
     * @param height    固定高度
     * @param savePath  保存路径、名称
     * @param targetURL 本地目标文件
     * @return 返回保存路径
     * @throws IOException
     * @author hoojo
     * @createDate 2012-2-7 上午10:52:23
     */
    public static String resizeByHeight(final int height, final String savePath, final URL targetURL) throws IOException {
        image = ImageIO.read(targetURL);
        int[] size = getSizeByHeight(height, image);
        return resize(size[0], size[1], savePath, image);
    }

    /**
     * <b>function:</b> 通过指定大小和图片的大小,计算出图片缩小的合适大小
     *
     * @param width  指定的宽度
     * @param height 指定的高度
     * @param image  图片文件
     * @return 返回宽度、高度的int数组
     * @author hoojo
     * @createDate 2012-2-6 下午05:53:10
     */
    public static int[] getSize(final int width, final int height, final Image image) {
        int[] target = getSize(image);
        double scaling = getScaling(target[0], target[1], width, height);
        long[] standard = getStandard(target, scaling);
        return new int[]{Integer.parseInt(Long.toString(standard[0])), Integer.parseInt(String.valueOf(standard[1]))};
    }

    /**
     * <b>function:</b> 通过指定的比例和图片对象,返回一个放大或缩小的宽度、高度
     *
     * @param scale 缩放比例
     * @param image 图片对象
     * @return 返回宽度、高度
     * @author hoojo
     * @createDate 2012-2-7 上午10:27:59
     */
    public static int[] getSize(final float scale, final Image image) {
        int[] target = getSize(image);
        long[] standard = getStandard(target, scale);
        return new int[]{Integer.parseInt(Long.toString(standard[0])), Integer.parseInt(String.valueOf(standard[1]))};
    }

    public static int[] getSize(final int width, final Image image) {
        int[] target = getSize(image);
        long height = Math.round((target[1] * width) / (target[0] * 1.00f));
        return new int[]{width, Integer.parseInt(String.valueOf(height))};
    }

    public static int[] getSizeByHeight(final int height, final Image image) {
        int[] target = getSize(image);
        long width = Math.round((target[0] * height) / (target[1] * 1.00f));
        return new int[]{Integer.parseInt(String.valueOf(width)), height};
    }

    public static int[] getSize(final Image image) {
        int targetWidth = image.getWidth(null);
        int targetHeight = image.getHeight(null);
        return new int[]{targetWidth, targetHeight};
    }

    public static long[] getStandard(final int[] target, final double scale) {
        long standardWidth = Math.round(target[0] * scale);
        long standardHeight = Math.round(target[1] * scale);
        return new long[]{standardWidth, standardHeight};
    }

    public static void main(String[] args) throws IOException{
        ImageUtils.resize(500,500,"/home/soi/xn.png","/home/soi/012133dsa4toxud8snsbo1.jpg");
    }

}


你可能感兴趣的:(使用Thumbnailtor实现图片的裁剪,缩放)