I'd like to overlay two images using the SRC_OVER Java 2D

I am taking in two images .jpg and .png and I'd like rotate one and overlay it onto the other using SRC_OVER available in Java 2D. Then output this new image as a .png. It would be nice to see some pseudo code as I am not familiar with Java 2D.
DG1212Asked:
Who is Participating?
 
Thomas4019Commented:
Here is an example i wrote of what you want.
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
 
class Example
{
	public static void main(String args[]) throws Exception
	{
		BufferedImage canvas = ImageIO.read(new File("Image1.jpg"));
		BufferedImage overlay = ImageIO.read(new File("Forest.jpg"));
 
		Graphics2D g = canvas.createGraphics();
 
		g.translate(overlay.getHeight()+(canvas.getWidth()-overlay.getHeight())/2,0);
		g.rotate(Math.toRadians(90));
		g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,.6f));
 
		g.drawImage(overlay,0,0,null);
 
		ImageIO.write(canvas,"PNG", new File("Output.png"));
	}
}

Open in new window

0
 
Thomas4019Commented:
Use javax.imageio.ImageIO to read and write your images.
Use each BufferedImage's Graphics2D Objects to draw the images.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Thomas4019Commented:
Here is the Java documentation for Graphics2D.

http://java.sun.com/javase/6/docs/api/java/awt/Graphics2D.html
0
 
DG1212Author Commented:
Yeah I found that SRC_OVER example and it paints objects. I'd like an example of bringing in image files and using it.
0
 
Thomas4019Commented:
Here are the image files i used in my example.
Forest.jpg
Image1.jpg
Output.png
0
 
DG1212Author Commented:
perfect. thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.