Class ImageCrop
java.lang.Object
com.vaadin.flow.component.Component
com.vaadin.flow.component.react.ReactAdapterComponent
com.flowingcode.vaadin.addons.imagecrop.ImageCrop
- All Implemented Interfaces:
AttachNotifier,DetachNotifier,HasElement,HasStyle,Serializable
@NpmPackage(value="react-image-crop",
version="11.0.6")
@JsModule("./src/image-crop.tsx")
@Tag("image-crop")
@CssImport("react-image-crop/dist/ReactCrop.css") @CssImport("./styles/image-crop-styles.css")
public class ImageCrop
extends ReactAdapterComponent
Component for cropping images based on
react-image-crop
library.
This component allows users to define and manipulate crop areas on images.
- Author:
- Paola De Bartolo / Flowing Code
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected RegistrationAdds a listener for theCroppedImageEventfired when the cropped image is updated.doubleGets the aspect ratio of the crop.getCrop()Gets the crop dimensions.Gets the maximum crop height, in pixels.Gets the maximum crop width, in pixels.Gets the minimum crop height, in pixels.Gets the minimum crop width, in pixels.byte[]Decodes the cropped image data URI and returns it as a byte array.Returns the cropped image data URI.Gets the alternative information of the image being cropped.Gets the source of the image being cropped.booleanGets whether the crop area is shown as a circle.booleanGets whether the crop resizing and drawing is disabled.booleanGets whether the selection is enabled.booleanisLocked()Gets whether the crop is locked.booleanGets whether rule of thirds lines are shown in the cropped area.voidsetAspect(double aspect) Sets the aspect ratio of the crop.voidsetCircularCrop(boolean circularCrop) Sets whether the crop area should be shown as a circle.voidDefines the crop dimensions.voidsetCropMaxHeight(Integer maxHeight) Sets a maximum crop height, in pixels.voidsetCropMaxWidth(Integer maxWidth) Sets a maximum crop width, in pixels.voidsetCropMinHeight(Integer minHeight) Sets a minimum crop height, in pixels.voidsetCropMinWidth(Integer minWidth) Sets a minimum crop width, in pixels.voidsetDisabled(boolean disabled) Sets whether the user cannot resize or draw a new crop.voidsetImageAlt(String imageAlt) Sets the alternative information of the image to be cropped.voidsetImageFullHeight(Boolean fullHeight) Sets the image to occupy the full viewport height when enabled.voidsetImageSrc(String imageSrc) Sets the source of the image to be cropped.voidsetKeepSelection(boolean keepSelection) Sets whether the selection can't be disabled if the user clicks outside the selection area.voidsetLocked(boolean locked) Sets whether the user cannot create or resize a crop, but can still drag the existing crop around.voidsetRuleOfThirds(boolean ruleOfThirds) Sets whether to show rule of thirds lines in the cropped area.Methods inherited from class com.vaadin.flow.component.react.ReactAdapterComponent
addStateChangeListener, addStateChangeListener, getState, getState, readFromJson, readFromJson, setState, writeAsJsonMethods inherited from class com.vaadin.flow.component.Component
addListener, findAncestor, fireEvent, from, get, getChildren, getElement, getEventBus, getId, getListeners, getLocale, getParent, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getUI, hasListener, isAttached, isTemplateMapped, isVisible, onAttach, onDetach, onEnabledStateChanged, removeFromParent, scrollIntoView, scrollIntoView, set, setElement, setId, setVisibleMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.vaadin.flow.component.AttachNotifier
addAttachListenerMethods inherited from interface com.vaadin.flow.component.DetachNotifier
addDetachListenerMethods inherited from interface com.vaadin.flow.component.HasStyle
addClassName, addClassNames, getClassName, getClassNames, getStyle, hasClassName, removeClassName, removeClassNames, setClassName, setClassName
-
Constructor Details
-
ImageCrop
Constructs an ImageCrop component with the given image URL.- Parameters:
src- the URL of the image to be cropped
-
ImageCrop
Deprecated, for removal: This API element is subject to removal in a future version.This constructor only preserves the image URL and alternate text. UseImageCrop(String)instead.Constructs an ImageCrop component with the given image.- Parameters:
image- the image to be cropped
-
-
Method Details
-
addCroppedImageListener
Adds a listener for theCroppedImageEventfired when the cropped image is updated.- Parameters:
listener- the listener to be added- Returns:
- a registration for the listener, which can be used to remove the listener
-
setImageSrc
Sets the source of the image to be cropped.- Parameters:
imageSrc- the image source
-
getImageSrc
Gets the source of the image being cropped.- Returns:
- the image source
-
setImageAlt
Sets the alternative information of the image to be cropped.- Parameters:
imageAlt- the image alternative information
-
getImageAlt
Gets the alternative information of the image being cropped.- Returns:
- the image alternative information
-
setCrop
Defines the crop dimensions.- Parameters:
crop- the crop dimensions
-
getCrop
Gets the crop dimensions. -
setAspect
public void setAspect(double aspect) Sets the aspect ratio of the crop. For example, 1 for a square or 16/9 for landscape.- Parameters:
aspect- the aspect ratio of the crop
-
getAspect
public double getAspect()Gets the aspect ratio of the crop.- Returns:
- the aspect ratio
-
setCircularCrop
public void setCircularCrop(boolean circularCrop) Sets whether the crop area should be shown as a circle. If the aspect ratio is not 1, the circle will be warped into an oval shape. Defaults to false.- Parameters:
circularCrop- true to show the crop area as a circle, false otherwise
-
isCircularCrop
public boolean isCircularCrop()Gets whether the crop area is shown as a circle.- Returns:
- true if the crop area is a circle, false otherwise
-
setKeepSelection
public void setKeepSelection(boolean keepSelection) Sets whether the selection can't be disabled if the user clicks outside the selection area. Defaults to false.- Parameters:
keepSelection- true so selection can't be disabled if the user clicks outside the selection area, false otherwise.
-
isKeepSelection
public boolean isKeepSelection()Gets whether the selection is enabled.- Returns:
- true if the selection is enabled, false otherwise
-
setDisabled
public void setDisabled(boolean disabled) Sets whether the user cannot resize or draw a new crop. Defaults to false.- Parameters:
disabled- true to disable crop resizing and drawing, false otherwise
-
isDisabled
public boolean isDisabled()Gets whether the crop resizing and drawing is disabled.- Returns:
- true if disabled, false otherwise
-
setLocked
public void setLocked(boolean locked) Sets whether the user cannot create or resize a crop, but can still drag the existing crop around. Defaults to false.- Parameters:
locked- true to lock the crop, false otherwise
-
isLocked
public boolean isLocked()Gets whether the crop is locked.- Returns:
- true if the crop is locked, false otherwise
-
setCropMinWidth
Sets a minimum crop width, in pixels.- Parameters:
minWidth- the minimum crop width
-
getCropMinWidth
Gets the minimum crop width, in pixels.- Returns:
- the minimum crop width
-
setCropMinHeight
Sets a minimum crop height, in pixels.- Parameters:
minHeight- the minimum crop height
-
getCropMinHeight
Gets the minimum crop height, in pixels.- Returns:
- the minimum crop height
-
setCropMaxWidth
Sets a maximum crop width, in pixels.- Parameters:
maxWidth- the maximum crop width
-
getCropMaxWidth
Gets the maximum crop width, in pixels.- Returns:
- the maximum crop width
-
setCropMaxHeight
Sets a maximum crop height, in pixels.- Parameters:
maxHeight- the maximum crop height
-
getCropMaxHeight
Gets the maximum crop height, in pixels.- Returns:
- the maximum crop height
-
setRuleOfThirds
public void setRuleOfThirds(boolean ruleOfThirds) Sets whether to show rule of thirds lines in the cropped area. Defaults to false.- Parameters:
ruleOfThirds- true to show rule of thirds lines, false otherwise
-
isRuleOfThirds
public boolean isRuleOfThirds()Gets whether rule of thirds lines are shown in the cropped area.- Returns:
- true if rule of thirds lines are shown, false otherwise
-
getCroppedImageDataUri
Returns the cropped image data URI.- Returns:
- the cropped image data URI
-
setImageFullHeight
Sets the image to occupy the full viewport height when enabled. IffullHeightistrue, applies a CSS class that sets the image height to 100vh. Iffalse, removes the class to revert to the default height.- Parameters:
fullHeight- whether the image should fill the viewport height
-
getCroppedImageBase64
public byte[] getCroppedImageBase64()Decodes the cropped image data URI and returns it as a byte array. If the image data URI is not in the format "data:image/*;base64,", it will be decoded assuming it is a Base64 encoded string.This method incorporates work licensed under MIT. Copyright 2021-2023 David "F0rce" Dodlek https://github.com/F0rce/cropper
- Returns:
- byte[] the decoded byte array of the cropped image
-