我只想在一個畫布上放置兩個不同的影像,并在 Java 中將其設為 .jpg 檔案。我只想制作一個結果檔案,而不是 GUI。


我想用上面的兩個影像制作一個如下所示的結果檔案:

uj5u.com熱心網友回復:
您可以使用BufferedImage組合兩個影像。下面的代碼展示了一個簡單的實作:
public static void combineImages(String imagePath1, String imagePath2, String outputPath) throws IOException {
int intervalWidth = 20; // The interval between two images
BufferedImage image1 = ImageIO.read(new File(imagePath1));
BufferedImage image2 = ImageIO.read(new File(imagePath2));
int combinedWidth = image1.getWidth() image2.getWidth() intervalWidth;
int combinedHeight = Math.max(image1.getHeight(), image2.getHeight());
BufferedImage combined = new BufferedImage(combinedWidth, combinedHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g = combined.createGraphics();
g.setColor(Color.WHITE);
// Fill the background with white
g.fillRect(0, 0, combinedWidth, combinedHeight);
// Draw the two images on the combined image
g.drawImage(image1, 0, 0, null);
g.drawImage(image2, image1.getWidth() intervalWidth, 0, null);
ImageIO.write(combined, "jpg", new File(outputPath));
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/454974.html
上一篇:顫動按鈕以在其上方顯示影像
