Search code examples
javaimagepngtransparencygif

Image handling in Java


I'm thinking about the best way to do two things in Java:

  1. merge two images, the background one in .PNG and the other in .GIF or .PNG (has transparency and is to overlap the first one);
  2. convert the merged image to .GIF (with transparency).

I don't want to render them, just to handle the images in the java class and write the resultant image to a file.

Can anyone help me? What's the best way to do this? Thank you!

EDIT: Thank you all for the suggestions! This was what I ended up using! Pretty simple!

BufferedImage background = ImageIO.read(new File("image.jpg"));
WritableRaster raster = background.getRaster();
BufferedImage layer = ImageIO.read(new File("overlay.png"));
Graphics2D g2d = (Graphics2D)background.getGraphics();
g2d.drawImage(layer,72,80,null);

About the second problem, I still can't save this with .gif extension with transparency. This

ImageIO.write(bufferedImage,"gif",file);

creates the .gif image file but it loses the transparency! Does anyone know how can I do this? JAI also doesn't have the gif encoder. Thank you.


Solution

  • Not sure of your application, but if this is server-based high performance stuff, I've had much better results shelling out to ImageMagick than using Java's image libraries.