0

I'm trying to get an image matriz (pixels configuration) in a fast way... In JAVA

I can get the matriz but depend the resolution of the pic, it takes a lot of time, so if somebody knows how to get it in a fast way, please tell me.

        int a;
        int r ; 
        int g ; 
        int b ;

            for(int y = 0; y < bufImage2.getHeight(); y++) {
                for(int x = 0 ; x < bufImage2.getWidth(); x++){
                    color = new Color(bufImage2.getRGB(x, y));
                    a = color.getAlpha();
                    r = color.getRed(); 
                    g = color.getGreen(); 
                    b = color.getBlue(); 
                    System.out.print(r+"."+g+"."+b+":");
                    }

That's the "for" that I use to get the RGB values.

If there are libraries or something to do it more fast tell me.

Thanks.

1 Answers1

1

Based on a similar question found here, here is what i have found for you, BufferedImage has its own getRGB function, it just has to be processed to get the values at any point (pixX, pixY)

        int w = image.getWidth();
        int h = image.getHeight();

        int[] dataBuffInt = image.getRGB(0, 0, w, h, null, 0, w); 

        int pixX = 25;
        int pixY = 50;

        Color c = new Color(dataBuffInt[pixX+pixY*w]);

        System.out.println(c.getRed());   // = (dataBuffInt[100] >> 16) & 0xFF
        System.out.println(c.getGreen()); // = (dataBuffInt[100] >> 8)  & 0xFF
        System.out.println(c.getBlue());  // = (dataBuffInt[100] >> 0)  & 0xFF
        System.out.println(c.getAlpha()); // = (dataBuffInt[100] >> 24) & 0xFF  
Community
  • 1
  • 1
Kore
  • 436
  • 2
  • 14
  • But with that code I scan all the image ? Because I need all the values of all the pixels. @Kore – Denner Portuguez Nov 07 '15 at 19:21
  • @DennerPortuguez yes, you define the size of the picture you want to read as: int[] dataBuffInt = image.getRGB(0, 0, w, h, null, 0, w); 0 and 0 being start X and Y and w, h being end X and Y – Kore Nov 07 '15 at 19:26