简体   繁体   English

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

[英]How to get the pixel values of an image in the range 0-255 in android

I am trying to modify the image which I take it from the gallery in android. 我正在尝试修改我从Android画廊获取的图像。 For modifying I need to get the pixel value of the image. 为了进行修改,我需要获取图像的像素值。

I do not want to differentiate the pixel value into R,G,B value. 我不想将像素值区分为R,G,B值。 I want the pixel value as a single value 我希望像素值是一个单一值

                              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=Assuming addition of redvalue, bluevalue,greenvalue and taking average will give me the pixel value in that location// // totalvalue =假设将红色值,蓝色值,绿色值相加并取平均值,将得出该位置的像素值//

When I try to recreate the image using the totalvalue, I am not getting the image which i gave it as input. 当我尝试使用totalvalue重新创建图像时,我没有得到作为输入提供的图像。

I did the same in java using Raster.getpixels, when I tried to create the same image I am able to get the same image. 我在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];
            }
        }

When I create a new image using bufferedImage with the pixel[x][y], I am getting the original image 当我使用带有pixel [x] [y]的bufferedImage创建新图像时,我正在获取原始图像

But in android, getpixel gives values in negative number 但是在android中,getpixel给出的值为负数

So how can i get the values of each pixel in the range 0-255 without separating into RGB value. 因此,如何在不分离为RGB值的情况下获取0-255范围内的每个像素的值。

Kindly help me with this problem. 请帮助我解决这个问题。 Thank you 谢谢

Since all red, blue and green component can take values up to 255, you can't just sum them to get a unic color value (since 1+2+3 = 2+3+1 for example) 由于所有红色,蓝色和绿色分量最多可取255,因此您不能仅仅求和以得到统一的颜色值(例如,因为1 + 2 + 3 = 2 + 3 + 1)

you will have to store the color value in a number bigger than 255 with the method 您将必须使用以下方法将颜色值存储在大于255的数字中

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

You should be able to figure out how to retrieve red blue and green from this unique value 您应该能够弄清楚如何从该唯一值中检索红色蓝色和绿色

Good luck 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM