How to Merge Photos in Java

This article explains how to merge photos in Java. It discusses the environment preparation, the stepwise procedure, and a runnable sample code to create a photo joiner in Java. This information is helpful for working with this feature in Java environments on different platforms.

Steps to Merge Photos in Java

  1. Prepare the environment to use Aspose.Imaging for Java with the Repository Manager
  2. Make a list of images to calculate the output image dimensions
  3. Combine the source images into one and make an output source
  4. Specify different properties of the JpegOptions class
  5. Render the merged image with the JpegImage class instance

The steps above elaborate on the complete process to combine photos in Java. In the first step, list different images to be merged into a single photo. Then create a new photo and export it to the output image before writing it to the disk or a stream based on your requirements.

Code to Merge Photos in Java

import com.aspose.imaging.*;
public class Main {
public static void main(String[] args) throws Exception // Merge photos in Java
{
// Set the licenses
new License().setLicense("License.lic");
// Creating an array of strings
String[] imagePaths = new String[3];
// Initializing array elements
imagePaths[0] = "Sample1.jpg";
imagePaths[1] = "Sample2.jpg";
imagePaths[2] = "Sample3.jpg";
// Path of output image
String outputPath = "output-combine.jpg";
// Get resulting image size
int newWidth = 0;
int newHeight = 0;
for (String imagePath : imagePaths) {
try (com.aspose.imaging.RasterImage image = (com.aspose.imaging.RasterImage) com.aspose.imaging.Image.load(imagePath)) {
com.aspose.imaging.Size size = image.getSize();
newWidth = Math.max(newWidth, size.getWidth());
newHeight += size.getHeight();
}
}
// Combine images into new one
try (com.aspose.imaging.imageoptions.JpegOptions options = new com.aspose.imaging.imageoptions.JpegOptions()) {
options.setSource(new com.aspose.imaging.sources.StreamSource()); // empty
options.setQuality(100);
// Create resultant image
try (com.aspose.imaging.fileformats.jpeg.JpegImage newImage = (com.aspose.imaging.fileformats.jpeg.JpegImage) com.aspose.imaging.Image.create(options, newWidth, newHeight)) {
int stitchedHeight = 0;
for (String imagePath : imagePaths) {
try (com.aspose.imaging.RasterImage image = (com.aspose.imaging.RasterImage) com.aspose.imaging.Image.load(imagePath)) {
com.aspose.imaging.Rectangle bounds = new com.aspose.imaging.Rectangle(0, stitchedHeight, image.getWidth(), image.getHeight());
newImage.saveArgb32Pixels(bounds, image.loadArgb32Pixels(image.getBounds()));
stitchedHeight += image.getHeight();
}
}
// Save resultant image
newImage.save(outputPath);
}
}
System.out.println("Done");
}
}

This sample code can be used to develop an image combiner in Java. It utilizes different methods of the JpegImage class to create the output picture and check the size of the images. Next, it creates a rectangle and saves the output image with the Save method however, you can set different characteristics of the output image by using the methods exposed by the JpegOptions class like the scaled quality, RGB color profile, sample rounding mode, etc. as per your requirements.

In this article, we have learned about performing image merge in Java. If you are interested in learning image cropping then read the article on how to crop images in Java.

 English