how to convert an image to matrix in java

Solutions on MaxInterview for how to convert an image to matrix in java by the best coders in the world

showing results for - "how to convert an image to matrix in java"
Sami
15 Aug 2017
1import java.awt.Color;
2import java.awt.image.BufferedImage;
3import java.io.File;
4import java.io.IOException;
5import javax.imageio.ImageIO;
6
7public class ImageUtil {
8
9    public static Color[][] loadPixelsFromFile(File file) throws IOException {
10
11        BufferedImage image = ImageIO.read(file);
12        Color[][] colors = new Color[image.getWidth()][image.getHeight()];
13
14        for (int x = 0; x < image.getWidth(); x++) {
15            for (int y = 0; y < image.getHeight(); y++) {
16                colors[x][y] = new Color(image.getRGB(x, y));
17            }
18        }
19
20        return colors;
21    }
22
23    public static void main(String[] args) throws IOException {
24        Color[][] colors = loadPixelsFromFile(new File("stars.jpg"));
25        System.out.println("Color[0][0] = " + colors[0][0]);
26    }
27}