The JPG image is a popular format for printing and editing purposes. Furthermore, its a popular image format for sharing photos and other images on the internet and between Mobile and PC users. The small file size of JPG images allows storing of thousands of images in small memory space. Now as the compression is lossy, it means that certain unnecessary information is permanently deleted. On the other end, the biggest advantage of PNG over JPEG is that the compression is lossless, meaning there is no loss in quality each time it is opened and saved again. PNG also handles detailed, high-contrast images well. Now in this article, we are going to discuss the details on how to Convert JPG to PNG using Java Cloud SDK
Image Conversion API
Our REST based API offers the capabilities to create, edit and transform image files to variety of supported file formats. Now in order to implement the feature to load and convert jpg to png transparent in Java application, we need to use Aspose.Imaging Cloud SDK for Java as it’s a wrapper around Cloud API. So the next step is to add its reference in java project by including following information in pom.xml of maven build type project.
<repositories> <repository> <id>aspose-cloud</id> <name>artifact.aspose-cloud-releases</name> <url>http://artifact.aspose.cloud/repo</url> </repository> </repositories> <dependencies> <dependency> <groupId>com.aspose</groupId> <artifactId>aspose-imaging-cloud</artifactId> <version>22.4</version> </dependency> </dependencies>
Once the JDK reference has been added in project, the next important step is to create a free account over Aspose Cloud Dashboard. Simply sign up for a Free Trial through a valid email address. Now login using newly created account and lookup/create Client ID and Client Secret at Cloud Dashboard. These details are required for authentication purposes in the following sections.
Convert JPG to PNG in Java
Now lets discuss the details on how we can convert JPG to PNG using Java code snippet. In order to perform the conversion, we have two APIs to convert an image to another format:
The GET API expects us to first upload an image to Cloud Storage then pass its name in the API URL. After updating the image parameters, the API returns the updated image in the response. If you would like to save the updated image on the Cloud Storage, you explicitly need to do this as shown in the below examples.
On the other hand, when calling the second POST API, you can directly pass the image in the request body. It also lets you save the updated image on the Cloud Storage by specifying the outPath parameter value. However, if you do not specify the value, the response contains a streamed image.
- Create an instance of ImagingApi based on your personalized client credentials
- Read all JPG files form local folder using File object
- Iterate through files in array and reach content of individual image into byte instance
- Now create an object of CreateConvertedImageRequest where we specify the output image format as PNG
- Call the method createConvertedImage(…) which saves JPG as PNG and returns result as response stream
- Finally save the PNG file to local drive using FileOutputStream object
Change JPG to PNG using cURL Commands
As we can access REST APIs using cURL commands, so in this section, we are exploring the details to convert JPG to PNG transparent. Now as a pre-requisite, we need to first generate a JWT access token (based on client credentials) while executing the following command.
curl -v "https://api.aspose.cloud/connect/token" \ -X POST \ -d "grant_type=client_credentials&client_id=bb959721-5780-4be6-be35-ff5c3a6aa4a2&client_secret=4d84d5f6584160cbd91dba1fe145db14" \ -H "Content-Type: application/x-www-form-urlencoded" \ -H "Accept: application/json"
Now we are going to use ConvertImage API call, it expects the input JPG to be already available in cloud storage. Now please execute the following command to change JPG to PNG.
curl -v -X GET "https://api.aspose.cloud/v3.0/imaging/image1.jpg/convert?format=PNG" \ -H "accept: application/json" \ -H "authorization: Bearer <JWT Token>" \ -o Converted.png
This article has provided some amazing details on how we can convert JPG to PNG using Java code snippet. Similarly, we have also discussed the steps to convert JPG to PNG transparent using cURL Commands. Please note that the Product Documentation is one amazing place to learn about other exciting features being offered by the API. In case you need to try out these APIs within browser, please try using the swagger API Reference.
Penultimate, please note that all our Cloud SDKs are published under MIT license, so you may consider downloading the complete source code from GitHub and modify it as per your requirements. Lastly, if you encoutner any issues while using the API, you may consider approaching us for a quick resolution via free product support forum.
Please visit the following links to learn more about: