gpt4 book ai didi

java - 如何使 BufferedImage 中的颜色透明并另存为 PNG

转载 作者:行者123 更新时间:2023-12-01 18:01:18 27 4
gpt4 key购买 nike

我一直在网上搜索这个问题,但没有找到任何像样的帮助。

我有一个 BufferedImage,我已经用 ImageIO 读入了它。现在我想将该图像中的某种颜色设置为透明,并将图像另存为 PNG。

我知道出于明显的原因我不能只是“绘制”透明颜色,所以我猜我需要某种过滤器。

有人有一些示例代码吗?

最佳答案

我最近这样做是为了回答我的项目经理的问题。
灰色转透明的函数为:

  private Image TransformGrayToTransparency(BufferedImage image)
{
ImageFilter filter = new RGBImageFilter()
{
public final int filterRGB(int x, int y, int rgb)
{
return (rgb << 8) & 0xFF000000;
}
};

ImageProducer ip = new FilteredImageSource(image.getSource(), filter);
return Toolkit.getDefaultToolkit().createImage(ip);
}

实际上,它作用于灰度图像,因此我只需将 RGB 分量(R 分量)复制到 alpha,丢弃与我的情况相同的其他分量。
您可以调整它以过滤特定颜色,例如。通过平等或范围等测试
当然,BufferedImage必须是BufferedImage.TYPE_INT_ARGB类型。

我不解决保存问题,因为它非常微不足道,但我也可以添加此代码页。

[编辑] 将图像转换为 BufferedImage:

BufferedImage dest = new BufferedImage(
imageWidth, imageHeight,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = dest.createGraphics();
g2.drawImage(image, 0, 0, null);
g2.dispose();

[编辑 2] 我是在 Christoffer 发布他的完整解决方案后来的,但这是我的,我展示了如何使一系列颜色透明。可以改进,例如。使用 HSB 组件代替。

import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.ImageProducer;
import java.awt.image.RGBImageFilter;
import java.io.*;

import javax.imageio.ImageIO;

public class AddTransparency
{
AddTransparency() throws IOException
{
String imagePath = "E:/Documents/images/";
File inFile = new File(imagePath, "map.png");
BufferedImage image = ImageIO.read(inFile);

Image transpImg1 = TransformGrayToTransparency(image);
BufferedImage resultImage1 = ImageToBufferedImage(transpImg1, image.getWidth(), image.getHeight());

File outFile1 = new File(imagePath, "map_with_transparency1.png");
ImageIO.write(resultImage1, "PNG", outFile1);

Image transpImg2 = TransformColorToTransparency(image, new Color(0, 50, 77), new Color(200, 200, 255));
BufferedImage resultImage2 = ImageToBufferedImage(transpImg2, image.getWidth(), image.getHeight());

File outFile2 = new File(imagePath, "map_with_transparency2.png");
ImageIO.write(resultImage2, "PNG", outFile2);
}

private Image TransformGrayToTransparency(BufferedImage image)
{
ImageFilter filter = new RGBImageFilter()
{
public final int filterRGB(int x, int y, int rgb)
{
return (rgb << 8) & 0xFF000000;
}
};

ImageProducer ip = new FilteredImageSource(image.getSource(), filter);
return Toolkit.getDefaultToolkit().createImage(ip);
}

private Image TransformColorToTransparency(BufferedImage image, Color c1, Color c2)
{
// Primitive test, just an example
final int r1 = c1.getRed();
final int g1 = c1.getGreen();
final int b1 = c1.getBlue();
final int r2 = c2.getRed();
final int g2 = c2.getGreen();
final int b2 = c2.getBlue();
ImageFilter filter = new RGBImageFilter()
{
public final int filterRGB(int x, int y, int rgb)
{
int r = (rgb & 0xFF0000) >> 16;
int g = (rgb & 0xFF00) >> 8;
int b = rgb & 0xFF;
if (r >= r1 && r <= r2 &&
g >= g1 && g <= g2 &&
b >= b1 && b <= b2)
{
// Set fully transparent but keep color
return rgb & 0xFFFFFF;
}
return rgb;
}
};

ImageProducer ip = new FilteredImageSource(image.getSource(), filter);
return Toolkit.getDefaultToolkit().createImage(ip);
}

private BufferedImage ImageToBufferedImage(Image image, int width, int height)
{
BufferedImage dest = new BufferedImage(
width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = dest.createGraphics();
g2.drawImage(image, 0, 0, null);
g2.dispose();
return dest;
}

public static void main(String[] args) throws IOException
{
AddTransparency at = new AddTransparency();
}
}

关于java - 如何使 BufferedImage 中的颜色透明并另存为 PNG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60621274/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com