package com.qmth.paper.library.common.util;


import net.coobird.thumbnailator.Thumbnails;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

/**
 * 图片工具类
 */
public class ImageUtil {

    private static final Logger log = LoggerFactory.getLogger(ImageUtil.class);

    /**
     * 裁剪图片
     *
     * @param sourceFile 源文件
     * @param x          x起点
     * @param y          y起点
     * @param width      宽度
     * @param height     调试
     * @param outFile    输出文件
     */
    public static File subImg(File sourceFile, int x, int y, int width, int height, File outFile) {
        ImageInputStream iis = null;
        try {
            if (outFile.exists()) {
                outFile.delete();
            }
            //图片输入流
            iis = ImageIO.createImageInputStream(sourceFile);
            //图片读取器
            Iterator<ImageReader> it = ImageIO.getImageReaders(iis);

            if (it.hasNext()) {
                ImageReader r = it.next();
                //设置输入流
                r.setInput(iis, true);
                //读取参数
                ImageReadParam param = r.getDefaultReadParam();
                //创建要截取的矩形范围
                Rectangle rect = new Rectangle(x, y, width, height);
                //设置截取范围参数
                param.setSourceRegion(rect);
                //读取截图数据
                BufferedImage bi = r.read(0, param);
                // 保存图片
                ImageIO.write(bi, "jpg", outFile);
            }
            return outFile;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                iis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 图片旋转
     *
     * @param sourceFile 原始图片
     * @param rotate     旋转角度
     * @param outFile    输出图片
     */
    public static File rotate(File sourceFile, int rotate, File outFile) {
        try {
            // rotate为0，不旋转
            if (rotate == 0) {
                return sourceFile;
            }
            Thumbnails.of(sourceFile).scale(0.8).rotate(rotate).toFile(outFile);
        } catch (IOException e) {
            log.error(e.getMessage(),e);
        }
        return outFile;
    }

    public static File rotate(InputStream inputStream, int rotate, File outFile) {
        try {
            // rotate为0，不旋转
            if (rotate == 0) {
                Thumbnails.of(inputStream).toFile(outFile);
            } else {
                Thumbnails.of(inputStream).scale(1).rotate(rotate).outputQuality(1).toFile(outFile);
            }
        } catch (IOException e) {
            log.error(e.getMessage(),e);
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                log.error(e.getMessage(),e);
            }
        }
        return outFile;
    }

}
