Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在按位提取后从android中的彩色图像返回灰度图像_Java_Android - Fatal编程技术网

Java 在按位提取后从android中的彩色图像返回灰度图像

Java 在按位提取后从android中的彩色图像返回灰度图像,java,android,Java,Android,下面的代码显示了我代码的一部分。 当我删除“//开始颜色提取和合成”之间的部分时 和“//结束颜色提取和合成”我总是得到相同颜色的图像 在tmpBmp中。但是如果我插入零件,那么我总是得到一个灰度图像 显示(而我期望的是相同颜色的图像)。 “//开始颜色提取和合成”和“//结束颜色提取和合成”之间的代码返回灰度图像是否有任何原因 int width = captureBmp.getWidth(); int height = captureBmp.getHeight(); int red, gre

下面的代码显示了我代码的一部分。 当我删除“//开始颜色提取和合成”之间的部分时 和“//结束颜色提取和合成”我总是得到相同颜色的图像 在tmpBmp中。但是如果我插入零件,那么我总是得到一个灰度图像 显示(而我期望的是相同颜色的图像)。 “//开始颜色提取和合成”和“//结束颜色提取和合成”之间的代码返回灰度图像是否有任何原因

int width = captureBmp.getWidth();
int height = captureBmp.getHeight();
int red, green, blue, alpha;

Bitmap tmpBmp = captureBmp.copy(Bitmap.Config.ARGB_8888, true);

for (int y = 0; y < height; y++) {
  for (int x = 0; x < width; x++) {
     int value = captureBmp.getPixel(x, y);

 //  start of color extraction & composing
   alpha = value & 0xFF000000 >> 24;         
       red = value & 0x00FF0000 >> 16;
   green = value & 0x0000FF00 >> 8;
   blue = value & 0x000000FF;

   value = ( (alpha << 24) & 0xFF000000) |
          ( (red << 16) & 0x00FF0000) |
          ((green << 8) & 0x0000FF00) |
      (blue & 0x000000FF);
   //  end of color extraction & composing


              tmpBmp.setPixel(x, y, value);
                }
            }
ImageView imgView = (ImageView) findViewById(R.id.imageview);
imgView.setImageBitmap(tmpBmp);
int-width=captureBmp.getWidth();
int height=captureBmp.getHeight();
int红色、绿色、蓝色、阿尔法色;
位图tmpBmp=captureBmp.copy(Bitmap.Config.ARGB_8888,true);
对于(int y=0;y>24;
红色=值&0x00FF0000>>16;
绿色=值&0x0000FF00>>8;
蓝色=值&0x000000FF;

value=((alpha问题在于此代码:

alpha = value & 0xFF000000 >> 24;         
       red = value & 0x00FF0000 >> 16;
   green = value & 0x0000FF00 >> 8;
   blue = value & 0x000000FF;
先用位掩码屏蔽,然后用移位,尝试相反的方法。然后用
0xFF
移位位掩码

它可能与字节格式、无符号整数和

正如前面提到的,运算符优先级起着主要作用。

red = value & 0x00FF0000 >> 16

red = value & (0x00FF0000 >> 16)
请注意,
(0x00FF0000>>16)
只是
0x000000FF
。绿色和蓝色的
也会发生同样的情况,因此您会得到三次相同的值。因此它是灰色的

alpha值不同,因为签名右移的工作方式决定了
0xFF000000>>24
0xffffff

解决方案是重拍(我也会使用
>
来解决这个问题,但实际上没有什么区别,因为
alpha
会被移回左24位)


实际的
alpha
red
green
blue
值是什么?它们对应于ARGB格式的彩色像素的alpha、red、green和blue分量。按照下面的回答修复代码后,我得到了相应的分量。我知道,但我问的是它们的实际值是什么。没关系,它是l看起来你的问题已经解决了。非常感谢Nikos M。你的回答帮助我找到了错误。事实上,错误是我先移位,然后掩盖。当我按如下方式更改代码时,代码按我预期的方式工作。我从你的回答中得到了提示:alpha=(值&0xFF000000)>>24;红色=(值&0x00FF0000)>>16;绿色=(值&0x0000FF00)>>8;blue=value&0x000000FF;@SukhoLee,很高兴听到你这么说,如果这对你的回答有帮助的话,你也可以接受。这是正确的答案。这样更正后,它会起作用。
alpha = (value & 0xFF000000) >>> 24;
red = (value & 0x00FF0000) >>> 16;
green = (value & 0x0000FF00) >>> 8;
blue = value & 0x000000FF;