BufferedImage[] input = new BufferedImage[count-1];
//Load each input image.
for(int index=0; index<input.length; index++) {
try {
File f = new File("folder_path\\"+img_"+
(index+1)+".tiff");
input[index] = ImageIO.read(f);
}
catch(Exception e) {
e.printStackTrace();
}
}
int offset = 5, width=0, height=input[0].getHeight();
for(int index=0; index<input.length; index++) {
width+=input[index].getWidth();
if(height<input[index].getHeight())
{
height=input[index].getHeight();
}
}
width+=count*offset;
height+=offset;
//Create Output image
BufferedImage output = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = output.createGraphics();
Color oldColor = g2.getColor();
//fill background
g2.setPaint(Color.WHITE);
g2.fillRect(0, 0, width, height);
//draw image
g2.setColor(oldColor);
int xcordinate=0;
for(int index=0; index<input.length; index++) {
g2.drawImage(input[index], null, xcordinate, 0);
xcordinate+=input[index].getWidth()+offset;
}
g2.dispose();
File merged = new File("folder_path\\"+merged_2_new.tiff");
try {
ImageIO.write(output, "tiff", merged);
}
catch(Exception e) {
e.printStackTrace();
}