Skip to main content

How to Join two images

4 replies [Last post]
ganeshpol
Offline
Joined: 2006-12-21
Points: 0

i have to do following things

i have 2 images
one which is something like header (something like logo of company which will present at the top of resultant image eg nike or coke logo )
and other with actual content (eg like nike shoe image or coke colddrink image)
how should i achieve this with the help of JAi

other thing is that resultant image must be of 176X208 size
with every pixel is of 8 bit

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
chris_e_brown
Offline
Joined: 2004-09-14
Points: 0

You don't need JAI if you're using Java 1.4+. Use javax.imageio.ImageIO instead.

Load the first two buffered images with ImageIO.read() to load each image. Then create a new buffered image, as follows:

new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB)

Then get the graphics (getGraphics) of the new buffered image, and call drawImage() on this graphics object, passing in each the buffered images (header & content) to draw them at the correct position.

Then use ImageIO to write the resulting buffered image to a file or stream in the format of your choice.

- Chris

Brian Burkhalter

That is a clever idea but there is another way which doesn't even require
using graphics: just create a single BufferedImage for the final image, use

http://java.sun.com/javase/6/docs/api/java/awt/image/BufferedImage.html#getSubimage(int,%20int,%20int,%20int)

to get a sub-image for each of the two images to be read, and pass each
sub-image to the reader using

http://java.sun.com/javase/6/docs/api/javax/imageio/ImageReadParam.html#setDestination(java.awt.image.BufferedImage)

This should use half the image buffer memory.

Brian

On Thu, 21 Dec 2006, jai-interest@javadesktop.org wrote:

> You don't need JAI if you're using Java 1.4+. Use javax.imageio.ImageIO instead.
>
> Load the first two buffered images with ImageIO.read() to load each image. Then create a new buffered image, as follows:
>
> new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB)
>
> Then get the graphics (getGraphics) of the new buffered image, and call drawImage() on this graphics object, passing in each the buffered images (header & content) to draw them at the correct position.
>
> Then use ImageIO to write the resulting buffered image to a file or stream in the format of your choice.
>
> - Chris

----------------
Brian Burkhalter
Java Media, Imaging, and Graphics
Sun Microsystems, Inc.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This email message is for the sole use of the intended recipient(s)
and may contain confidential and privileged information. Any
unauthorized review, use, disclosure or distribution is prohibited.
If you are not the intended recipient, please contact the sender by
reply email and destroy all copies of the original message.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
For additional commands, e-mail: interest-help@jai.dev.java.net

sanjibjan18
Offline
Joined: 2009-04-06
Points: 0

Brian,

It will be really helpful if you can provide a small code stub illustrating your solution. I tried couple of ways but was not able to do.

michael_dekmetzian
Offline
Joined: 2009-06-10
Points: 0

Whilst this was some time ago, here's the solution. Simplified and without exception handling, it is as follows:

//start

BufferedImage mainImage = ImageIO.read(new File("/path/to/file.jpg"));

BufferedImage replaceArea = mainImage.getSubimage(startFromLeft, startFromTop, width, height);
Graphics g = replaceArea.getGraphics();

BufferedImage replaceImage = ImageIO.read(new File("/path/to/replace.jpg"));
g.drawImage(replaceImage, 0, 0, width, height, null);
g.dispose();

//end

Cheers

Michael