Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.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 什么是&;0xff,同时从RGB图像中分离RGB通道?_Java_Image Processing - Fatal编程技术网

Java 什么是&;0xff,同时从RGB图像中分离RGB通道?

Java 什么是&;0xff,同时从RGB图像中分离RGB通道?,java,image-processing,Java,Image Processing,我有以下代码片段 for(int row=0; row<r; row++) { for(int col=0; col<c; col++) { alphaPixels[row][col] = ((RGBarray[ii]>>24)&0xff); redPixels[row][col] = ((RGBarray[ii]>>16)&0xff); greenPixels[row][col]

我有以下代码片段

for(int row=0; row<r; row++)
{
    for(int col=0; col<c; col++)
    {
        alphaPixels[row][col] = ((RGBarray[ii]>>24)&0xff);
        redPixels[row][col] = ((RGBarray[ii]>>16)&0xff);
        greenPixels[row][col] = ((RGBarray[ii]>>8)&0xff);
        bluePixels[row][col] = (RGBarray[ii]&0xff);
        ii++;
    }
}
for(int行=0;第24行)&0xff);
红像素[行][col]=((RGBarray[ii]>>16)和0xff);
绿色像素[row][col]=((RGBarray[ii]>>8)和0xff);
蓝色像素[行][列]=(RGBarray[ii]&0xff);
ii++;
}
}
为什么在移位操作之后必须使用按位AND操作
&0xff

为什么我们必须在 换档操作

Oxff
是十六进制数,等于十进制255

在您的情况下,
&0xff
确保所有像素值范围在0到255之间(即正8位)。例如,如果任何值大于255,则将在0-255范围内截断该值

    int value=257;
    int result = value & 0xff; // result will be 1
因此,它的工作原理类似于正值的余数运算符
%
。但是位运算符
&
比余数运算符
%
更快

    int result = value % 256; //You will get same result for positive value 

0xff
的整数值为255

>n
将数字
n
位右移,
&
运算符执行按位AND运算

因此
&0xff
屏蔽了变量。它只保留最后8位中的值,而忽略其余所有位


当您尝试将颜色值从特殊格式转换为标准RGB值(因为它有8位)时,这是一个常见的技巧。

我们可以假设RGBarray是字节[]数组吗?如果发布代码段,至少要花点时间确保该代码段中的所有内容都已正确指定给正在阅读的人。@Gimby:做出有根据的猜测。如果它不是
int
long
数组,并且由于
int
经常用于颜色,则此代码毫无意义,您可以假定它是
int[]
数组。@Fabian:Gimby可能指的是
alpha/red/green/bluePixels
数组。如果目标数组是字节,0xff屏蔽无效。
int result=value%256;//对于负值
s,您将得到相同的结果。因为移位不是无符号移位运算符,所以每个颜色通道都可能出现负值。@fabian,你说得对。对于负值,它不会给出相同的结果。