androidimagecolorspixelgetpixel

Huge negative values extracted by using getPixel() method


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.


Solution

  • 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.