Huge negative values extracted by using getPixel() method

孤街醉人 提交于 2019-12-10 16:43:07

问题


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.


回答1:


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.




回答2:


You can get the pixel from the view like this:

ImageView imageView = ((ImageView)v);
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
int pixel = bitmap.getPixel(x,y);

Now you can get each channel with:

int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);



回答3:


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.



来源:https://stackoverflow.com/questions/41332253/huge-negative-values-extracted-by-using-getpixel-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!