【问题标题】:Error while getting pixel values from an Image从图像获取像素值时出错
【发布时间】: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


    【解决方案1】:

    您得到负值的原因如下: 图像的每个像素包含 4 个值(红色、绿色、蓝色、alpha)。每个值有 8 位(一个字节)。所有 4 个加起来正好是 32 位,这是一个整数值的大小。但是当您打印(有符号)整数时,第一位被解释为符号标志,因此如果该位设置为 1(当第一个通道 >= 128 时发生),您可以获得负值。

    要从像素中获取 RGB 值,我通常使用这个:

    int pixel = bmp.getPixel(x, y);
    int red = Color.red(pixel);
    int green = Color.green(pixel);
    int blue = Color.blue(pixel);
    

    【讨论】:

    • 我根本不想拥有 RGB 值,我只需要项目中的像素值
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-15
    • 2017-06-14
    • 1970-01-01
    • 2013-07-21
    相关资源
    最近更新 更多