【问题标题】:Applying alpha paths to images in java在java中将alpha路径应用于图像
【发布时间】:2020-11-05 09:36:09
【问题描述】:

我想应用图像数据中的 alpha 蒙版。我有 不同格式的图像,即 tiffs、PSD PNG 和 jpeg。我是 将它们作为缓冲图像读取,并希望使用 Twelvemonkeys 库 获取图像中配置的 alpha 路径,并相应地应用透明度。但我找不到 相关功能。请帮忙。

ImageInputStream stream = ImageIO.createImageInputStream(new File(c:/img.psd);
BufferedImage image = Paths.readClipped(stream);
image.getcoclormodel().hasAlpha(); 

for(i < image.getwidth()) {
   for(j < image.getHeight()) {
       pixels = image.getRGB(i, j, width, height, null, 0, width);
       Color col = new Color(pixels[pixelIndex]);
       int p = col.getAlpha() 
       image.setRGB(i, j, width, height, p, 0, width)
    }
}

【问题讨论】:

  • 嗨,有趣,也许这可能会引起您的兴趣stackoverflow.com/questions/221830/…
  • 我还想要在 photoshop 图像和其他 tiff 图像中配置的 alpha 通道,然后我想应用它们来获取新的缓冲图像

标签: java alpha javax.imageio twelvemonkeys


【解决方案1】:

使用Paths.readClipped(...) 方法将读取图像数据和Photoshop 剪切路径资源,并将剪切路径应用于图像。换句话说,生成的BufferedImage 将根据路径包含透明度。

如果您喜欢分别读取路径和图像,可以使用Paths 实用程序类的readPath(...)applyClippingPath(...) 方法:

try (ImageInputStream stream = ImageIO.createImageInputStream(new File("image_with_path.jpg")) {
    Shape clip = Paths.readPath(stream);
    stream.seek(0);
    BufferedImage image = ImageIO.read(stream);

    if (clip != null) {
        image = Paths.applyClippingPath(clip, image);
    }

    // Do something with the clipped image...
}

上面的代码基本上和readClipped(...)做同样的事情,但允许你检查和修改每一步。

PS:请注意,Paths 中的方法仅适用于包含具有 ClippingPath 资源(资源 id 0x07d0)的 Photoshop 资源块的图像。图像的 Alpha 通道将始终存在。对于包含多个 Alpha 通道(也称为“透明蒙版”)的 PSD 和 TIFF 文件,目前无法访问额外的 Alpha 通道。

【讨论】:

    猜你喜欢
    • 2015-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多