2017-09-03 96 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); 
相關問題