【发布时间】:2014-04-11 12:12:53
【问题描述】:
我正在使用一个 android 项目,在我的项目中,我想将图像的像素值存储到一个数组中,我使用了 getPixels() 函数并将其存储在一个名为像素的数组中,但是当我尝试在 TextView 中打印它,我得到了一些负值,比如 -1623534 等等。为什么会这样。 ?
这是我的代码:-
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);
我在其他一些帖子中发现了
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;
}
}
所以我将我的代码修改为:-
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);
正确吗?即使在修改后也得到负值,请帮助我,提前谢谢
【问题讨论】:
标签: android android-imageview android-image