Save the resultant Image as JPEG
Hi all,
I have a question regarding image saving. Such as
- I have two images like img1 and img2
- These two images are in jpeg format.
- I write code to add this two images and display it
- Now I want to save this image as img3.jpeg so that I
can use it and open it using different editor.
Please suggest me the solution.
Thank you.
I write code to add this two images and display it
What does "add the images" mean? Draw one on top of or inside the other, draw them side-by-side or one to the north and the other to the south?
Let's assume you will draw them side-by-side. Then you could try (pseudo-code):
int w = image1.getWidth() + image2.getWidth();
int h = Math.max(image1.getHeight(), image2.getHeight());
BufferedImage bi = new BufferedImage(w, h, image1.getType());
Graphics2D g2 = bi.createGraphics();
// if images are not same height you may want to fill the background
g2.setPaint(myBackgroundColor);
g2.fillRect(0,0,w,h);
g2.drawImage(image1, 0, 0, null);
g2.drawImage(image2, image1.getWidth(), 0, null);
g2.dispose();
// save it
File file = new File("myImage.jpg");
try {
ImageIO.write(bi, "jpg", file);
} catch(IOException ioe) {