4

I am having a problem with an image processing app I am developing (newbie here). I am trying to extract the value of specific pixels by using the getPixel() method.

I am having a problem though. The number I get from this method is a huge negative number, something like -1298383. Is this normal? How can I fix it?

Thanks.

Joshua
  • 40,822
  • 8
  • 72
  • 132
max_tech91
  • 47
  • 5

2 Answers2

2

I'm not an expert, but to me it looks like you are getting the hexadecimal value. Perhaps you want something more understandable like the value of each RGB layer.

To unpack a pixel into its RGB values you should do something like:

private short[][] red;
private short[][] green;
private short[][] blue;

 /** 
 * Map each intensity of an RGB colour into its respective colour channel
 */
private void unpackPixel(int pixel, int row, int col) {
    red[row][col] = (short) ((pixel >> 16) & 0xFF);
    green[row][col] = (short) ((pixel >> 8) & 0xFF);
    blue[row][col] = (short) ((pixel >> 0) & 0xFF);
}

And after changes in each channel you can pack the pixel back.

/** 
 * Create an RGB colour pixel.
 */
private int packPixel(int red, int green, int blue) {
    return (red << 16) | (green << 8) | blue;
}

Sorry if it is not what you are looking for.

Eduardo
  • 277
  • 1
  • 6
  • 17
0

getPixel() returns the Color at the specified location. Throws an exception if x or y are out of bounds (negative or >= to the width or height respectively).

The returned color is a non-premultiplied ARGB value.

Charuක
  • 12,953
  • 5
  • 50
  • 88
ucMedia
  • 1
  • 1