1

I am trying to put two images together using java. So I tried drawing a buffered image on top of another buffered image it worked but it ruined the colors of the image the final image is somewhat green Here is my code:

  try
{
BufferedImage source = ImageIO.read(new File("marker.png"));
BufferedImage logo = ImageIO.read(new File("pic.png"));

Graphics2D g = (Graphics2D) source.getGraphics();
g.drawImage(logo, 20, 50, null);
File outputfile = new File("image.jpg");
ImageIO.write(source, "jpg", outputfile);
}
catch (Exception e)
{
e.printStackTrace();
}

1 Answers1

2

jpg could mess with your data during compression - you could try png as output format.

To make sure you have all the colors you need, I suggest using a dedicated target image with the colordepth you need instead of overwriting on of your source images. Like this:

BufferedImage target = new BufferedImage(source.getWidth(), source.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = (Graphics2D) target.getGraphics();
g.drawImage(source, 0, 0, null);
g.drawImage(logo, 20, 50, null);
File outputfile = new File("targetimage.png");
ImageIO.write(target, "png", outputfile);