【问题标题】:Convert a raw negative rgb int value back to a 3 number rgb value将原始负 rgb int 值转换回 3 数字 rgb 值
【发布时间】:2011-07-28 10:55:27
【问题描述】:

好的,我正在开发一个程序,该程序接收图像,将像素块隔离到一个数组中,然后获取该数组中每个像素的每个单独的 rgb 值。

当我这样做时

//first pic of image
//just a test
int pix = myImage.getRGB(0,0)
System.out.println(pix);

它吐出-16106634

我需要从这个 int 值中得到 (R, G, B) 值

有公式、alg、方法吗?

【问题讨论】:

    标签: java int rgb negative-number


    【解决方案1】:

    BufferedImage.getRGB(int x, int y) 方法始终返回TYPE_INT_ARGB 颜色模型中的像素。所以你只需要为每种颜色隔离正确的位,如下所示:

    int pix = myImage.getRGB(0, 0);
    int r = (pix >> 16) & 0xFF;
    int g = (pix >> 8) & 0xFF;
    int b = pix & 0xFF;
    

    如果你碰巧想要 alpha 组件:

    int a = (pix >> 24) & 0xFF;
    

    为了方便起见,您也可以使用Color(int rgba, boolean hasalpha) 构造函数(以性能为代价)。

    【讨论】:

    • 酷。确保你在正确的方向复制了位移......实际上我第一次打错了字并在编辑中修复了它。
    【解决方案2】:
    int pix = myImage.getRGB(0,0);
    Color c = new Color(pix,true); // true for hasalpha
    int red = c.getRed();
    int green = c.getGreen();
    int blue = c.getBlue();
    

    【讨论】:

      猜你喜欢
      • 2014-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-02
      • 2019-08-28
      • 2012-10-29
      • 2011-01-24
      相关资源
      最近更新 更多