In modern enterprise applications, uploading image is a popular requirement, for example, upload ID Card images, avatar images or image attachments etc. Almost all applications have restriction on image size, in order to reduce upload time and stored file size.
Generally, the original image does not fit the size requirement, we need to process the raw image to fit the application restriction. This component will be helpful for processing the original image.
- Based on the Croppie Project, so investigating that project will be helpful before using this component
- Working with FileUploadField from CUBA
- Provided a simple API for using the component
- Provided some options for customize the component appearance, cropping area, image quality etc
- Reviewing the cropping result in real time
The add-on can be added to your project in one of the ways described below. Installation from the Marketplace is the simplest way. The last version of the add-on compatible with the used version of the platform will be installed. Also, you can install the add-on by coordinates choosing the required version of the add-on from the table.
In case you want to install the add-on by manual editing or by building from sources see the complete add-ons installation guide in CUBA Platform documentation.
- Open your application in CUBA Studio. Check the latest version of CUBA Studio on the CUBA Platform site.
- Go to CUBA -> Marketplace in the main menu.
- Find the ImageCropper add-on there.
- Click Install and apply the changes. The add-on corresponding to the used platform version will be installed.
- Open your application in CUBA Studio. Check the latest version of CUBA Studio on the CUBA Platform site.
- Go to CUBA -> Marketplace in the main menu.
- Click the icon in the upper-right corner.
- Paste the add-on coordinates in the corresponding field as follows:
cubacn.cmp.crop:cubacnimgcrop-global:<add-on version>
where <add-on version>
is compatible with the used version of the CUBA platform.
Platform Version | Add-on Version |
---|---|
7.1.x | 1.1.1 |
- Click Install and apply the changes. The add-on will be installed to your project.
@Subscribe("cropBtn")
public void onCropBtnClick(Button.ClickEvent event) {
UUID fileId = uploadField.getFileId();
File file = fileUploadingAPI.getFile(fileId);
if(file==null){
return;
}
// Create an viewport configuration object
ImgCropServerComponent.ViewPort viewPort =
new ImgCropServerComponent.ViewPort(200, 100,
ImgCropServerComponent.ViewPortType.square);
// Create an option object
ImageCropWindowOptions options = new ImageCropWindowOptions(file, 10, viewPort);
// Open a winow for cropping an image
ImageCropWindow.showAsDialog(this, options, (cropWindowAfterScreenCloseEvent)->{
// process the cropping result
if(cropWindowAfterScreenCloseEvent.getCloseAction().equals(WINDOW_DISCARD_AND_CLOSE_ACTION)){
//cropping window is closed by "Cancel" button
}else if(cropWindowAfterScreenCloseEvent.getCloseAction().equals(WINDOW_COMMIT_AND_CLOSE_ACTION)){
// cropping window is closed by "ok" button,then we can get the cropping result in bytes.
byte[] result = options.getResult();
if (result != null) {
//show the cropping result to an image component
image.setSource(StreamResource.class)
.setStreamSupplier(()-> new ByteArrayInputStream(result)).setBufferSize(1024);
}
}
});
}
This component included a demo screen. Clone and run this project, you will see an "Sample" menu item in menu bar.