Answers for "how to convert an image to matrix in java"

0

how to convert an image to matrix in java

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

public class ImageUtil {

    public static Color[][] loadPixelsFromFile(File file) throws IOException {

        BufferedImage image = ImageIO.read(file);
        Color[][] colors = new Color[image.getWidth()][image.getHeight()];

        for (int x = 0; x < image.getWidth(); x++) {
            for (int y = 0; y < image.getHeight(); y++) {
                colors[x][y] = new Color(image.getRGB(x, y));
            }
        }

        return colors;
    }

    public static void main(String[] args) throws IOException {
        Color[][] colors = loadPixelsFromFile(new File("stars.jpg"));
        System.out.println("Color[0][0] = " + colors[0][0]);
    }
}
Posted by: Guest on July-31-2021

Code answers related to "how to convert an image to matrix in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language