繁体   English   中英

“ int pix =(0xff&((int)first [ij]))”是什么? “ 意思?

[英]what does “ int pix = (0xff & ((int) first[ij])); ” mean?

以下代码是工作相机应用程序的一部分,该应用程序可检测运动并保存预览,将运动部件涂成红色。

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;
            }
        }
    }

我想充分理解这一点,以便能够进行修改。

真正令我困惑的是:

 int pix = (0xff & ((int) first[ij]));

它有什么作用?

谢谢

戴夫

我认为这里它尝试获取像素值,但范围是0到255,因此它使用掩码0xFF删除高于8的所有位。

但是我不明白为什么要用

            if (pix < 0) pix = 0;
            if (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

将结果移到整数内最右边的位置

0000cc00 >> 8 = 000000cc

与红色和16相似。 透明度。

因此,该行为您提供了像素的蓝色子像素的值。 此值的范围是0..255,因为这是仅有的八位值(当解释为无符号字节或按此处的说明存储在Java int中时; Java byte已签名并且不会使用该十进制表示形式,但为-128..127); 在此检查其他值是没有用的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM