Java Graphics How to - Convert image into pixels data








Question

We would like to know how to convert image into pixels data.

Answer

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
//from www .ja  v  a2 s.  co  m
import javax.imageio.ImageIO;

public class Main {
  public static void main(String args[]) throws IOException {
    File file = new File("your file path.jpg");
    BufferedImage image = ImageIO.read(file);

    for (int i = 0; i < image.getHeight(); i++) {
      for (int j = 0; j < image.getWidth(); j++) {
        int color = image.getRGB(j, i);
        // convert the color to a readable format
        String clr = Integer.toHexString(color).substring(2);
        System.out.println(clr);
      }
    }
  }
}