package com.casic.missiles.service.listeners.register;

import com.casic.missiles.dto.business.file.CropPicture;
import com.spire.doc.fields.DocPicture;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class ImageTool {

    public static List<String> crosswiseCutting(CropPicture cropPicture) throws IOException {
        // 读入大图
        File file = new File(cropPicture.getTargetImgUrl());
        FileInputStream fis = new FileInputStream(file);
        System.out.println(file.exists());
        BufferedImage image = ImageIO.read(fis);
        int chunks = cropPicture.getCutRows() * cropPicture.getCutCols();
        ImageReader imageReader=ImageIO.getImageReadersBySuffix(cropPicture.getTargetImgUrl()).next();
        // 计算每个小图的宽度和高度
        int chunkWidth = image.getWidth() / cropPicture.getCutCols();
        int chunkHeight = image.getHeight() / cropPicture.getCutRows();
        System.out.println("图片的宽度为:" + chunkWidth * cropPicture.getCutRows() + "图片的高度为:" + chunkHeight * cropPicture.getCutCols());//230,278
        BufferedImage imgs[] = new BufferedImage[chunks];
        for (int x = 0,count = 0; x < cropPicture.getCutRows(); x++) {
            for (int y = 0; y < cropPicture.getCutCols(); y++) {
                //设置小图的大小和类型
                imgs[count] = new BufferedImage(chunkWidth, chunkHeight, image.getType());
                //写入图像内容
                Graphics2D gr = imgs[count++].createGraphics();
                gr.drawImage(image, 0, 0, chunkWidth, chunkHeight, chunkWidth * y, chunkHeight * x, chunkWidth * y + chunkWidth, chunkHeight * x + chunkHeight, null);
                System.out.println("源矩阵第一个角的坐标" + chunkWidth * y + "+" + chunkHeight * x + "源矩阵第二个角的坐标" + chunkWidth * (y + 1) + "+" + chunkHeight * (x + 1));
                gr.dispose();
            }
        }
        List<String> subfileUrls = new ArrayList<>();
        // 输出小图
        for (int i = 0; i < imgs.length; i++) {
            subfileUrls.add(cropPicture.getSubfileUrl() + i + cropPicture.getSubfileType());
            ImageIO.write(imgs[i], cropPicture.getSubfileType(), new File(subfileUrls.get(subfileUrls.size() - 1)));
            System.out.println(i);
        }
        System.out.println("完成分割！");
        return subfileUrls;
    }

    public static void main(String[] agrs) throws IOException {
        String originalImg = "D:\\casic\\tmp\\1645701777117741057\\ceshi.jpg";
        // 读入大图
        File file = new File(originalImg);
        FileInputStream fis = new FileInputStream(file);
        System.out.println(file.exists());
        BufferedImage image = ImageIO.read(fis);
        /*
         * 假设我需要切割的四点坐标为a(20,30)b(200,40)c(30,200)d(200,210)
         * 起始坐标为(最小的x,最小的y)
         * 此时的实际切割坐标应为由a为起始坐标,width为(x坐标最大的点的x - a的x坐标),height为(y坐标最大的点的y - a的y坐标)
         * 此时的width为180,height为180
         */
        //切割图片
        BufferedImage bf = new BufferedImage(image.getWidth(), image.getHeight(), image.getType());
        Graphics2D graphics2D = bf.createGraphics();
        graphics2D.drawImage(image, 20, 30, 180, 180, 0, 0, 180, 180, null);
        graphics2D.dispose();
        //输出图片
        Random random = new Random();
        int j = random.nextInt(1000);
        ImageIO.write(bf, "jpg", new File("D:\\casic\\tmp\\1645701777117741057" + j + ".jpg"));
        System.out.println("完成切割");
    }

}
