Note that there are some explanatory texts on larger screens.

plurals
  1. POjava background transparent
    primarykey
    data
    text
    <p>I have a gif image wich contains only a colored shape, and a transparent background</p> <p>I would like to replace the shape's color by the one I want (the color palet for this gif is only 2 colors : transparent and white in my case).</p> <p>I've created a filter wich correctly replace white with red (this is a test)</p> <p>However I'm encountering an issue with my method imageToBufferedImage, it removes the transparency and replace it with black (don't know why).</p> <p>So what I've done so far is this :</p> <pre><code>import java.awt.Color; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Toolkit; 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.File; import javax.imageio.ImageIO; public class TestPNG { public static void main(String[] args) throws Exception { File in = new File("bg.gif"); BufferedImage source = ImageIO.read(in); int color = source.getRGB(0, 0); Image image = makeColorTransparent(source, new Color(color), new Color(255, 0, 0)); BufferedImage transparent = imageToBufferedImage(image); File out = new File("bg2.gif"); ImageIO.write(transparent, "gif", out); } private static BufferedImage imageToBufferedImage(Image image) { BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = bufferedImage.createGraphics(); //g2.setBackground(Color.blue); g2.clearRect(0, 0, 200, 40); g2.drawImage(image, 0, 0, null); g2.dispose(); return bufferedImage; } public static Image makeColorTransparent(BufferedImage im, final Color search, final Color replace) { ImageFilter filter = new RGBImageFilter() { public final int filterRGB(int x, int y, int rgb) { if (rgb == search.getRGB()) { return replace.getRGB(); } else { return rgb; } } }; ImageProducer ip = new FilteredImageSource(im.getSource(), filter); return Toolkit.getDefaultToolkit().createImage(ip); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload