Monday, August 10, 2009

Java: Concatenate multiple images into a single image

Let’s see how to concatenate (NOT combine) multiple images into a single image. So far I have tried with JPEGs and PNGs.
Here is the code:


public static void main(String[] args) throws IOException, Exception {
File file1 = new File("c:/BlackDragonWallpaper.jpg");
File file2 = new File("c:/black_attack_001_1024x768.jpg");
File file3 = new File("c:/windows-vista-wallpaper-120.jpg");
File file4 = new File("c:/wallpapers_windows_vista-xp-longnhor_-_redcode_0051.jpg");

BufferedImage img1 = ImageIO.read(file1);
BufferedImage img2 = ImageIO.read(file2);
BufferedImage img3 = ImageIO.read(file3);
BufferedImage img4 = ImageIO.read(file4);

int widthImg1 = img1.getWidth();
int heightImg1 = img1.getHeight();

int widthImg2 = img2.getWidth();
int heightImg2 = img2.getHeight();

BufferedImage img = new BufferedImage (
widthImg1 + widthImg2, // Final image will have width and height as
heightImg1 + heightImg2, // addition of widths and heights of the images we already have
BufferedImage.TYPE_INT_RGB);

boolean image1Drawn = img.createGraphics().drawImage(img1, 0, 0, null); // 0, 0 are the x and y positions

if(!image1Drawn) System.out.println("Problems drawing first image"); //where we are placing image1 in final image

boolean image2Drawn = img.createGraphics().drawImage(img2, widthImg1, 0, null); // here width is mentioned as width of

if(!image2Drawn) System.out.println("Problems drawing second image"); // image1 so both images will come in same level

boolean image3Drawn = img.createGraphics().drawImage(img3, 0, heightImg1, null);

if(!image3Drawn) System.out.println("Problems drawing third image");

boolean image4Drawn = img.createGraphics().drawImage(img4, widthImg1, heightImg1, null);

if(!image4Drawn) System.out.println("Problems drawing fourth image");

// horizontally
File final_image = new File("C:/Final.jpg");

boolean final_Image_drawing = ImageIO.write(img, "jpeg", final_image);

if(!final_Image_drawing) System.out.println("Problems drawing final image");

System.out.println("Successfull");
}


You can play with dimensions and the image placement positions and create final image with multiple images at different positions in it. You can put more images. I personally prefer “PNG” coz final image size will be smaller that what JPEG would have been and it is more clear.

Finding from where the java class is loaded

When programming java often times you end up in situation were whatever changes you made to the class is no reflecting when you run the code. Every time when I end up in this situation I knew that there is an old version of my class sitting some where and getting loaded in to the class path. Many of the time, you can figure out this by examining the java classpath. But most of the time the class is loaded from one of the old jar files that you don’t want to load.

To know from where the class is loaded; you can use the below code snippet


/**
* Gets the class location.
*
* @return the class location
*/
public String getClassLocation() {
String className = this.getClass().getName();
className = className.replace('.', '/');
className = '/' + className + ".class";
java.net.URL classUrl = this.getClass().getResource(className);
String arr[] = classUrl.getFile().split("!");
return arr[0].replaceFirst("file:/", "");
}

The getClassLocation function will work for any of your class and it will return the absolute path of the class file with the file name from where the class is loaded.

/E:/Projects/java/SpeedTimer/classes/com/sample/speedtimer/ClassLoader.class