How can I read image pixels' values as RGB into 2d array?

后端 未结 3 1445
梦毁少年i
梦毁少年i 2020-11-28 10:16

I was making a 2d map editor for my square tile platformer game, when I realized I could really use an image editor with its abilities to repaint adjacent pixels and many mo

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-28 10:26

    I think I've done something similar once. Here's a code snippet of what I was doing:

    public static void main(String[] args) {
            try {
    
                String path = "src/colors.jpg";
                BufferedImage image = ImageIO.read(new File(path));
                int w = image.getWidth();
                int h = image.getHeight();
                for (int y = 0; y < h; y++) {
                    for (int x = 0; x < w; x++) {
                        Color c = new Color(image.getRGB(x, y));
                        int red = c.getRed();
                        int green = c.getGreen();
                        int blue = c.getBlue();
                        countColor(red, green, blue);
                        totalCount++;
                    }
                }
    
                printColors();
    
            } catch (Exception ex) {
                System.out.println(ex.getMessage());
            }
        }
    

    In the inner for loop you can put something into an array[i][j]. (If that is what you're looking for)

提交回复
热议问题