how can i extract the colors in the image using java?

-2

I am trying with the following code to extract the colors in an image. I am using the following code but it is unable fetch the colors. Can anyone have better solution or please tell what is wrong in my code.

public static void main(String args[]) throws IOException {
    File file = new File("image.png");
    BufferedImage image = ImageIO.read(file);
    //  int clr;
    int redd = 0;
    int greenn = 0;
    int bluee = 0;
    for (int i = 0; i < image.getHeight(); i++) {
        for (int j = 0; j < image.getWidth(); j++) {
            int clr = image.getRGB(i, j);
            redd = (clr & 0x00ff0000) >> 16;
            greenn = (clr & 0x0000ff00) >> 8;
            bluee = clr & 0x000000ff;

        }
    }
    System.out.println("Red Color value = " + redd);
    System.out.println("Green Color value = " + greenn);
    System.out.println("Blue Color value = " + bluee);

Any suggestions will be highly helpful, Thanks :-)

java
ocr
rgb
bufferedimage
asked on Stack Overflow Jul 20, 2017 by vinay • edited Sep 3, 2018 by IMSoP

2 Answers

0

You might better off using Java's Color library to parse the image's RGB components:

Color clr = new Color(image.getRGB());
redd = c.getRed();
greenn = c.getGreen();
bluee = c.getBlue();

Can you see if that helps?

answered on Stack Overflow Jul 20, 2017 by itsmichaelwang
0

You can use some javafx features :

import javafx.scene.image.Image;
import javafx.scene.image.PixelReader;
import javafx.scene.paint.Color;

public class Test {
    public static void main(String[] args) {
        Image image = new Image("image.png");
        PixelReader reader = image.getPixelReader();
        Color c = reader.getColor(154, 87);
        System.out.println(c);
    }
}
answered on Stack Overflow Jul 20, 2017 by azro

User contributions licensed under CC BY-SA 3.0