Denner Portuguez
Denner Portuguez

Reputation: 3

How to get an image matriz in a fast way? JAVA

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.

Upvotes: 0

Views: 112

Answers (1)

Kore
Kore

Reputation: 436

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  

Upvotes: 1

Related Questions