Class ImageCrop
- All Implemented Interfaces:
AttachNotifier,DetachNotifier,HasElement,HasStyle,Serializable
- Author:
- Paola De Bartolo / Flowing Code
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionImageCrop(DownloadHandler downloadHandler) Constructs an ImageCrop component whose image is served by the givenDownloadHandler.Constructs an ImageCrop component with the given image URL. -
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.Gets the MIME type used to encode the cropped image, ornullif it is auto-detected from the image source.doubleGets the encoding quality used for lossy output formats.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.protected voidonAttach(AttachEvent attachEvent) 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 rendered (on-screen) 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(DownloadHandler downloadHandler) Sets the source of the image to be cropped to a resource served by the givenDownloadHandler.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.voidsetOutputMimeType(String outputMimeType) Sets the MIME type used to encode the cropped image (for example"image/jpeg"or"image/png").voidsetOutputQuality(double outputQuality) Sets the encoding quality used for lossy output formats such asimage/jpegandimage/webp.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, getContentElement, getState, getState, readFromJson, readFromJson, readFromJson, readFromJson, setState, writeAsJson, writeToJsonMethods 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, 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
Constructs an ImageCrop component whose image is served by the givenDownloadHandler. SeesetImageSrc(DownloadHandler)for the conditions under which the image is served.- Parameters:
downloadHandler- the handler that serves the image to be cropped- See Also:
-
-
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
-
onAttach
-
setImageSrc
Sets the source of the image to be cropped.- Parameters:
imageSrc- the image source
-
setImageSrc
Sets the source of the image to be cropped to a resource served by the givenDownloadHandler.The handler is registered to this component, so the image is served while this component is attached and visible, independently of any other component. Requests are also rejected while this component is disabled or inert (for instance, behind a modal dialog), unless the handler overrides
ElementRequestHandler.getDisabledUpdateMode()orElementRequestHandler.isAllowInert().Use an
inline()handler, since the image is displayed rather than downloaded. When the output MIME type is not set, it is detected from the file extension of the handler's URL postfix; handlers without one (such asDownloadHandler.fromInputStream(...)) fall back toimage/png.As with any
DownloadHandlerset on an element, this method must be called while aUIis current.- Parameters:
downloadHandler- the handler that serves the image to be cropped
-
getImageSrc
Gets the source of the image being cropped.When the source was set with a
DownloadHandler, this returns the internal URL of the registered resource. Passing that URL back tosetImageSrc(String)unregisters the handler, so the URL stops being served.- 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.A
%crop is resolution-independent and is recommended for programmatic use. Apxcrop is interpreted in the image's source (natural) pixels, so the cropped output has a deterministic size regardless of how the image is scaled on screen (see issue #33). Note that this differs fromthe min/max crop constraints, which are expressed in rendered (on-screen) pixels.The configured
x/yare only honored when the crop is set after the image has loaded. On the initial load the selection is centered on the image and its configured position is discarded; its size and the configuredaspect ratioare honored in both cases.A
%crop is measured against the image's natural size when the output is generated, but against its rendered box when the selection is drawn. Both agree as long as the rendered image keeps the source's aspect ratio; forcing both a width and a height on the image (or anobject-fitthat crops or stretches it) makes the on-screen selection diverge from the exported region.- Parameters:
crop- the crop dimensions
-
getCrop
Gets the crop dimensions.Once the image has loaded, the crop is normalized to a percentage of the image's natural size, so the returned unit is
%even whensetCrop(Crop)was given apxcrop. BecauseCropholds integer values, those percentages are rounded to whole units, which on a large image is coarser than the configured pixel values.- Returns:
- 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 rendered (on-screen) pixels.This constraint is applied by react-image-crop in the image's displayed pixels, unlike a
pxcrop, which is expressed in source (natural) 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
-
setOutputMimeType
Sets the MIME type used to encode the cropped image (for example"image/jpeg"or"image/png"). Supported types areimage/png,image/jpegandimage/webp; any other or unsupported value falls back toimage/png.When left unset, the output format is auto-detected from the image source (its data URL MIME type or file extension). Circular crops are always encoded in a transparency-capable format regardless of this setting.
- Parameters:
outputMimeType- the MIME type used to encode the cropped image
-
getOutputMimeType
Gets the MIME type used to encode the cropped image, ornullif it is auto-detected from the image source.- Returns:
- the configured output MIME type, or
null
-
setOutputQuality
public void setOutputQuality(double outputQuality) Sets the encoding quality used for lossy output formats such asimage/jpegandimage/webp. Ignored forimage/png. Defaults to1.0.- Parameters:
outputQuality- a value between 0 and 1- Throws:
IllegalArgumentException- ifoutputQualityisNaNor is outside the range[0, 1]
-
getOutputQuality
public double getOutputQuality()Gets the encoding quality used for lossy output formats.- Returns:
- the output quality, between 0 and 1
-
getCroppedImageDataUri
Returns the cropped image data URI.When the component is created with a
DownloadHandler, this isnulluntil the image is cropped for the first time.- Returns:
- the cropped image data URI, or
nullif not available yet
-
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
-