On daily basis, we produce a huge collection of images through digital cameras, scanners etc and majority of them are in JPG, PNG, GIF, TIFF etc. Now we might be capturing unnecessary objects/elements in a picture. So we often have a requirement to crop a photo to remove an unwanted subject or irrelevant detail from an image. We may have a requirement to change the aspect ratio of an image or to improve the overall composition of the image. So in this article, we are going to learn the details on how to Crop Image using Java Cloud SDK.
Image Crop API
In order to crop photo in Java application, we are going to use Aspose.Imaging Cloud SDK for Java. The API enables you to specify the position as well as the dimensions of the cropping rectangle. After the crop image operation is complete, we can either save the output image in same format or, take the leverage to save the cropped image in a different format. Please visit the following link to get the list of supported formats. Now in order to get started, the first step is to add its reference in java project by including following information in pom.xml (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 project is setup, we need to obtain the client credentials from Cloud Dashboard. Now if you do not have an account over Aspose Cloud Dashboard, please create a free account through valid email address. Now login using newly created account and lookup/create Client ID and Client Secret at Aspose Cloud Dashboard.
Crop Image using Java
Let’s explore the details on how we can load an existing image and crop image online. First we are going to crop PNG/croppng and save the updated image in same format. The PNG will be loaded from local drive, crop photo online and the resultant output is saved in cloud storage. In case we do not specify the output file name, the response will contain the streamed image.
- Firstly, we need to create an instance of ImagingApi based on personalized client credentials
- Secondly, read the PNG file form local folder using File object
- Create byte instance and read all bytes from file using readAllBytes(…) method
- Now specify the dimensions and X,Y coordinates regarding the point to start crop operation
- Subsequently, create an instance of CreateCroppedImageRequest which requires all above parameters and name of resultant image
- Finally, crop image using createCroppedImage(…) method
Crop Photo using cURL Commands
As our APIs are developed on REST architecture, so we provide two flexibilities i.e. get unified experience even when calling APIs on any platform and access the APIs using cURL commands. Now in order to access the APIs using cURL commands over command line terminal, we need to first generate a JWT access token (based on client credentials) using 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"
Once the JWT token has been generated, please call CropImage API to crop photo loaded from cloud storage in PNG format. Now in order to save the cropped image in JPG format, we are specifying JPG value against format parameter. The resultant crop picture is returned in response stream.
curl -v -X GET "https://api.aspose.cloud/v3.0/imaging/image1.jpg/crop?x=50&y=50&width=200&height=200&format=JPG" \ -H "accept: application/json" \ -H "authorization: Bearer <JWT Token>" -o "cropped.jpg"
By the end of this article, we have leared the steps on how to crop photo using Java code snippet. At the same time, we have also also explored the option to crop image online using cURL commands. Other than crop picture operation, you may explore Product Documentation to learn about other exciting features offered by the API. Before you work with code snippet, you may explore API features within browser through swagger API Reference.
Nevertheless, we provide full flexibility to our users by enabling them to download complete source code of Image processing API from GitHub (it’s published under MIT license). In case 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: