2017-09-03 66 views
0

我想把两个图像放在一起使用java。所以,我想在它的工作另一种缓冲图像的顶部绘制缓冲图像,但它破坏了图像的颜色最终图像是有点绿色 这里是我的代码:在另一个上绘制缓冲图像?

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

JPG可能乱用压缩过程中的数据 - 你可以尝试PNG作为输出格式。

为确保您拥有所有您需要的颜色,我建议您使用您需要的colordepth而不是覆盖源图像的专用目标图像。像这样:

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); 
相关问题