package com.izouma.nineth.utils;

import net.coobird.thumbnailator.Thumbnails;
import org.pngquant.PngQuant;

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

public class ImageUtils {

    public static BufferedImage resizeJpg(InputStream image, int width, int height) throws IOException {
        BufferedImage bufferedImage = ImageIO.read(image);
        return Thumbnails.of(bufferedImage)
                .size(Math.min(width, bufferedImage.getWidth()), Math.min(height, bufferedImage.getHeight()))
                .outputFormat("jpg")
                .asBufferedImage();
    }

    public static BufferedImage resizePng(InputStream image, int width, int height, boolean compress) throws IOException {
        BufferedImage bufferedImage = ImageIO.read(image);
        BufferedImage img = Thumbnails.of(bufferedImage)
                .size(Math.min(width, bufferedImage.getWidth()), Math.min(height, bufferedImage.getHeight()))
                .outputFormat("png")
                .asBufferedImage();
        bufferedImage.flush();
        PngQuant pngQuant = new PngQuant();
        return pngQuant.getRemapped(img);
    }

    public static void main(String[] args) throws IOException {
        ImageUtils.resizePng(new FileInputStream("/Users/drew/Downloads/盲盒详情.png"), 1000, 2000,true);
        PngQuant pngQuant = new PngQuant();
        ImageIO.write(pngQuant
                        .getRemapped(ImageIO.read(new File("/Users/drew/Desktop/1.png"))),
                "png", new File("/Users/drew/Desktop/2.png"));
    }
}
