简体   繁体   English

从图像获取像素值时出错

[英]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.

相关问题 从 Android 中的 8Bit Greyscale 图像获取像素颜色值,当前获取值如 -16777216 而我想要值 0-255 - Getting Pixel color values from 8Bit Greyscale image in Android, currently getting values like -16777216 while I want values 0-255 MPAndroidChart:从条目值中获取像素值 - MPAndroidChart: Getting Pixel values from Entry values 在Android中从图库获取图像时出错 - Error while getting image from gallery in android 从图库中导入的图像获取Android中的像素颜色? - Getting Pixel Colour in Android from an imported image from gallery? 在Android中显示路径中的图像时出现错误 - getting error while displaying image from path in android 将图像从解析设置为imageview时出错 - Getting error while setting image from parse to imageview android从url显示图像时出错 - android getting error while displaying image from url 将图像从Soap Web服务获取到Android应用程序时出错 - Error while getting an image from a soap web service into an android application 渲染脚本,图像处理,从预先计算的数组分配像素值 - Renderscript, image processing, assigning pixel values from a precomputed array 上传图片时出现RuntimeException错误 - getting RuntimeException error while uploading image
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM