java.lang.Object
org.miaixz.bus.extra.image.Images
- All Implemented Interfaces:
Flushable,Serializable
An image editor for performing various image manipulations. This class provides a fluent API for chaining operations
like scaling, cutting, watermarking, and more.
- Since:
- Java 17+
- Author:
- Kimi Liu
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionImages(BufferedImage srcImage) Constructs a newImagesinstance from aBufferedImage.Images(BufferedImage srcImage, String targetImageType) Constructs a newImagesinstance. -
Method Summary
Modifier and TypeMethodDescriptionbinary()Converts the image to a binary (black and white) image.cut(int x, int y) Cuts a circular area from the image, with the diameter being the smaller of the image's width and height.cut(int x, int y, int radius) Cuts a circular area from the image with a specified radius.Crops the image to the specified rectangle.flip()Flips the image horizontally.voidflush()static ImagesCreates anImagesinstance from anImage.static ImagesCreates anImagesinstance from an image file.static Imagesfrom(InputStream in) Creates anImagesinstance from an input stream.static ImagesCreates anImagesinstance from a URL.static ImagesCreates anImagesinstance from an image file path.static Imagesfrom(ImageInputStream imageStream) Creates anImagesinstance from anImageInputStream.static Imagesfrom(org.miaixz.bus.core.io.resource.Resource resource) Creates anImagesinstance from a resource.getImg()Retrieves the processed image.gray()Converts the image to grayscale.pressImage(Image pressImg, int x, int y, float alpha) Adds an image watermark to the image.pressImage(Image pressImg, Rectangle rectangle, float alpha) Adds an image watermark to the image within a specified rectangle.pressImageFull(Image pressImage, int lineHeight, int degree, float alpha) Adds a full-screen image watermark to the image.Adds a text watermark to the image.Adds a text watermark to the image using anImageTextobject.pressTextFull(String pressText, Color color, Font font, int lineHeight, int degree, float alpha) Adds a full-screen text watermark to the image.rotate(int degree) Rotates the image by a specified angle.round(double arc) Applies rounded corners to the image.scale(float scale) Scales the image by a given ratio.scale(int width, int height) Scales the image to the specified width and height.scale(int width, int height, int scaleType) Scales the image to the specified width and height with a specific scaling algorithm.Scales the image proportionally to fit within the given dimensions, filling blank space with a fixed color.setBackgroundColor(Color backgroundColor) Sets the background color of the image.setPositionBaseCentre(boolean positionBaseCentre) Sets whether to calculate x, y coordinates from the center as the origin.setQuality(double quality) Sets the image output quality for compression (0.0 to 1.0).setQuality(float quality) Sets the image output quality for compression (0.0 to 1.0).setTargetImageType(String imgType) Sets the target image file format for writing.Adds a stroke (border) to the image.Adds a stroke (border) to the image with a specifiedStrokeobject.Writes the processed image to a file.write(OutputStream out) Writes the processed image to anOutputStream.write(ImageOutputStream targetImageStream) Writes the processed image to anImageOutputStream.
-
Constructor Details
-
Images
Constructs a newImagesinstance from aBufferedImage. The target image type is determined by the source image type.- Parameters:
srcImage- The sourceBufferedImage.
-
Images
Constructs a newImagesinstance.- Parameters:
srcImage- The sourceBufferedImage.targetImageType- The target image type (e.g., "jpg", "png"). If null, it is inferred from the source image.
-
-
Method Details
-
from
Creates anImagesinstance from an image file path.- Parameters:
imagePath- The path to the image file.- Returns:
- A new
Imagesinstance.
-
from
Creates anImagesinstance from an image file.- Parameters:
imageFile- The image file.- Returns:
- A new
Imagesinstance.
-
from
Creates anImagesinstance from a resource.- Parameters:
resource- The image resource.- Returns:
- A new
Imagesinstance.
-
from
Creates anImagesinstance from an input stream.- Parameters:
in- The input stream of the image.- Returns:
- A new
Imagesinstance.
-
from
Creates anImagesinstance from anImageInputStream.- Parameters:
imageStream- The image input stream.- Returns:
- A new
Imagesinstance.
-
from
Creates anImagesinstance from a URL.- Parameters:
imageUrl- The URL of the image.- Returns:
- A new
Imagesinstance.
-
from
Creates anImagesinstance from anImage.- Parameters:
image- The image.- Returns:
- A new
Imagesinstance.
-
setTargetImageType
Sets the target image file format for writing.- Parameters:
imgType- The image format (e.g., "jpg", "png").- Returns:
- This
Imagesinstance for method chaining.
-
setPositionBaseCentre
Sets whether to calculate x, y coordinates from the center as the origin.- Parameters:
positionBaseCentre-trueto use the center as the origin,falsefor the top-left corner.- Returns:
- This
Imagesinstance for method chaining.
-
setQuality
Sets the image output quality for compression (0.0 to 1.0).- Parameters:
quality- The quality, a float between 0.0 and 1.0.- Returns:
- This
Imagesinstance for method chaining.
-
setQuality
Sets the image output quality for compression (0.0 to 1.0).- Parameters:
quality- The quality, a float between 0.0 and 1.0.- Returns:
- This
Imagesinstance for method chaining.
-
setBackgroundColor
Sets the background color of the image.- Parameters:
backgroundColor- The background color.- Returns:
- This
Imagesinstance for method chaining.
-
scale
Scales the image by a given ratio.- Parameters:
scale- The scaling ratio. Greater than 1 for enlargement, between 0 and 1 for reduction.- Returns:
- This
Imagesinstance for method chaining.
-
scale
Scales the image to the specified width and height. This may cause distortion.- Parameters:
width- The target width.height- The target height.- Returns:
- This
Imagesinstance for method chaining.
-
scale
Scales the image to the specified width and height with a specific scaling algorithm.- Parameters:
width- The target width.height- The target height.scaleType- The scaling algorithm (e.g.,Image.SCALE_SMOOTH).- Returns:
- This
Imagesinstance for method chaining.
-
scale
Scales the image proportionally to fit within the given dimensions, filling blank space with a fixed color.- Parameters:
width- The target width.height- The target height.fixedColor- The color to fill the blank space, or null for no fill.- Returns:
- This
Imagesinstance for method chaining.
-
cut
Crops the image to the specified rectangle.- Parameters:
rectangle- TheRectangledefining the crop area.- Returns:
- This
Imagesinstance for method chaining.
-
cut
Cuts a circular area from the image, with the diameter being the smaller of the image's width and height.- Parameters:
x- The starting x-coordinate.y- The starting y-coordinate.- Returns:
- This
Imagesinstance for method chaining.
-
cut
Cuts a circular area from the image with a specified radius.- Parameters:
x- The starting x-coordinate.y- The starting y-coordinate.radius- The radius of the circle. If less than 0, the diameter is the smaller of the image's width and height.- Returns:
- This
Imagesinstance for method chaining.
-
round
Applies rounded corners to the image.- Parameters:
arc- The arc ratio for the corners (0.0 to 1.0), relative to the smaller of the image's width and height.- Returns:
- This
Imagesinstance for method chaining.
-
gray
Converts the image to grayscale.- Returns:
- This
Imagesinstance for method chaining.
-
binary
Converts the image to a binary (black and white) image.- Returns:
- This
Imagesinstance for method chaining.
-
pressText
Adds a text watermark to the image.- Parameters:
pressText- The watermark text.color- The color of the text.font- The font of the text.x- The x-coordinate offset.y- The y-coordinate offset.alpha- The opacity of the text (0.0 to 1.0).- Returns:
- This
Imagesinstance for method chaining.
-
pressText
Adds a text watermark to the image using anImageTextobject.- Parameters:
imageText- TheImageTextobject containing all text watermark information.- Returns:
- This
Imagesinstance for method chaining.
-
pressTextFull
public Images pressTextFull(String pressText, Color color, Font font, int lineHeight, int degree, float alpha) Adds a full-screen text watermark to the image.- Parameters:
pressText- The watermark text.color- The color of the text.font- The font of the text.lineHeight- The line height.degree- The rotation angle in degrees.alpha- The opacity of the text (0.0 to 1.0).- Returns:
- This
Imagesinstance for method chaining.
-
pressImage
Adds an image watermark to the image.- Parameters:
pressImg- The watermark image.x- The x-coordinate offset.y- The y-coordinate offset.alpha- The opacity of the watermark (0.0 to 1.0).- Returns:
- This
Imagesinstance for method chaining.
-
pressImage
Adds an image watermark to the image within a specified rectangle.- Parameters:
pressImg- The watermark image.rectangle- TheRectangledefining the position and size of the watermark.alpha- The opacity of the watermark (0.0 to 1.0).- Returns:
- This
Imagesinstance for method chaining.
-
pressImageFull
Adds a full-screen image watermark to the image.- Parameters:
pressImage- The watermark image.lineHeight- The line height.degree- The rotation angle in degrees.alpha- The opacity of the watermark (0.0 to 1.0).- Returns:
- This
Imagesinstance for method chaining.
-
rotate
Rotates the image by a specified angle.- Parameters:
degree- The rotation angle in degrees.- Returns:
- This
Imagesinstance for method chaining.
-
flip
Flips the image horizontally.- Returns:
- This
Imagesinstance for method chaining.
-
stroke
Adds a stroke (border) to the image.- Parameters:
color- The color of the stroke.width- The width of the stroke.- Returns:
- This
Imagesinstance for method chaining.
-
stroke
Adds a stroke (border) to the image with a specifiedStrokeobject.- Parameters:
color- The color of the stroke.stroke- TheStrokeobject defining the border properties.- Returns:
- This
Imagesinstance for method chaining.
-
getImg
Retrieves the processed image.- Returns:
- The processed
Image.
-
write
Writes the processed image to anOutputStream.- Parameters:
out- The output stream to write to.- Returns:
- This
Imagesinstance for method chaining. - Throws:
org.miaixz.bus.core.lang.exception.InternalException- if an I/O error occurs.
-
write
public Images write(ImageOutputStream targetImageStream) throws org.miaixz.bus.core.lang.exception.InternalException Writes the processed image to anImageOutputStream.- Parameters:
targetImageStream- The target image output stream.- Returns:
- This
Imagesinstance for method chaining. - Throws:
org.miaixz.bus.core.lang.exception.InternalException- if an I/O error occurs.
-
write
Writes the processed image to a file.- Parameters:
destFile- The destination file.- Returns:
- This
Imagesinstance for method chaining. - Throws:
org.miaixz.bus.core.lang.exception.InternalException- if an I/O error occurs.
-
flush
public void flush()
-