Converting PSD files to JPG images is a frequent requirement when preparing assets for web or mobile applications. Conholdate.Total for Java provides a robust SDK that handles complex Photoshop documents with ease. This guide walks you through PSD to JPG conversion in Java, covering setup, code, and performance tips.
PSD to JPG Conversion in Java in 5 Steps
- Add Maven Dependency: Include the Conholdate.Total repository and the SDK artifact in your
pom.xml.
<repositories>
<repository>
<id>conholdate-repo</id>
<name>Conholdate Maven Repository</name>
<url>https://repository.conholdate.com/repo/</url>
</repository>
</repositories>
<dependency>
<groupId>com.conholdate</groupId>
<artifactId>conholdate-total</artifactId>
<version>24.9</version>
<type>pom</type>
</dependency>
- Define Input and Output Paths: Set the source PSD file and the target JPG file.
private static final String INPUT_PSD_PATH = "sample.psd";
private static final String OUTPUT_JPG_PATH = "sample.jpg";
- Initialize the Converter: Create a
Converterinstance from anInputStream.
Path input = Paths.get(INPUT_PSD_PATH);
try (InputStream inputStream = Files.newInputStream(input)) {
Converter converter = new Converter(inputStream);
// ...
}
- Configure JPG Options: Set JPEG quality and page number using
JpgConvertOptions.
JpgConvertOptions options = new JpgConvertOptions();
options.setQuality(90); // JPEG quality (0‑100)
options.setPageNumber(0); // PSD files have a single page
- Execute Asynchronous Conversion and Validate: Run the conversion in a
CompletableFutureand verify the output file size.
CompletableFuture<Boolean> conversionFuture = CompletableFuture.supplyAsync(() -> {
try {
return convertPSDToJPG(INPUT_PSD_PATH, OUTPUT_JPG_PATH);
} catch (Exception e) {
System.err.println("Conversion failed: " + e.getMessage());
return false;
}
});
boolean success = conversionFuture.get();
if (success && validateOutput(OUTPUT_JPG_PATH)) {
System.out.println("PSD successfully converted to JPG: " + OUTPUT_JPG_PATH);
}
For detailed API information, see the Converter class reference.
Full Working Example for PSD to JPG Conversion in Java
The following code demonstrates a complete, ready‑to‑run implementation of PSD to JPG conversion using Conholdate.Total for Java.
import com.groupdocs.conversion.Converter;
import com.groupdocs.conversion.options.convert.JpgConvertOptions;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public class PSDToJPGConverter {
private static final String INPUT_PSD_PATH = "sample.psd";
private static final String OUTPUT_JPG_PATH = "sample.jpg";
public static void main(String[] args) {
try {
// Asynchronous conversion to keep UI thread responsive or to parallelize work
CompletableFuture<Boolean> conversionFuture = CompletableFuture.supplyAsync(() -> {
try {
return convertPSDToJPG(INPUT_PSD_PATH, OUTPUT_JPG_PATH);
} catch (Exception e) {
System.err.println("Conversion failed: " + e.getMessage());
return false;
}
});
// Wait for conversion to finish and get the result
boolean success = conversionFuture.get();
// Validate output
if (success && validateOutput(OUTPUT_JPG_PATH)) {
System.out.println("PSD successfully converted to JPG: " + OUTPUT_JPG_PATH);
} else {
System.err.println("Conversion completed but output validation failed.");
}
} catch (InterruptedException | ExecutionException e) {
System.err.println("Error during asynchronous processing: " + e.getMessage());
}
}
/**
* Converts a PSD file to JPG using streams to minimise memory consumption.
*
* @param inputPath Path to the source PSD file.
* @param outputPath Desired path for the resulting JPG file.
* @return true if conversion succeeded, false otherwise.
* @throws IOException if file operations fail.
*/
private static boolean convertPSDToJPG(String inputPath, String outputPath) throws IOException {
Path input = Paths.get(inputPath);
Path output = Paths.get(outputPath);
if (!Files.exists(input) || !Files.isReadable(input)) {
System.err.println("Input file does not exist or is not readable: " + inputPath);
return false;
}
// Ensure the output directory exists
if (output.getParent() != null) {
Files.createDirectories(output.getParent());
}
long startTime = System.nanoTime();
// Use try‑with‑resources to guarantee stream closure
try (InputStream inputStream = Files.newInputStream(input);
OutputStream outputStream = Files.newOutputStream(output)) {
// Initialise the Conholdate.Total converter with the input stream
Converter converter = new Converter(inputStream);
// Configure JPG conversion options
JpgConvertOptions options = new JpgConvertOptions();
options.setQuality(90); // Set JPEG quality (0‑100)
options.setPageNumber(0); // PSD files contain a single page; 0 is the default
// Perform conversion directly to the output stream
converter.convert(outputStream, options);
} catch (Exception ex) {
System.err.println("Exception during conversion: " + ex.getMessage());
return false;
}
long durationMs = (System.nanoTime() - startTime) / 1_000_000;
System.out.println("Conversion completed in " + durationMs + " ms.");
return true;
}
/**
* Simple validation to ensure the JPG file was created and is not empty.
*
* @param outputPath Path to the generated JPG file.
* @return true if the file exists and has a size greater than zero.
*/
private static boolean validateOutput(String outputPath) {
Path output = Paths.get(outputPath);
try {
return Files.exists(output) && Files.size(output) > 0;
} catch (IOException e) {
System.err.println("Failed to validate output file: " + e.getMessage());
return false;
}
}
}
Note: This code example demonstrates the core functionality. Before using it in your project, make sure to update any file paths and configuration values to match your actual environment, verify that all required dependencies are properly installed, and test thoroughly in your development environment. If you encounter any issues, please refer to the official documentation or reach out to the support team for assistance.
Conholdate.Total for Java - Prerequisites and Setup
To start using the SDK, you need Java 8 or higher and a Maven‑compatible build system. Download the latest library from the official release page.
# Download the latest JARs (optional if using Maven)
curl -L -o conholdate-total.jar https://releases.conholdate.com/total/java/conholdate-total-24.9.jar
Add the Maven repository and dependency shown earlier to your pom.xml. No additional runtime prerequisites are required, but a valid license is needed for production use.
What Makes Conholdate.Total for Java Suitable for High‑Performance PSD to JPG Conversion
- Stream‑Based Processing - The SDK reads and writes via streams, keeping memory usage low even for large PSD files.
- Asynchronous Execution - Converting in a separate thread prevents UI blocking and enables parallel batch jobs.
- Fine‑Grained JPEG Options - Control quality, compression, and color space through
JpgConvertOptions. - Single‑Page PSD Support - PSD files are treated as a single page, simplifying the conversion flow.
- Robust Error Handling - Detailed exceptions help pinpoint issues such as missing layers or unsupported features.
For more details, see the official documentation.
Practical Tips for Efficient PSD to JPG Conversion in Java
- Use try‑with‑resources to guarantee that streams are closed automatically.
- Reuse the
Converterinstance when converting multiple files to reduce object‑creation overhead. - Set JPEG quality appropriately; a value of 80‑90 provides a good balance between size and visual fidelity.
- Process large files in chunks by relying on the SDK’s streaming architecture rather than loading the entire PSD into memory.
- Measure conversion time with
System.nanoTime()as shown in the sample to identify performance bottlenecks.
Conclusion
PSD to JPG conversion in Java becomes straightforward with Conholdate.Total for Java. The SDK’s streaming API, asynchronous capabilities, and configurable JPEG options let you build fast, memory‑efficient image pipelines. Remember to acquire a proper license for production; you can review pricing details on the pricing page and obtain a temporary license for testing from the temporary license page. With the code and best‑practice guidance in this article, you’re ready to integrate PSD to JPG conversion into your Java applications.
FAQs
How do I perform PSD to JPG conversion in Java using Conholdate.Total?
Follow the step‑by‑step guide above, add the Maven dependency, and use the provided code sample. The SDK handles the heavy lifting of parsing PSD layers and generating a high‑quality JPG.What memory considerations should I keep in mind for PSD to JPG conversion?
The SDK works with streams, so only small buffers are kept in memory. This design avoids OutOfMemory errors even with multi‑megabyte PSD files.Can I adjust JPEG quality during the conversion?
Yes. Set the desired quality (0‑100) on theJpgConvertOptionsobject before invokingconverter.convert().Is asynchronous conversion necessary?
While not mandatory, running the conversion in a separate thread keeps your UI responsive and allows you to process several files in parallel, which is beneficial for batch workflows.
