Complete Java examples for common Image to Image tasks. See the Java SDK guide for installation and result handling.
You can run the examples below with the displayed API credentials. Update the input URLs and filenames as needed. Each Java example can be copied and run independently.
Choose file, in-memory, or output-stream handling for the JPEG result. Stream examples use a binary file stream as their destination.
Upload an existing logo.png file.
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.ImageToImageClient client = new Pdfcrowd.ImageToImageClient("demo", "demo"); client.setOutputFormat("jpg"); client.convertFileToFile("logo.png", "logo.jpg"); } }
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.ImageToImageClient client = new Pdfcrowd.ImageToImageClient("demo", "demo"); client.setOutputFormat("jpg"); byte[] image = client.convertFile("logo.png"); System.out.println(image.length); } }
import com.pdfcrowd.Pdfcrowd; import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Path; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.ImageToImageClient client = new Pdfcrowd.ImageToImageClient("demo", "demo"); try (OutputStream output_stream = Files.newOutputStream(Path.of("logo.jpg"))) { client.setOutputFormat("jpg"); client.convertFileToStream("logo.png", output_stream); } } }
Fetch an image from its URL.
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.ImageToImageClient client = new Pdfcrowd.ImageToImageClient("demo", "demo"); client.setOutputFormat("jpg"); client.convertUrlToFile("https://your-server.com/logo.png", "logo.jpg"); } }
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.ImageToImageClient client = new Pdfcrowd.ImageToImageClient("demo", "demo"); client.setOutputFormat("jpg"); byte[] image = client.convertUrl("https://your-server.com/logo.png"); System.out.println(image.length); } }
import com.pdfcrowd.Pdfcrowd; import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Path; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.ImageToImageClient client = new Pdfcrowd.ImageToImageClient("demo", "demo"); try (OutputStream output_stream = Files.newOutputStream(Path.of("logo.jpg"))) { client.setOutputFormat("jpg"); client.convertUrlToStream( "https://your-server.com/logo.png", output_stream ); } } }
Read logo.png as binary data. In an application, pass the bytes you already hold in memory instead.
import com.pdfcrowd.Pdfcrowd; import java.nio.file.Files; import java.nio.file.Path; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.ImageToImageClient client = new Pdfcrowd.ImageToImageClient("demo", "demo"); byte[] image_data_1 = Files.readAllBytes(Path.of("logo.png")); client.setOutputFormat("jpg"); client.convertRawDataToFile(image_data_1, "logo.jpg"); } }
import com.pdfcrowd.Pdfcrowd; import java.nio.file.Files; import java.nio.file.Path; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.ImageToImageClient client = new Pdfcrowd.ImageToImageClient("demo", "demo"); byte[] image_data_1 = Files.readAllBytes(Path.of("logo.png")); client.setOutputFormat("jpg"); byte[] image = client.convertRawData(image_data_1); System.out.println(image.length); } }
import com.pdfcrowd.Pdfcrowd; import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Path; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.ImageToImageClient client = new Pdfcrowd.ImageToImageClient("demo", "demo"); try (OutputStream output_stream = Files.newOutputStream(Path.of("logo.jpg"))) { byte[] image_data_1 = Files.readAllBytes(Path.of("logo.png")); client.setOutputFormat("jpg"); client.convertRawDataToStream(image_data_1, output_stream); } } }
Open logo.png as a binary input stream and save the result to logo.jpg.
import com.pdfcrowd.Pdfcrowd; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.ImageToImageClient client = new Pdfcrowd.ImageToImageClient("demo", "demo"); try (InputStream input_stream = Files.newInputStream(Path.of("logo.png"))) { client.setOutputFormat("jpg"); client.convertStreamToFile(input_stream, "logo.jpg"); } } }
These examples use scan.png, a 1224 × 1584 pixel image with a chart and a uniform white border. Rotation uses scan-sideways.png; JPEG background replacement uses transparent-logo.png.
Fit scan.png within 400 × 400 pixels while preserving its aspect ratio. The result is a 309 × 400 pixel thumbnail.png. Use a square canvas below when you need an exact 400 × 400 result.
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.ImageToImageClient client = new Pdfcrowd.ImageToImageClient("demo", "demo"); client.setOutputFormat("png"); client.setResize("400x400"); client.convertFileToFile("scan.png", "thumbnail.png"); } }
Fit the whole image onto a 400 × 400 pixel canvas with either a pale blue background or transparent padding. Set 96 DPI so the px canvas dimensions match output pixels. The transparent variant preserves the original image's white background; transparency applies to the added canvas area.
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.ImageToImageClient client = new Pdfcrowd.ImageToImageClient("demo", "demo"); client.setCanvasBackgroundColor("EEF2F6"); client.setCanvasHeight("400px"); client.setCanvasWidth("400px"); client.setDpi(96); client.setOutputFormat("png"); client.setPrintCanvasMode("fit"); client.convertFileToFile("scan.png", "square.png"); } }
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.ImageToImageClient client = new Pdfcrowd.ImageToImageClient("demo", "demo"); client.setCanvasBackgroundColor("00000000"); client.setCanvasHeight("400px"); client.setCanvasWidth("400px"); client.setDpi(96); client.setOutputFormat("png"); client.setPrintCanvasMode("fit"); client.convertFileToFile("scan.png", "square-transparent.png"); } }
Extract the chart from scan.png into chart.png, or remove the uniform outer border and save trimmed.png.
The chart starts 96 pixels from the left and 370 pixels from the top, and measures 1024 × 384 pixels. Set 96 DPI so the px crop coordinates match source image pixels. Adjust the region for your image. Automatic border removal works on solid-color borders; use an explicit crop for uneven scan edges.
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.ImageToImageClient client = new Pdfcrowd.ImageToImageClient("demo", "demo"); client.setCropAreaHeight("384px"); client.setCropAreaWidth("1024px"); client.setCropAreaX("96px"); client.setCropAreaY("370px"); client.setDpi(96); client.setOutputFormat("png"); client.convertFileToFile("scan.png", "chart.png"); } }
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.ImageToImageClient client = new Pdfcrowd.ImageToImageClient("demo", "demo"); client.setOutputFormat("png"); client.setRemoveBorders(true); client.convertFileToFile("scan.png", "trimmed.png"); } }
Rotate scan-sideways.png 90 degrees clockwise to restore the upright orientation, then save upright.png.
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.ImageToImageClient client = new Pdfcrowd.ImageToImageClient("demo", "demo"); client.setOutputFormat("png"); client.setRotate("90"); client.convertFileToFile("scan-sideways.png", "upright.png"); } }
Convert transparent-logo.png to logo-background.jpg with a pale blue background. JPEG cannot retain transparency, so choose the background color explicitly.
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.ImageToImageClient client = new Pdfcrowd.ImageToImageClient("demo", "demo"); client.setCanvasBackgroundColor("EEF2F6"); client.setOutputFormat("jpg"); client.convertFileToFile("transparent-logo.png", "logo-background.jpg"); } }
Convert scan.png to scan.webp at its original pixel dimensions. WebP uses lossy compression here, which can reduce file size at the cost of fine detail. Compare the result with your source image before choosing it for web delivery.
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.ImageToImageClient client = new Pdfcrowd.ImageToImageClient("demo", "demo"); client.setOutputFormat("webp"); client.convertFileToFile("scan.png", "scan.webp"); } }