In this short tutorial, you’ll learn how to convert SVG to PNG with Java REST API. SVG (Scalable Vector Graphics) is an XML-based vector image format for two-dimensional graphics. It is used to display a variety of graphics on the web and is supported by most web browsers. The SVG format is resolution independent, meaning that an SVG graphic can be scaled to any size without losing quality.
While, A PNG (Portable Network Graphics) file is a type of raster image file format that uses lossless compression to store images. PNG files are commonly used to store graphics for web images, such as logos or icons, and have a higher quality than other file formats, such as JPG or GIF. PNG files are larger than other file formats, but support 24-bit color, which allows for a much wider range of colors than other file formats. If you need SVG to PNG Conversion in Java Low Code API then this can be achieved by using the following code snippet.
Prerequisite
- Create account and get API credentials
- Download Aspose.Imaging Cloud SDK for Java
- Setup Java project with the above SDK
Steps to Convert SVG to PNG in Java REST API
- Set Client ID and Client Secret for the API
- Create an instance of ImagingAPI class with client credentials
- Specify input and output files
- Read input SVG file and upload to cloud storage
- Create an instance of ConvertImageRequest with input and output file formats
- Call convertImage method to Convert SVG to PNG using REST API
- Save the output PNG file on local disk
Code for SVG to PNG 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 = "SVGtoPNG.svg"; | |
String outputFileName = "SVGtoPNG.png"; | |
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 = "png"; | |
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 above code snippet helps you to convert SVG to PNG with Java REST API. You merely need to provide SVG file with the help of the Aspose.Imaging REST API SDK and download output PNG file to save it locally.
The above SVG to PNG Conversion could be put to use with any no code or low code apps on any operating system.
The following link shows a similar feature that might be helpful to you: How to Convert GIF to JPG with Java REST API