Crop Image online using Java Cloud SDK
On a daily basis we capture a huge collection of images with digital cameras, scanners, and smartphones. Most files are in JPG, PNG, GIF, TIFF and similar formats. Often we need to remove unwanted objects or adjust the aspect ratio to improve composition. In this article we will show you how to Crop Image using Java Cloud SDK.
Image Crop API
To crop a photo in a Java application we use the Aspose.Imaging Cloud SDK for Java. The API lets you define the position and dimensions of the cropping rectangle. After cropping, you can save the result in the same format or convert it to a different one. See the full list of supported formats. First, add the SDK to your Maven project by updating pom.xml.
<repositories>
<repository>
<id>aspose-cloud</id>
<name>artifact.aspose-cloud-releases</name>
<url>https://artifact.aspose.cloud/repo</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-imaging-cloud</artifactId>
<version>22.4</version>
</dependency>
</dependencies>
After the project is set up, obtain client credentials from the Cloud Dashboard. If you don’t have an account, create a free one on the Aspose Cloud Dashboard and generate a Client ID and Client Secret.
Crop Image using Java
Below is a step‑by‑step guide to load an existing image and crop it online. We will crop a PNG, save the updated image in the same format, and store the result in cloud storage. If no output file name is provided, the API returns the image stream.
- Create an ImagingApi instance using your client credentials.
- Read the PNG file from the local folder with a File object.
- Load the file into a byte[] using readAllBytes(…).
- Define the crop rectangle’s width, height, and X/Y coordinates.
- Build a CreateCroppedImageRequest with the parameters and the desired output name.
- Call createCroppedImage(…) to perform the crop.
Crop Photo in Java
Crop Image preview
Crop Photo using cURL Commands
Our REST‑based APIs can be accessed from any platform, including command‑line tools like cURL. First, generate a JWT access token (using client credentials) with 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"
After obtaining the JWT token, call the CropImage endpoint to crop a PNG stored in cloud storage. The example below saves the cropped image as a JPG.
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"
Conclusion
By the end of this article you have learned how to crop photos with Java code and how to perform the same operation using cURL commands. Explore the Product Documentation for additional API features, or try the API directly in your browser via the Swagger API Reference.
You can download the full source code from our GitHub repository (MIT licensed). If you encounter any issues, visit our free product support forum for quick assistance.
Related Articles
Please visit the following links to learn more about: