如何将图像的白色背景转换为透明背景?任何人都可以告诉我如何做到这一点?如何将图像转换为java中的透明图像
2
A
回答
4
从谷歌的第一个结果是这样的:
进行彩色透明 http://www.rgagnon.com/javadetails/java-0265.html
它使透明图像的蓝色部分,但我敢肯定,你可以适应,要使用白intstead
(提示:通过Color.WHITE
到makeColorTransparent
功能,而不是Color.BLUE
)
发现了一个比较完整的现代ANSW呃在这里:How to make a color transparent in a BufferedImage and save as PNG
0
这是我的解决方案。只要背景图像颜色位于左上角,该过滤器就会从任何图像中移除背景。
private static class BackgroundFilter extends RGBImageFilter{
boolean setUp = false;
int bgColor;
@Override
public int filterRGB(int x, int y, int rgb) {
int colorWOAlpha = rgb & 0xFFFFFF;
if(! setUp && x == 0 && y == 0){
bgColor = colorWOAlpha;
setUp = true;
}
else if(colorWOAlpha == bgColor)
return colorWOAlpha;
return rgb;
}
}
别处......
ImageFilter bgFilter = new BackgroundFilter();
ImageProducer ip = new FilteredImageSource(image.getSource(), bgFilter);
image = Toolkit.getDefaultToolkit().createImage(ip);
1
这种方法会使背景透明。您需要传递想要修改的图像,颜色和容差。
final int color = ret.getRGB(0, 0);
final Image imageWithTransparency = makeColorTransparent(ret, new Color(color), 10);
final BufferedImage transparentImage = imageToBufferedImage(imageWithTransparency);
private static BufferedImage imageToBufferedImage(final Image image) {
final BufferedImage bufferedImage =
new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
final Graphics2D g2 = bufferedImage.createGraphics();
g2.drawImage(image, 0, 0, null);
g2.dispose();
return bufferedImage;
}
private static Image makeColorTransparent(final BufferedImage im, final Color color, int tolerance) {
int temp = 0;
if (tolerance < 0 || tolerance > 100) {
System.err.println("The tolerance is a percentage, so the value has to be between 0 and 100.");
temp = 0;
} else {
temp = tolerance * (0xFF000000 | 0xFF000000)/100;
}
final int toleranceRGB = Math.abs(temp);
final ImageFilter filter = new RGBImageFilter() {
// The color we are looking for (white)... Alpha bits are set to opaque
public int markerRGBFrom = (color.getRGB() | 0xFF000000) - toleranceRGB;
public int markerRGBTo = (color.getRGB() | 0xFF000000) + toleranceRGB;
public final int filterRGB(final int x, final int y, final int rgb) {
if ((rgb | 0xFF000000) >= markerRGBFrom && (rgb | 0xFF000000) <= markerRGBTo) {
// Mark the alpha bits as zero - transparent
return 0x00FFFFFF & rgb;
} else {
// Nothing to do
return rgb;
}
}
};
final ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
return Toolkit.getDefaultToolkit().createImage(ip);
}
相关问题
- 1. 将jpeg图像转换为透明PNG图像
- 2. 如何将JPEG图像转换为透明背景的PNG图像?
- 3. 如何将非调色板透明图像转换为调色板图像?
- 4. 图像处理 - 在将图像保存为jpg之前转换透明像素
- 5. 透明图像搞乱透明图像
- 6. 使用透明图像将具有alpha图像的TrueColor转换为256色
- 7. 将SWF转换为具有透明度的PNG图像
- 8. 将图像的背景转换为透明
- 9. 将透明图像复制到另一个透明图像
- 10. 如何将位图转换为图像
- 11. 如何将图像转换为图标?
- 12. 透明图像
- 13. 将svg图像转换为png透明背景
- 14. 在ImageView中的不透明图像上的透明图像
- 15. 如何将索引图像转换为C#中的RGB图像?
- 16. 如何图像刷转换为图像
- 17. 在Java中将32bpp图像转换为16bpp图像
- 18. 将.png图像转换为.gif图像
- 19. 将图像转换为灰度图像
- 20. 将PIL图像转换为VIPS图像
- 21. 如何在PHP中将图像转换为黑白图像
- 22. Java图像转换为RGB565
- 23. EaselJS透视图像转换
- 24. 如何将位图图像转换为CIELab图像?
- 25. 如何让html中的图像透明?
- 26. 如何使用透明图像作为透明winform的背景?
- 27. 将GDCM图像转换为Java BufferedImage
- 28. Java - 将图像转换为Base64
- 29. 如何在Java中将16位.tiff图像转换为8位.tiff图像?
- 30. 在ASP.net中反转图像透明度
+1为提示。我们不想混淆SWDeveloper。 – 2010-06-23 12:31:36
嗨,蒂姆,感谢您的答复,我已经尝试用白色代替蓝色的代码,但我没有得到一个透明的图像,图像的背景颜色充满了黑色。仅供参考 - 我没有使用小程序,而是使用了“ImageIO.write”,并将其写为image.png,并尝试使用gif。 – SWDeveloper 2010-06-23 13:18:32
发现这个以及http://stackoverflow.com/questions/665406/how-to-make-a-color-transparent-in-a-bufferedimage-and-save-as-png – 2010-06-23 13:32:25