[英]PixelGrabber.getPixels returns values out of the range 0-255
[英]How to get the pixel values of an image in the range 0-255 in android
我正在尝试修改我从Android画廊获取的图像。 为了进行修改,我需要获取图像的像素值。
我不想将像素值区分为R,G,B值。 我希望像素值是一个单一值
for (x = 0; x < w; x++){
for (y = 0;y < w; y++){
int pixels = bmpimg1.getPixel(x, y);
int alphavalue=Color.alpha(pixels);
int redValue = Color.red(pixels);
int blueValue = Color.blue(pixels);
int greenValue = Color.green(pixels);
totalvalue[x][y]=(redvalue+bluevalue+greenvalue)/3
}
}
// totalvalue =假设将红色值,蓝色值,绿色值相加并取平均值,将得出该位置的像素值//
当我尝试使用totalvalue重新创建图像时,我没有得到作为输入提供的图像。
我在Java中使用Raster.getpixels进行了相同的操作,当我尝试创建相同的图像时,我能够获得相同的图像。
Bufferedimage img=ImageIO.read(new File("a.jpg");
Raster raster=img.getData();
for (int x=0;x<w;x++)
{
for(int y=0;y<h;y++)
{
pixels[x][y]=raster.getSample(x,y,0);
pixel[count++]=pixels[x][y];
}
}
当我使用带有pixel [x] [y]的bufferedImage创建新图像时,我正在获取原始图像
但是在android中,getpixel给出的值为负数
因此,如何在不分离为RGB值的情况下获取0-255范围内的每个像素的值。
请帮助我解决这个问题。 谢谢
由于所有红色,蓝色和绿色分量最多可取255,因此您不能仅仅求和以得到统一的颜色值(例如,因为1 + 2 + 3 = 2 + 3 + 1)
您将必须使用以下方法将颜色值存储在大于255的数字中
color = redValue*2^16 + greenValue*2^8 + blueValue
您应该能够弄清楚如何从该唯一值中检索红色蓝色和绿色
祝好运
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.