How to read a pixel RGB colors from a given image?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Leandro_Miranda
Posted On:   Friday, May 9, 2003 12:56 PM

Hi Folks, I'm having a hard time trying to read a pixel RGB colors from a given images. Here is my code: ... try {   File file = new File(local);   BufferedImage image =     javax.imageio.ImageIO.read(file.toURL());   ColorModel cm = image.getColorModel();   int w = image.getWidth();   int h = image.getHeight();   int[] rgbs = new int[w*h];   int[] values = image.getRGB(0,0,w,h,rgbs,0,w); } catch (MalformedURLException mur) { System.err.println("Bad filename"); } catch (IOException ioe) { System.err.println("Error    More>>

Hi Folks,



I'm having a hard time trying to read a pixel RGB colors from a given images. Here is my code:



...

try {

  File file = new File(local);

  BufferedImage image =

    javax.imageio.ImageIO.read(file.toURL());

  ColorModel cm = image.getColorModel();



  int w = image.getWidth();

  int h = image.getHeight();

  int[] rgbs = new int[w*h];

  int[] values = image.getRGB(0,0,w,h,rgbs,0,w);



} catch (MalformedURLException mur) {

System.err.println("Bad filename");

} catch (IOException ioe) {

System.err.println("Error reading file");

}

...



The problem is the returned values like "-16878820" , and I wold like to have the RGB values like "255,120,0" .



Can some one help-me with it?
   <<Less

Re: How to read a pixel RGB colors from a given image?

Posted By:   Bahman_Barzideh  
Posted On:   Friday, May 9, 2003 02:04 PM

Remember that the integer number you are getting is in fact not a
number but three (four?) integer bytes <(A?,) R, G, B> put next to each other.



You have the correct code. If you want to get the R, G, and B values,
you will need to take the returned integer and decode it. Something
like the following:


int r = (rgb >> 16) & 0xff;
int g = (rgb >> 8) & 0xff;
int b = (rgb >> 0) & 0xff;


where rgb is the value you are getting out the image
About | Sitemap | Contact