Last active
April 30, 2021 17:07
-
-
Save groupdocs-cloud-gists/49545366208d702673bf828cfde885f5 to your computer and use it in GitHub Desktop.
Add Watermark to Images using a REST API in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add Watermark to an Image | |
1. Programmatically upload a JPG file on the cloud | |
2. Add Text Watermark to Image using a REST API in Python | |
3. Download Watermarked image from Cloud |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String clientId = "da0c487d-c1c0-45ae-b7bf-43eaf53c5ad5"; | |
String clientSecret = "479db2b01dcb93a3d4d20efb16dea971"; | |
String myStorage = ""; | |
Configuration configuration = new Configuration(clientId, clientSecret); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// api initialization | |
FileApi apiInstance = new FileApi(clientId, clientSecret); | |
// create download file request | |
DownloadFileRequest request = new DownloadFileRequest("watermark/added_watermark/sample_jpg/sample.jpg", "", null); | |
File response = apiInstance.downloadFile(request); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// api initialization | |
WatermarkApi apiInstance = new WatermarkApi(configuration); | |
// set JPG file path | |
FileInfo fileInfo = new FileInfo(); | |
fileInfo.setFilePath("sample.jpg"); | |
fileInfo.setStorageName(myStorage); | |
// define Watermark Options | |
WatermarkOptions options = new WatermarkOptions(); | |
options.setFileInfo(fileInfo); | |
// define Watermark Details | |
WatermarkDetails watermarkDetails = new WatermarkDetails(); | |
// define Watermark Image Options | |
ImageWatermarkOptions imageWatermarkOptions = new ImageWatermarkOptions(); | |
FileInfo image = new FileInfo(); | |
image.setFilePath("sample_watermark.png"); | |
image.setStorageName(myStorage); | |
imageWatermarkOptions.setImage(image); | |
// set Watermark position | |
Position position = new Position(); | |
position.setX(100.0); | |
position.setY(450.0); | |
watermarkDetails.setPosition(position); | |
// set Image Watermark Options | |
watermarkDetails.setImageWatermarkOptions(imageWatermarkOptions); | |
List<WatermarkDetails> watermarkDetailsList = new ArrayList<WatermarkDetails>(); | |
watermarkDetailsList.add(watermarkDetails); | |
options.setWatermarkDetails(watermarkDetailsList); | |
// create Add Request | |
AddRequest request = new AddRequest(options); | |
WatermarkResult response = apiInstance.add(request); | |
System.out.println("Resultant file path: " + response.getPath()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// api initialization | |
WatermarkApi apiInstance = new WatermarkApi(configuration); | |
// set JPG file path | |
FileInfo fileInfo = new FileInfo(); | |
fileInfo.setFilePath("sample.jpg"); | |
fileInfo.setStorageName(myStorage); | |
// define Watermark Options | |
WatermarkOptions options = new WatermarkOptions(); | |
options.setFileInfo(fileInfo); | |
// define Text Watermark Options | |
TextWatermarkOptions textWatermarkOptions = new TextWatermarkOptions(); | |
textWatermarkOptions.setText("Copyright© 2021"); | |
textWatermarkOptions.setFontFamilyName("Arial"); | |
textWatermarkOptions.setFontSize(12d); | |
textWatermarkOptions.setTextAlignment("Center"); | |
// set Watermark text color | |
Color color = new Color(); | |
color.setName("White"); | |
textWatermarkOptions.setForegroundColor(color); | |
// define Watermark Details | |
WatermarkDetails watermarkDetails = new WatermarkDetails(); | |
watermarkDetails.setTextWatermarkOptions(textWatermarkOptions); | |
// set Watermark position | |
Position position = new Position(); | |
position.setX(180.0); | |
position.setY(450.0); | |
watermarkDetails.setPosition(position); | |
// set watermark details | |
List<WatermarkDetails> watermarkDetailsList = new ArrayList<WatermarkDetails>(); | |
watermarkDetailsList.add(watermarkDetails); | |
options.setWatermarkDetails(watermarkDetailsList); | |
// create Add watermark request | |
AddRequest request = new AddRequest(options); | |
WatermarkResult response = apiInstance.add(request); | |
System.out.println("Resultant file path: " + response.getPath()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// api initialization | |
FileApi apiInstance = new FileApi(clientId, clientSecret); | |
File fileStream = new File("C:\\Files\\sample.jpg"); | |
// create file upload request | |
UploadFileRequest request = new UploadFileRequest("sample.jpg", fileStream, myStorage); | |
FilesUploadResult response = apiInstance.uploadFile(request); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment