[英]Error while getting pixel values from an Image
am working with an android project , in my project , i want to store the pixel values of an image into an array, i used getPixels() function for it and store it in an array named as pixels, but when i tried to print it in a TextView , am getting some negetive values sometinh like -1623534 and so on . 正在使用android项目,在我的项目中,我想将图像的像素值存储到数组中,为此我使用了getPixels()函数并将其存储在名为pixel的数组中,但是当我尝试打印它时在TextView中,正在获取一些可取的值,如-1623534等。 Why it is like that.
为什么会这样。 ?
?
Here is my code :- 这是我的代码:-
Bitmap result = BitmapFactory.decodeFile(filePath);
TextView resultText=(TextView)findViewById(R.id.txtResult);
try
{
int pich=(int)result.getHeight();
int picw=(int)result.getWidth();
int[] pixels = new int[pich*picw];
result.getPixels(pixels, 0, picw, 0, 0, picw, pich);
//To convert into String
StringBuffer buff = new StringBuffer();
for (int i = 0; i <100; i++)
{
// getting values from array
buff.append(pixels[i]).append(" ");
}
//To save the binary in newString
String newString=new String(buff.toString());
resultText.setText(newString);
And i found in some other post like 我发现其他一些帖子
int R, G, B;
for (int y = 0; y < pich; y++)
{
for (int x = 0; x < picw; x++)
{
int index = y * picw + x;
R = (pixels[index] >> 16) & 0xff; //bitwise shifting
G = (pixels[index] >> 8) & 0xff;
B = pixels[index] & 0xff;
//R,G.B - Red, Green, Blue
//to restore the values after RGB modification, use
//next statement
pixels[index] = 0xff000000 | (R << 16) | (G << 8) | B;
}
}
So i modified my code as :- 所以我将代码修改为:-
Bitmap result = BitmapFactory.decodeFile(filePath);
TextView resultText=(TextView)findViewById(R.id.txtResult);
try
{
int pich=(int)result.getHeight();
int picw=(int)result.getWidth();
int[] pixels = new int[pich*picw];
result.getPixels(pixels, 0, picw, 0, 0, picw, pich);
int R, G, B;
for (int y = 0; y < pich; y++)
{
for (int x = 0; x < picw; x++)
{
int index = y * picw + x;
R = (pixels[index] >> 16) & 0xff; //bitwise shifting
G = (pixels[index] >> 8) & 0xff;
B = pixels[index] & 0xff;
//R,G.B - Red, Green, Blue
//to restore the values after RGB modification, use
//next statement
pixels[index] = 0xff000000 | (R << 16) | (G << 8) | B;
}
}
//To convert into String
StringBuffer buff = new StringBuffer();
for (int i = 0; i <100; i++)
{
// getting values from array
buff.append(pixels[i]).append(" ");
}
//To save the binary in newString
String newString=new String(buff.toString());
resultText.setText(newString);
Is it correct ? 这是正确的吗 ? Am getting the negetive values even after the modification , pleasehelp me , Thanks in advance
修改后仍在获取有益的价值,请帮帮我,谢谢
The reason why you are getting negative values is the following: Each pixle of an image contains of 4 values(red, green, blue, alpha). 获得负值的原因如下:图像的每个像素包含4个值(红色,绿色,蓝色,alpha)。 Each value has 8 bit (one byte).
每个值都有8位(一个字节)。 All 4 together have exactly 32 bit, which is the size of an integer value.
所有4个都恰好具有32位,这是整数值的大小。 But when you print a (signed-)integer, the first bit is interpreted as sign-flag, so you can get negative values, if this bit is set to 1 (happens when the first channel is >= 128).
但是,当您打印(有符号)整数时,第一位将被解释为符号标志,因此,如果将此位设置为1,则可能会得到负值(在第一个通道> = 128时发生)。
To get the RGB values from a pixel I normally use this: 为了从像素获取RGB值,我通常使用以下方法:
int pixel = bmp.getPixel(x, y);
int red = Color.red(pixel);
int green = Color.green(pixel);
int blue = Color.blue(pixel);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.