package com.ai.da.common.utils;

import lombok.extern.slf4j.Slf4j;

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

/**
 * 图片工具类
 */
@Slf4j
public class ImageUtil {
    static BufferedImage bufferedImage;
    static int r, g, b; // 分别用来存放获取的RGB值
    static int heigth, width;
    static int id = 0;

    public static void find() {
        // 读取要操作的图片，这里的图片路径请改成自己要处理的图片
        try {
            bufferedImage = ImageIO.read(new File("D:\\programManager\\private\\curtain\\curtain\\WechatIMG170.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 获取图片的宽和高；
        heigth = bufferedImage.getHeight();
        width = bufferedImage.getHeight();
        System.out.println("heigth   = " + heigth + ", width   =" + width);
        // 采用行优先遍历，先遍历宽
        for (int y = 0; y < heigth; y++) {
            for (int x = 0; x < width; x++) {
                id++;
                Color color = new Color(bufferedImage.getRGB(x, y));
                r = color.getRed();
                g = color.getGreen();
                b = color.getBlue();
                System.out.println("此时的id为： " + id + "  R =  " + r + ",  G = " + g + ", B =" + b);
            }
        }
    }

    public static void main(String[] args) {
        find();
    }
}
