Merge Multiple JPG Files into One in Java using REST API.
Merging JPG images can be a time-consuming and challenging task, especially when you have multiple images to merge. Fortunately, GroupDocs.Merger Cloud SDK for Java can help you to accomplish this task quickly and easily. In this article, we will demonstrate how to merge multiple JPG files into one in Java using REST API. So, let’s get started!
The following topics shall be covered in this article:
Java REST API to Merge JPG Images and SDK Installation
GroupDocs.Merger Cloud SDK for Java is a powerful and versatile tool that helps Java developers to merge, extract, rotate, change the document orientation to portrait or landscape, and modify files in the cloud. It is a cloud-based document manipulation and cross-platform API that supports a wide variety of file formats, including Word, PDF, Excel, PowerPoint, HTML, and many more. The SDK is easy to use and can be integrated easily into a Java-based application.
You can either download the API’s JAR file or install it using Maven by adding the following repository and dependency into your project’s pom.xml file:
Maven Repository:
<repository> <id>groupdocs-artifact-repository</id> <name>GroupDocs Artifact Repository</name> <url>https://repository.groupdocs.cloud/repo</url> </repository> Maven Dependency:
<dependency> <groupId>com.groupdocs</groupId> <artifactId>groupdocs-merger-cloud</artifactId> <version>23.2</version> <scope>compile</scope> </dependency> Next, you need to sign up for a free trial account or purchase a subscription plan on the GroupDocs website and get your API key. Once you have the Client Id and Client Secret, add below code snippet into your Java application:
# Get your client_id and client_secret from https://dashboard.groupdocs.cloud after registration. String ClientId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; String ClientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; String MyStorage = "test-internal-storage"; Configuration configuration = new Configuration(ClientId, ClientSecret); How to Combine JPG Files into One in Java using REST API
Merging JPG images in Java using GroupDocs.Merger Cloud SDK is a straightforward process. Here’s how to do it:
Upload the Files
Firstly, upload the JPG files to the cloud using the code example given below:
package com.groupdocsdev.classes; import java.io.File; import com.groupdocs.cloud.merger.client.*; import com.groupdocs.cloud.merger.model.*; import com.groupdocs.cloud.merger.model.requests.*; import com.groupdocs.cloud.merger.api.*; import com.groupdocs.cloud.merger.client.ApiException; // Upload file to the Cloud Storage using Java public class App { public static void main(String[] args) { FileApi apiInstance = new FileApi(configuration); try { File fileStream = new File("H:\\groupdocs-cloud-data\\input-sample-file-one.jpg"); UploadFileRequest request = new UploadFileRequest("java-testing\\input-sample-file-one.jpg", fileStream, MyStorage); FilesUploadResult response = apiInstance.uploadFile(request); System.out.println("Expected response type is FilesUploadResult: " + response.getUploaded().size()); } catch (ApiException e) { System.err.println("Exception while calling FileApi:"); e.printStackTrace(); } } } As a result, the uploaded JPG files will be available in the files section of your dashboard on the cloud.
Combine JPG into One File in Java
This section provides step-by-step instructions and an example code snippet for merging JPG images into one:
- Firstly, import the required classes into your Java file.
- Secondly, create an instance of the DocumentApi class.
- Thirdly, create an instance of the FileInfo class.
- Next, call the setFilePath() method and pass the input file path as a parameter.
- Then, create an instance of the JoinItem class.
- Now, call the setFileInfo() method and pass the fileInfo1 parameter.
- Next, create a second instance of the FileInfo and JoinItem classes.
- Provide the input file path and fileInfo2 parameters.
- Then, set image join mode to VERTICAL or HORIZONTAL .
- Add more JoinItems for merging more than two documents.
- After that, create an instance of the JoinOptions() class.
- Then, add a comma-separated list of created join items.
- Next, set the output file path.
- Now, create an instance of the JoinRequest() class and pass the JoinOptions in the parameter.
- Finally, merge JPG files by calling the join() method of the DocumentApi and passing the JoinRequest parameter.
The following code snippet shows how to merge multiple JPG files into one file in Java using REST API:
package com.groupdocsdev.classes; import java.util.Arrays; import com.groupdocs.cloud.merger.client.*; import com.groupdocs.cloud.merger.model.*; import com.groupdocs.cloud.merger.model.JoinItem.ImageJoinModeEnum; import com.groupdocs.cloud.merger.model.requests.*; import com.groupdocs.cloud.merger.api.*; import com.groupdocs.cloud.merger.client.ApiException; // How to Combine Multiple JPG Files in Java using REST API. public class App { public static void main(String[] args) { // Create an instance of the Document API DocumentApi apiInstance = new DocumentApi(configuration); try { // Prepare the settings FileInfo fileInfo1 = new FileInfo(); fileInfo1.setFilePath("java-testing/input-sample-file-one.jpg"); JoinItem item1 = new JoinItem(); item1.setFileInfo(fileInfo1); FileInfo fileInfo2 = new FileInfo(); fileInfo2.setFilePath("java-testing/input-sample-file-two.jpg"); JoinItem item2 = new JoinItem(); item2.setFileInfo(fileInfo2); item2.setImageJoinMode(ImageJoinModeEnum.VERTICAL); JoinOptions options = new JoinOptions(); options.setJoinItems(Arrays.asList(item1, item2)); options.setOutputPath("java-testing/output-sample-file.jpg"); JoinRequest request = new JoinRequest(options); // Get output file path. DocumentResult response = apiInstance.join(request); System.out.println("Output file path: " + response.getPath()); } catch (ApiException e) { System.err.println("Exception while calling Java API: "); e.printStackTrace(); } } } You can see the output in the image below:
Combine JPG into One File in Java.
Download the File
The above code sample will save the merged JPG file on the cloud. You can download it using the following code sample:
package com.groupdocsdev.classes; import java.io.File; import com.groupdocs.cloud.merger.client.*; import com.groupdocs.cloud.merger.model.requests.*; import com.groupdocs.cloud.merger.api.*; import com.groupdocs.cloud.merger.client.ApiException; // Download File from the Cloud Storage using Java public class App { public static void main(String[] args) { FileApi apiInstance = new FileApi(configuration); try { DownloadFileRequest request = new DownloadFileRequest("java-testing\\output-sample-one.jpg", MyStorage, null); File response = apiInstance.downloadFile(request); System.out.println("Expected response type is downloadFile: " + response.length()); } catch (ApiException e) { System.err.println("Exception while calling FileApi:"); e.printStackTrace(); } } } Free Online JPG Images Merger
How to merge JPG files online for free? Please try the free JPG Merger to combine multiple JPG files into one online. This online document merger is developed using the above-mentioned Groupdocs.Merger Cloud APIs.
Conclusion
In this article, we have explored how to merge JPG images into one in Java using GroupDocs.Merger Cloud SDK. The following is what you have learned from this article:
- how to merge two JPG images into one in Java on the cloud;
- programmatically upload and download the merged file in Java;
- and merge JPG files for free using an online JPG file merger.
Additionally, we also provide an API Reference section that lets you visualize and communicate with our APIs directly through the browser. Java SDK’s complete source code is freely available on Github. Please check the GroupDocs.Merger Cloud SDK for Java Examples here.
Moreover, we suggest you follow our Getting Started guide for detailed steps and API usage.
Finally, we keep writing new blog posts on different document operations using REST API. So, please get in touch for the latest updates.
FAQs
Is the GroupDocs.Merger Cloud SDK free to use?
The GroupDocs.Merger Cloud SDK offers a free trial period, but there are subscription plans available if you want to continue using the SDK after the trial period.
How do I merge multiple JPG images into one in Java?
You can merge and combine JPG files into one in Java using GroupDocs.Merger Cloud SDK for Java.
What file formats does the GroupDocs.Merger Cloud SDK support?
The GroupDocs.Merger Cloud SDK supports a wide range of file formats, including JPG images, Word, PDF, DOCX, XLSX, HTML, and more.
Can I merge multiple JPG images into a single image using GroupDocs.Merger Cloud SDK for Java?
Yes, GroupDocs.Merger Cloud SDK for Java allows developers to merge multiple JPG images into a single image using its merge feature.

