如何將圖像的白色背景轉換爲透明背景?任何人都可以告訴我如何做到這一點?如何將圖像轉換爲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. pdfBox將pdf轉換爲透明圖像
- 2. 將透明PNG圖像轉換爲透明JPEG或透明GIF
- 3. 將透明度的png圖像轉換爲jpg破壞圖像
- 4. 將jpeg圖像轉換爲透明PNG圖像
- 5. 如何將JPEG圖像轉換爲透明背景的PNG圖像?
- 6. 如何將非調色板透明圖像轉換爲調色板圖像?
- 7. 如何將Ipl圖像轉換爲Java中的Mat圖像
- 8. 如何將索引圖像轉換爲Java中的RGB圖像?
- 9. 如何在BlackBerry中將圖像製作爲透明圖像?
- 10. 圖像處理 - 在將圖像保存爲jpg之前轉換透明像素
- 11. 透明圖像搞亂透明圖像
- 12. 使用透明圖像將具有alpha圖像的TrueColor轉換爲256色
- 13. Java OpenCV:如何將彩色圖像轉換爲黑白圖像?
- 14. 將圖像的背景轉換爲透明
- 15. 將SWF轉換爲具有透明度的PNG圖像
- 16. 如何將圖像轉換爲圖標而不會丟失透明度?
- 17. Java SplashScreen透明圖像
- 18. 如何將圖像轉換爲java中的圖標?
- 19. 如何將圖像轉換爲位圖
- 20. 如何將圖像轉換爲圖標?
- 21. 如何將位圖轉換爲圖像
- 22. 將透明圖像放置到另一個不透明圖像
- 23. 將透明圖像複製到另一個透明圖像
- 24. J2ME:將透明PNG圖像轉換爲灰度
- 25. 將svg圖像轉換爲png透明背景
- 26. 使用圖像magick將png轉換爲平白色和透明
- 27. 使用PHP將圖像轉換爲Alpha透明度
- 28. 透明圖像
- 29. Java OpenCV將圖像小到透明的大圖像
- 30. 在ImageView中的不透明圖像上的透明圖像
+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