Huge negative values extracted by using getPixel() method

1.3k views Asked by At

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.

2

There are 2 answers

0
Eduardo On BEST ANSWER

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.

0
ucMedia On

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.