How to merge images to JPEG using Java

JPEG Merger .NET API

com.groupdocs.merger allows developers to combine multiple image documents in the preferred order and save them as a single JPEG file.

About JPEG File Format

JPEG is a type of image format that is saved using the method of lossy compression. The output image, as result of compression, is a trade-off between storage size and image quality. Users can adjust the compression level to achieve the desired quality level while at the same time reduce the storage size. Image quality is negligibly affected if 10:1 compression is applied to the image. The higher the compression value, the higher the degradation in image quality.

Download and Configure

Use the downloads section to download API jar package and add it to the .pom file. :

<dependency>
    <groupId>com.groupdocs</groupId>
    <artifactId>groupdocs-merger</artifactId>
    <version>23.2</version>
</dependency>

Source images

&ldquo;Sample1.jpg&rdquo; &ldquo;Sample2.jpg&rdquo; &ldquo;Sample3.jpg&rdquo;

How to merge images to JPEG file

The following example demonstrates how to merge images to JPEG file with several lines of Java code:

  • Create an instance of Merger class and pass source image file path as a constructor parameter. You may specify absolute or relative file path as per your requirements.
  • Create an instance of ImageJoinOptions class and pass enum value of ImageJoinMode as a constructor parameter.
  • Add another image file to merge with join method and pass instance of ImageJoinOptions class as a method parameter. Repeat this step for other image documents you want to merge.
  • Call Merger class save method and specify the filename for the merged image file as parameter.
// Load the source image file
Merger merger = new Merger("c:\sample1.jpeg")
{
    // Define image join options with horizontal join mode
    ImageJoinOptions joinOptions = new ImageJoinOptions(ImageJoinMode.Horizontal);
    // Add another image file to merge
    merger.join("c:\sample2.png", joinOptions);
    // Define image join options with vertical join mode
    ImageJoinOptions joinOptions = new ImageJoinOptions(ImageJoinMode.Vertical);
    // Add next image file to merge
    merger.join("c:\sample3.bmp", joinOptions);
    // Merge image files and save result
    merger.save("c:\merged.jpeg");
}

Result merged JPEG image

&ldquo;Merged.jpeg&rdquo;

Code Examples

Please find more use-cases and complete Java sources of our backend and frontend examples and try them for free!

Merge JPEG Live Demo

GroupDocs.Merger for Java provides an online Image to JPEG Merger App, which allows you to try it for free and check its quality and accuracy.