Reputation: 9
I am using the below code to merge two PNGs together although I get a syntax error on both the lines which start with g.drawImage. This is coming from an example at Merging two images but I can not comment on it because I just signed up here.
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"));
}
Thanks
EDIT
I got further with the help so far by making a method and exceptions. It now compiles and runs although it does not create the new png file. I feel like there are exceptions thrown which stops the program from doing what it should.
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
Upvotes: 0
Views: 231
Reputation: 271735
You have this error because you should write statements in a method, not a class. You see, you created a class, and you write statements immediately in the class. You should write the statements in a method and some statements throw an exception so you should add throw IOException
, like this;
public static void mergeImage (String p_basePath, String p_image, String p_overlay) throws IOException {
File path = new File(p_basePath); // base path of the images
// load source images
BufferedImage image = ImageIO.read(new File(path, p_image));
BufferedImage overlay = ImageIO.read(new File(path, p_overlay));
// 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"));
}
Alternatively you can add parameters:
public static void mergeImage () 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"));
}
Next time, just remember, always write statements in methods or constructors and be aware of the possible exceptions that some methods will throw.
Upvotes: 1
Reputation: 552
The code syntax looks totally fine I don't understand why it's giving you errors, what is exactly the error that you get? P.S: I'll run the code on my pc and I'll follow up soon with an edit
EDIT: Ok so there were 2 problems,
ImageIO.write()
and ImageIO.read()
functions with a try/catch blockhere is what works for me: package imageEditor;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImageEditor15092703{
public ImageEditor15092703() 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"));
}
}
as I said this code code should work but is NOT better practice use the alternatives that I suggested above
Upvotes: 0