Saturday, February 27, 2010

How to merge multiple images into one image - Java ImageIO

My previous post shows how to split an image into chunks. Now let's see how to merge multiple images into one image. Say we need to concatenate following four image chunks. I got these chunks by splitting the image in the right hand side, using the image splitter.

















Following code shows how to concatenate the image chunks above into one image.
int rows = 2;   //we assume the no. of rows and cols are known and each chunk has equal width and height
        int cols = 2;
        int chunks = rows * cols;

        int chunkWidth, chunkHeight;
        int type;
        //fetching image files
        File[] imgFiles = new File[chunks];
        for (int i = 0; i < chunks; i++) {
            imgFiles[i] = new File("archi" + i + ".jpg");
        }

       //creating a bufferd image array from image files
        BufferedImage[] buffImages = new BufferedImage[chunks];
        for (int i = 0; i < chunks; i++) {
            buffImages[i] = ImageIO.read(imgFiles[i]);
        }
        type = buffImages[0].getType();
        chunkWidth = buffImages[0].getWidth();
        chunkHeight = buffImages[0].getHeight();

        //Initializing the final image
        BufferedImage finalImg = new BufferedImage(chunkWidth*cols, chunkHeight*rows, type);

        int num = 0;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                finalImg.createGraphics().drawImage(buffImages[num], chunkWidth * j, chunkHeight * i, null);
                num++;
            }
        }
        System.out.println("Image concatenated.....");
        ImageIO.write(finalImg, "jpeg", new File("finalImg.jpg"));

25 comments:

HazyMind said...

This was very helpful. Thank you!

Shiney said...

hey...thankx a lot...ws helpful 4 our prjct...

Seftian said...

Thankx Kalani, this was very helpful for may final project, since I was to start my project using java.

kalani Ruwanpathirana said...

welcome...you all.

Knick said...

On line 23: shouldn't it be "chunkWidth*cols" instead of "chunkWidth*chunks" (respectively "rows")? Or not?

Anyway thanks a lot!

kalani Ruwanpathirana said...

Thanks Knick for pointing that out. (I couldn't test this code but I'll change that)

PHiL said...

Hi,

I gonna use your piece of code but I'm not sure about memory consumption because I would like to use it to merge a large amount of image... about 4000 images with a maximum size of 256x256x8bit / image. I'm not sure that Java will be able to handle that. Do you have any advice concerning my case ?

Thanks

krish indhu said...

hi...i need the java code for replacing some parts in image...i tried many times.but i cannot get any code for image parts replacing,i completed half of the project,if i got the java code for image parts replacing,i completed my project...plzzz plzzz help me

krish indhu said...

hi...i need the java code for replacing some parts in image...i tried many times.but i cannot get any code for image parts replacing,i completed half of the project,if i got the java code for image parts replacing,i completed my project...plzzz plzzz help me

Mihir Parekh said...

Hello
your post is really helpful. i tried both the splitting and merging. it has a problem of rounding. for example i split my image 574 * 574 in to 4 * 4 chunks which results in 143 * 143 sized 16 chunks and when i merge all the chunks the resultant size will be the 572 * 572 , because it does not take care of rounding mistake happens with division operation. if we make more chunks say 200 then the rounding loss 100 pixels and so on.

Satya Narayana said...

thank u very much.........

Prateek Shrivastava said...

I am getting a black image after concating the images. Any idea?

Sterling Ramroach said...

Works like a charm.

Nilesh Jain said...

sir,
Could you please tell me if we can import this into an android project ?
And if Yes How should we go about it.

ranjit kumar said...

gud afternoon madam .madam i excute that code not run in netbeens .

ranjit kumar said...
This comment has been removed by the author.
ranjit kumar said...

view plainprint?

int rows = 2; //we assume the no. of rows and cols are known and each chunk has equal width and height
int cols = 2;
int chunks = rows * cols;

int chunkWidth, chunkHeight;
int type;
//fetching image files
File[] imgFiles = new File[chunks];
for (int i = 0; i < chunks; i++) {
imgFiles[i] = new File("archi" + i + ".jpg");
}

//creating a bufferd image array from image files
BufferedImage[] buffImages = new BufferedImage[chunks];
for (int i = 0; i < chunks; i++) {
buffImages[i] = ImageIO.read(imgFiles[i]);
}
type = buffImages[0].getType();
chunkWidth = buffImages[0].getWidth();
chunkHeight = buffImages[0].getHeight();

//Initializing the final image
BufferedImage finalImg = new BufferedImage(chunkWidth*cols, chunkHeight*rows, type);

int num = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
finalImg.createGraphics().drawImage(buffImages[num], chunkWidth * j, chunkHeight * i, null);
num++;
}
}
System.out.println("Image concatenated.....");
ImageIO.write(finalImg, "jpeg", new File("finalImg.jpg"));

ranjit kumar said...

above code is not excute please slove that code

Sunil Kale said...
This comment has been removed by the author.
Sunil Kale said...

Hi Kalani,
I am Sunil From Mumbai. I want take multiple images from DB and make one image and fetch that one image from server side to client and again extract that one image get multiple, so can we get this. Can you help me. my email id is sunilkale24@gmail.com

Dilshan Rupasinghe said...

This is very helpful for the works. Thank u as a srilankan

Ilayaraj Ramasamy said...

Good morning mam.do you hav the code to crop a img

Ilayaraj Ramasamy said...

This code shows the error
Cant find symbol ImageIO.read();
and ImageIO.write();

Harvinder singh said...

i have to merge images of 30 MP resulting image will be 2*2 i.e 120 MP so i am getting out of memory error
what can i do can anyone help me

Shashank(admin) said...

Hi Kalyani
What If I have a fifth image which is png and want to overlay above this collage. So that transparent area remains transparent in png image. Please help

Related Posts with Thumbnails