How to Convert SVG to JPG with Java REST API

This short tutorial shows you how to convert SVG to JPG with Java REST API. SVG stands for Scalable Vector Graphics and is a type of file format used for creating and displaying vector graphics on the web. Vector graphics are composed of a series of points, lines, and shapes that can be scaled without losing any image quality. SVG is widely used for creating logos, icons, illustrations, diagrams, charts, and more.

Whereas, JPG (or JPEG) stands for Joint Photographic Experts Group and is a file format used for storing digital photos. It is a popular format for compressing and displaying high-quality photographic images. JPG files are smaller than some other image file formats, making them easier to share online. If you want SVG to JPG Conversion in Java Low Code API then this can be achieved by using the following code snippet.

Prerequisite

Steps to Convert SVG to JPG in Java REST API

  1. Set Client ID and Client Secret for the API
  2. Create an object of ImagingAPI class with client credentials
  3. Specify input and output files
  4. Read input SVG file and upload to cloud storage
  5. Create an instance of ConvertImageRequest with input and output file formats
  6. Call convertImage method to Convert SVG to JPG using REST API
  7. Save the output JPG file on local disk

Code for SVG to JPG Conversion in Java Low Code API

String ClientID = Client.getID(); //replace Client.getID() with your own client ID here
String ClientSecret = Client.getSecret(); //replace Client.getSecret() with your own client secret here
String APIBaseUrl="https://api.aspose.cloud";
String Local_Path = "C:/Temp/";
ImagingApi imagingApiSdk = new ImagingApi(ClientSecret, ClientID, APIBaseUrl);
// Input & output file names
String inputFileName = "SVGtoJPG.svg";
String outputFileName = "SVGtoJPG.jpg";
try {
// Upload local image to Cloud Storage
File inputFile = new File(Local_Path + inputFileName);
FileInputStream inputFileStream = new FileInputStream(inputFile);
byte[] inputImageData = IOUtils.toByteArray(inputFileStream);
UploadFileRequest uploadFileRequest = new UploadFileRequest(inputFileName, inputImageData, null);
FilesUploadResult filesUploadResult = imagingApiSdk.uploadFile(uploadFileRequest);
String outputFormat = "jpg";
String remoteFolder = null; // Input file is saved at the root of the storage
String remoteStorage = null; // Cloud Storage name
ConvertImageRequest convertImageRequest = new ConvertImageRequest(inputFileName, outputFormat, remoteFolder, remoteStorage);
byte[] convertedImageData = imagingApiSdk.convertImage(convertImageRequest);
// Save exported image to local storage
FileOutputStream fileOutputStream = new FileOutputStream(Local_Path + outputFileName);
fileOutputStream.write(convertedImageData);
fileOutputStream.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}

The simple and easy code snippet given on this page lets you convert SVG to JPG with Java REST API. You simply have to supply SVG file with the help of the Aspose.Imaging REST API SDK and download output JPG file to save it locally.

The above SVG to JPG Conversion could be used with any no code or low code apps on any operating system.

Another relevant feature can be found at the following URL: How to Convert JPG to PNG with Java REST API

 English