package cn.com.qmth.stmms.ms;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
import javax.imageio.stream.FileImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.ImageProducer;
import java.io.File;

@SpringBootApplication
@EnableTransactionManagement
@EnableAsync
@EnableScheduling
public class Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

    public static void testRotate() throws Exception {

        File src = new File("/Users/yuanpan/Downloads/IMG_0762.JPG");
        File dest = new File("/Users/yuanpan/Downloads/IMG_0762_3.JPG");

        BufferedImage bufferedImage = ImageIO.read(src);
        ColorModel colorModel = bufferedImage.getColorModel();
        System.out.println(colorModel);
        /*
        String[] props = bufferedImage.getPropertyNames();
        for (String propName : props) {
            System.out.println("propName = "+propName);
            Object prop = bufferedImage.getProperty(propName);
            System.out.println(prop);
        }
        */
        ImageProducer imageProducer = bufferedImage.getSource();
        System.out.println(imageProducer);
        bufferedImage.getData();


        BufferedImage destImage = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.TYPE_INT_RGB);

        Graphics2D graphics2D = (Graphics2D) destImage.getGraphics();
        //graphics2D.rotate(0, bufferedImage.getWidth() / 2, bufferedImage.getHeight() / 2);

//        graphics2D.drawImage(bufferedImage, null, 0, 0);
//        ImageIO.write(destImage, "jpg", dest);
//        System.out.println("旋转完成");


        graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        graphics2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        graphics2D.drawImage(bufferedImage.getScaledInstance(bufferedImage.getWidth(), bufferedImage.getHeight(), Image.SCALE_SMOOTH), 0, 0, null);
        JPEGImageWriteParam jepgParams = new JPEGImageWriteParam(null);
        jepgParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        jepgParams.setCompressionQuality(1f);
        final ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next();

        writer.setOutput(new FileImageOutputStream(dest));
        writer.write(null, new IIOImage(destImage, null, null), jepgParams);
        writer.dispose();
        graphics2D.dispose();
        System.out.println("旋转完成");
    }

}
