Solved

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

Posted on 2009-04-14
7
627 Views
Last Modified: 2012-05-06
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.
0
Comment
Question by:DG1212
  • 4
  • 2
7 Comments
 
LVL 9

Expert Comment

by:arioh
ID: 24144357
0
 
LVL 17

Expert Comment

by:Thomas4019
ID: 24153624
Use javax.imageio.ImageIO to read and write your images.
Use each BufferedImage's Graphics2D Objects to draw the images.
0
 
LVL 17

Expert Comment

by:Thomas4019
ID: 24153626
Here is the Java documentation for Graphics2D.

http://java.sun.com/javase/6/docs/api/java/awt/Graphics2D.html
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Comment

by:DG1212
ID: 24163454
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
 
LVL 17

Accepted Solution

by:
Thomas4019 earned 125 total points
ID: 24163945
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
 
LVL 17

Expert Comment

by:Thomas4019
ID: 24163972
Here are the image files i used in my example.
Forest.jpg
Image1.jpg
Output.png
0
 

Author Closing Comment

by:DG1212
ID: 31570168
perfect. thank you!
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
arguments to jar 5 26
Why my table column Id is not passed to java object? 4 39
ejb on wildfly 5 20
printf performancy 11 43
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question