我正在使用下面的代码将两个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