我正在尝试将两个图像合并为 JPEG 图像上的 PNG/TIFF 文件。我正在使用以下代码
try {
image = ImageIO.read(new File("C:\\Users\\user\\Desktop\\test\\a.jpg"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedImage overlay = null;
try {
overlay = ImageIO.read(new File("C:\\Users\\user\\Desktop\\test\\b.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// create the new image, canvas size is the max. of both image sizes
int w = Math.max(image.getWidth(), overlay.getWidth());
int h = Math.max(image.getHeight(), overlay.getHeight());
BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
// paint both images, preserving the alpha channels
Graphics2D g = combined.createGraphics();
/**Set Antialias Rendering**/
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
/**
* Draw background image at location (0,0)
* You can change the (x,y) value as required
*/
g.drawImage(image, 0, 0, null);
/**
* Draw foreground image at location (0,0)
* Change (x,y) value as required.
*/
g.drawImage(overlay, 0, 0, null);
g.dispose();
// Save as new image
try {
ImageIO.write(combined, "JPG", new File("C:\\Users\\user\\Desktop\\test\\c.jpg"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
但是,最终形成的图像并不是我想要的。为清楚起见,请参阅所附图像。