Java 什么是;int-pix=(0xff&;((int)first[ij])&引用;什么意思?
以下代码是一个正在工作的摄像头应用程序的一部分,该应用程序检测运动并保存预览,将运动部件涂成红色Java 什么是;int-pix=(0xff&;((int)first[ij])&引用;什么意思?,java,android,Java,Android,以下代码是一个正在工作的摄像头应用程序的一部分,该应用程序检测运动并保存预览,将运动部件涂成红色 public class RgbMotionDetection implements IMotionDetection { that detects motion private static final int mPixelThreshold = 50; // Difference in pixel (RGB) private static final int mThreshold
public class RgbMotionDetection implements IMotionDetection {
that detects motion
private static final int mPixelThreshold = 50; // Difference in pixel (RGB)
private static final int mThreshold = 10000; // Number of different pixels
private static int[] mPrevious = null;
private static int mPreviousWidth = 0;
private static int mPreviousHeight = 0;
/**
* {@inheritDoc}
*/
@Override
public int[] getPrevious() {
return ((mPrevious != null) ? mPrevious.clone() : null);
}
protected static boolean isDifferent(int[] first, int width, int height) {
if (first == null) throw new NullPointerException();
if (mPrevious == null) return false;
if (first.length != mPrevious.length) return true;
if (mPreviousWidth != width || mPreviousHeight != height) return true;
int totDifferentPixels = 0;
for (int i = 0, ij = 0; i < height; i++) {
for (int j = 0; j < width; j++, ij++) {
int pix = (0xff & ((int) first[ij]));
int otherPix = (0xff & ((int) mPrevious[ij]));
// Catch any pixels that are out of range
if (pix < 0) pix = 0;
if (pix > 255) pix = 255;
if (otherPix < 0) otherPix = 0;
if (otherPix > 255) otherPix = 255;
if (Math.abs(pix - otherPix) >= mPixelThreshold) {
totDifferentPixels++;
// Paint different pixel red
first[ij] = Color.RED;
}
}
}
它有什么作用
谢谢
Dave我认为这里它试图获取像素的值,但范围是0到255,因此它使用掩码0xFF删除高于8的所有位 但我不明白为什么它会使用
if (pix < 0) pix = 0;
if (pix > 255) pix = 255;
if(pix<0)pix=0;
如果(pix>255)pix=255;
当pix变量不能高于255时,请原谅我现在解释了一些您知道的问题,但我想让这成为一个独立的答案 像素的颜色可以存储为整数。Java中的整数由四个字节组成,颜色通常(在本上下文中)由四个字节表示:红色、绿色和蓝色各一个字节,透明度的最后一个字节。屏幕上的子像素混合会产生观察到的颜色 因此,以下整数表示一种颜色:
0 0 f f c c a a (Hexadecimal representation)
0000 0000 1111 1111 1100 1100 1010 1010 (Binary representation)
Transp.-- Red------ Green---- Blue----- (Color interpretation)
16764074 (Decimal representation, quite useless here)
在本例中,前两个字节(00)表示透明度,ff表示红色子像素,cc表示绿色,aa表示蓝色
如果我们只想得到其中的一部分,例如蓝色亚像素,我们需要一个位掩码
0000 0000 1111 1111 1100 1100 1010 1010 (Binary representation)
& (Binary AND: Return 1 if both bits are 1)
0000 0000 0000 0000 0000 0000 1111 1111 (Bitmask for blue, in hex 0x000000ff)
=
0000 0000 0000 0000 0000 0000 1010 1010
这是您提到的行中操作的结果,它只是对掩码使用简写的十六进制解释:
int pix = (0xff & ((int) first[ij]));
由于数组first
已经是一个int
数组,first[ij]
的施法是无用的
如果需要像素的另一部分,例如绿色部分,则需要移动遮罩(或使用硬编码值),并需要向后移动结果:
00ffccaa
&
0000ff00 Hardcoded, alternative: ff << 8
=
0000cc00
与红色响应的16和24相似。透明度
这条线给出了像素的蓝色子像素的值。该值的范围为0..255,因为这是8位唯一可能的值(当解释为无符号字节或存储在Java
int
中时,如此处所述;Javabyte
是有符号的,不会使用十进制表示,而是-128..127);此处对其他值的任何检查都是无效的。0xff
是十六进制整数文本&
是按位AND运算符<代码>(int)是演员阵容first[ij]
是一个数组访问表达式。int-pix=(0xff&((int)first[ij])
与intpix=(int)first[ij]相同代码>@GrijeshChauhan No。用255以上的任何值试试。@his Yes你是对的。虽然我知道大部分,但谢谢你把这些放在一起。
00ffccaa
&
0000ff00 Hardcoded, alternative: ff << 8
=
0000cc00
0000cc00 >> 8 = 000000cc