0

I want to read an image and print ARGB value of all pixels.

This is how I try to achieve that:

public static void main(String[] ar){ Image image = new Image("file:///C:/Users/PC2/Desktop/duke_44x80.png"); //ERROR HERE ImageView imageView = new ImageView(); imageView.setImage(image); PixelReader pixelReader = image.getPixelReader(); for(int x = 0; x < image.getWidth(); ++x){ for(int y = 0; y < image.getHeight(); ++y){ System.out.print(pixelReader.getArgb(x, y) + ", "); } System.out.println(); } } 

But when I try to run it, I get an error at line Image image = new Image(...);

Exception in thread "main" java.lang.RuntimeException: Internal graphics not initialized yet at javafx.graphics/com.sun.glass.ui.Screen.getScreens(Screen.java:70) at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.getScreens(QuantumToolkit.java:699) at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.getMaxRenderScale(QuantumToolkit.java:726) at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.loadImage(QuantumToolkit.java:735) at javafx.graphics/javafx.scene.image.Image.loadImage(Image.java:1052) at javafx.graphics/javafx.scene.image.Image.initialize(Image.java:802) at javafx.graphics/javafx.scene.image.Image.<init>(Image.java:618) at test.core.MainCore.main(MainCore.java:11) 

How to fix this error?

3
  • 3
    You need to start the JavaFX runtime. Is this going to be part of a larger JavaFX application? Commented May 14, 2019 at 14:01
  • Try running the code in a JavaFX project. Commented May 14, 2019 at 14:03
  • @Slaw Now it works. Thank you. No, this is all code. Commented May 14, 2019 at 14:04

1 Answer 1

2

You can try without JavaFX, like this:

Path path = Paths.get("C:/Users/PC2/Desktop/duke_44x80.png"); try(InputStream is = Files.newInputStream(path)) { BufferedImage bi = ImageIO.read(is); // Use ImageIO to create a BufferedImage int w = bi.getWidth(); int h = bi.getHeight(); for(int i = 0; i < h; i++) { for(int j = 0; j < w; j++) { Color myColor = new Color(bi.getRGB(j, i)); // bi.getRGB returns an integer like -14350844, representing the specific color. use Color class to get the individual colors with: myColor.getBlue()... System.out.print(myColor + ", "); } System.out.println(); } }catch(IOException e) { System.out.println(e); } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.