gpt4 book ai didi

java - 如何在android中获取0-255范围内图像的像素值

转载 作者:行者123 更新时间:2023-11-30 03:02:24 24 4
gpt4 key购买 nike

我正在尝试修改从 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=假设将红值、蓝值、绿值相加并取平均值会得到该位置的像素值//

当我尝试使用总值重新创建图像时,我没有得到我作为输入提供的图像。

我在 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];
}
}

当我使用带像素 [x][y] 的 bufferedImage 创建新图像时,我得到的是原始图像

但是在android中,getpixel给出的是负数

那么如何在不分离成 RGB 值的情况下获取 0-255 范围内每个像素的值。

请帮我解决这个问题。谢谢

最佳答案

由于所有红色、蓝色和绿色分量最多可以取 255 个值,因此您不能将它们相加得到一个单一的颜色值(例如 1+2+3 = 2+3+1)

您必须使用该方法将颜色值存储在大于 255 的数字中

color = redValue*2^16 + greenValue*2^8 + blueValue

您应该能够弄清楚如何从这个唯一值中检索红色蓝色和绿色

祝你好运

关于java - 如何在android中获取0-255范围内图像的像素值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22343890/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com