【问题标题】:Order of channels in BufferedImage?BufferedImage 中的通道顺序?
【发布时间】:2011-03-02 16:20:27
【问题描述】:

如何找出 BufferedImage 中颜色通道的顺序(包括 Alpha 通道在内的不同类型)?

对于 LookupOp(ByteLookupTable(int, byte[][]) 中的 byte[][] 的顺序)或 RescaleOp(float[],float[ ],提示)。

是否有一种通用的方法可以从给定的 BufferedImage 中找到订单?我认为它应该在 ColorModel 中,但我找不到它。

我用过if (t == BufferedImage.TYPE_INT_ARGB)之类的代码,但肯定有更好的方法吧?

【问题讨论】:

  • 澄清:如果我需要将 R、G 和 B 的查找表以 RGB、BRG、RGBA 甚至其他顺序放入 LookupOp 中,我正在寻找的信息。

标签: java java-2d bufferedimage


【解决方案1】:

我认为您要查找的内容分为 SampleModelColorModel

SampleModel 描述了数据的组织方式,使您可以获取一个或多个像素的数据。 (您可以通过调用 bi.getData().getSampleModel(), 获得 SampleModel,其中 bi 是 BufferedImage)。

ColorModel 然后提供方法(getAlphagetRedgetGreenGetBlue)从像素中获取 ARGB 分量。

附录:

我认为你使用它的方式是:

    BufferedImage bi = ...;
    Raster r = bi.getData();
    // Use the sample model to get the pixel
    SampleModel sm = r.getSampleModel();
    Object pixel = sm.getPixel(0, 0, (int[])null, r.getDataBuffer());
    // Use the color model to get the red value from the pixel
    ColorModel cm = bi.getColorModel();
    int red = cm.getRed(pixel[0]);

这看起来可以非常灵活地处理您可能遇到的任何颜色/样本模型,但我无法想象性能会如此出色。我可能会使用这种与模型无关的方法将图像转换为TYPE_INT_ARGB,其中布局有据可查,然后直接操作数据。然后,如有必要,将其转换回原始形式。

【讨论】:

  • SampleModel 的链接对我来说是新的,但我仍然找不到有关我必须提供的频道顺序的信息,例如查找操作。我知道我可以从 ColorModel 中获取像素的 getRed(),但是我怎么知道红色在通道的顺序中在哪里?
猜你喜欢
  • 1970-01-01
  • 2013-04-15
  • 2019-04-07
  • 1970-01-01
  • 2014-03-07
  • 2019-02-22
  • 2019-01-30
  • 2018-11-12
  • 2013-05-17
相关资源
最近更新 更多