TomRed.net

  • Increase font size
  • Default font size
  • Decrease font size
Subscribe Bookmark and Share
Home Tutorials Java Java import, layer and output images

Java import, layer and output images

PDF
User Rating: / 1
PoorBest 

A recent project required me to accept an image from a user upload and to overlay the customer logo, before saving the file.  This wasn't a watermarking exercise but placing a voucher code on the image.  That said it could easily be used for watermarking, if you are using a format capable of handling transparency.
In this example I will outline the loading images from a file and merging them before saving them to a file again.  I won't cover handling the file upload, that is for another day.

For this example we will use the following imports:

import javax.imageio.ImageIO;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

At first sight this might seem like a messy or difficult process, but essentially this is simple. I have outlined the process below.

BufferedImage background = ImageIO.read( new File( "background.jpg" ) );
BufferedImage foreground = ImageIO.read( new File( "foreground.png" ) );

Graphics bgg = background.getGraphics();
bgg.drawImage(foreground.getScaledInstance(foreground.getWidth(), foreground.getHeight(), java.awt.Image.SCALE_DEFAULT), 0, 0, null);

ImageIO.write(background, "png", new File ( "output.png" ));

Firstly we create two BufferedImages (or as many as are required to accomplish your result). I have called them background and foreground. I will place foreground on top of background.  To achieve this I retrieve a reference to background BufferedImage graphics object.  Graphics allows you to manipulate the image.    To overlay the foreground we simple convert the BufferedImage to an Image object using the getScaledInstance() method.  This method allows us to return a manipulated version of a BufferedImage as a type Image.  Since we only want it as it we enter the full width and height and set the hint to scale default (which in this case makes no change).  Along with our Image object we pass the 0 for both the x & y coordinates as we want the image to start in the top left corner.  Finally we add null this is instead of an ImageObserver.  As you can see we can go much further then we have in this case, which literally overlays the images;  you can layer images scale, scew, rotate, sharpen and blur the images*.  Since my example is using images with transparency we are going to output as a PNG file which can handle transparency.  Also note that my files loaded are of different types JPG and PNG are merged and saved as PNG.  The output can be out of a range, file types.  To write it out we use ImageIO.write passing the BufferedImage object, the format name and the file to save it to.  In the example I saved it as output.png and used new File() so a new file is created.  If this file exist it will be overwritten!!

* As a side note we are using the simple Java 2D API in this example.

For completeness you don't have to load image from file system. You can also load images from a URL or from raw bytes.
Loading an Image from Raw Bytes

BufferedImage image = ImageIO.read ( new ByteArrayInputStream ( rawImageBytes ) );

Loading a Image from an URL

BufferedImage image = ImageIO.read( url );

I hope you find this useful if you have any questions drop a comment below.