我正在使用下面的代碼將兩個PNG合併在一起,儘管我在兩個以g.drawImage開頭的行上都遇到了語法錯誤。這是來自Merging two images的一個例子,但我不能評論它,因爲我剛在這裏註冊。使用圖形的語法錯誤
package imageEditor;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class ImageEditor15092703 {
File path = new File("C:/Users/Colton/Desktop/JavaImageEditor/"); // base path of the images
// load source images
BufferedImage image = ImageIO.read(new File(path, "image.png"));
BufferedImage overlay = ImageIO.read(new File(path, "overlay.png"));
// 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
Graphics g = combined.getGraphics();
g.drawImage(image, 0, 0, null);
g.drawImage(overlay, 0, 0, null);
// Save as new image
ImageIO.write(combined, "PNG", new File(path, "combined.png"));
}
感謝
編輯
我的幫助下進一步得到了迄今爲止製作方法和異常。它現在編譯並運行,雖然它不創建新的PNG文件。我覺得有拋出的異常會阻止程序做它應該做的事情。
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImageEditor15092705{
public void ImageEditor15092705() throws IOException{
File path = new File("C:/Users/Colton/Desktop/JavaImageEditor/"); // base path of the images
// load source images
BufferedImage image = ImageIO.read(new File(path, "image.png"));
BufferedImage overlay = ImageIO.read(new File(path, "overlay.png"));
// 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
Graphics g = combined.getGraphics();
g.drawImage(image, 0, 0, null);
g.drawImage(overlay, 0, 0, null);
// Save as new image
ImageIO.write(combined, "PNG", new File(path, "combined.png"));
}
public static void main (String[] args)
{
ImageEditor15092705 foo = new ImageEditor15092705();
}//end main
} //end image editor class
您應該將錯誤消息添加到您的帖子。 – Renzo