Module bus.extra

Class Images

java.lang.Object
org.miaixz.bus.extra.image.Images
All Implemented Interfaces:
Flushable, Serializable

public class Images extends Object implements 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 Details

    • Images

      public Images(BufferedImage srcImage)
      Constructs a new Images instance from a BufferedImage. The target image type is determined by the source image type.
      Parameters:
      srcImage - The source BufferedImage.
    • Images

      public Images(BufferedImage srcImage, String targetImageType)
      Constructs a new Images instance.
      Parameters:
      srcImage - The source BufferedImage.
      targetImageType - The target image type (e.g., "jpg", "png"). If null, it is inferred from the source image.
  • Method Details

    • from

      public static Images from(Path imagePath)
      Creates an Images instance from an image file path.
      Parameters:
      imagePath - The path to the image file.
      Returns:
      A new Images instance.
    • from

      public static Images from(File imageFile)
      Creates an Images instance from an image file.
      Parameters:
      imageFile - The image file.
      Returns:
      A new Images instance.
    • from

      public static Images from(org.miaixz.bus.core.io.resource.Resource resource)
      Creates an Images instance from a resource.
      Parameters:
      resource - The image resource.
      Returns:
      A new Images instance.
    • from

      public static Images from(InputStream in)
      Creates an Images instance from an input stream.
      Parameters:
      in - The input stream of the image.
      Returns:
      A new Images instance.
    • from

      public static Images from(ImageInputStream imageStream)
      Creates an Images instance from an ImageInputStream.
      Parameters:
      imageStream - The image input stream.
      Returns:
      A new Images instance.
    • from

      public static Images from(URL imageUrl)
      Creates an Images instance from a URL.
      Parameters:
      imageUrl - The URL of the image.
      Returns:
      A new Images instance.
    • from

      public static Images from(Image image)
      Creates an Images instance from an Image.
      Parameters:
      image - The image.
      Returns:
      A new Images instance.
    • setTargetImageType

      public Images setTargetImageType(String imgType)
      Sets the target image file format for writing.
      Parameters:
      imgType - The image format (e.g., "jpg", "png").
      Returns:
      This Images instance for method chaining.
    • setPositionBaseCentre

      public Images setPositionBaseCentre(boolean positionBaseCentre)
      Sets whether to calculate x, y coordinates from the center as the origin.
      Parameters:
      positionBaseCentre - true to use the center as the origin, false for the top-left corner.
      Returns:
      This Images instance for method chaining.
    • setQuality

      public Images setQuality(double quality)
      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 Images instance for method chaining.
    • setQuality

      public Images setQuality(float quality)
      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 Images instance for method chaining.
    • setBackgroundColor

      public Images setBackgroundColor(Color backgroundColor)
      Sets the background color of the image.
      Parameters:
      backgroundColor - The background color.
      Returns:
      This Images instance for method chaining.
    • scale

      public Images scale(float 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 Images instance for method chaining.
    • scale

      public Images scale(int width, int height)
      Scales the image to the specified width and height. This may cause distortion.
      Parameters:
      width - The target width.
      height - The target height.
      Returns:
      This Images instance for method chaining.
    • scale

      public Images scale(int width, int height, int scaleType)
      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 Images instance for method chaining.
    • scale

      public Images scale(int width, int height, Color fixedColor)
      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 Images instance for method chaining.
    • cut

      public Images cut(Rectangle rectangle)
      Crops the image to the specified rectangle.
      Parameters:
      rectangle - The Rectangle defining the crop area.
      Returns:
      This Images instance for method chaining.
    • cut

      public Images 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.
      Parameters:
      x - The starting x-coordinate.
      y - The starting y-coordinate.
      Returns:
      This Images instance for method chaining.
    • cut

      public Images cut(int x, int y, int radius)
      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 Images instance for method chaining.
    • round

      public Images round(double arc)
      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 Images instance for method chaining.
    • gray

      public Images gray()
      Converts the image to grayscale.
      Returns:
      This Images instance for method chaining.
    • binary

      public Images binary()
      Converts the image to a binary (black and white) image.
      Returns:
      This Images instance for method chaining.
    • pressText

      public Images pressText(String pressText, Color color, Font font, int x, int y, float alpha)
      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 Images instance for method chaining.
    • pressText

      public Images pressText(ImageText imageText)
      Adds a text watermark to the image using an ImageText object.
      Parameters:
      imageText - The ImageText object containing all text watermark information.
      Returns:
      This Images instance 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 Images instance for method chaining.
    • pressImage

      public Images pressImage(Image pressImg, int x, int y, float alpha)
      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 Images instance for method chaining.
    • pressImage

      public Images pressImage(Image pressImg, Rectangle rectangle, float alpha)
      Adds an image watermark to the image within a specified rectangle.
      Parameters:
      pressImg - The watermark image.
      rectangle - The Rectangle defining the position and size of the watermark.
      alpha - The opacity of the watermark (0.0 to 1.0).
      Returns:
      This Images instance for method chaining.
    • pressImageFull

      public Images pressImageFull(Image pressImage, int lineHeight, int degree, float alpha)
      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 Images instance for method chaining.
    • rotate

      public Images rotate(int degree)
      Rotates the image by a specified angle.
      Parameters:
      degree - The rotation angle in degrees.
      Returns:
      This Images instance for method chaining.
    • flip

      public Images flip()
      Flips the image horizontally.
      Returns:
      This Images instance for method chaining.
    • stroke

      public Images stroke(Color color, float width)
      Adds a stroke (border) to the image.
      Parameters:
      color - The color of the stroke.
      width - The width of the stroke.
      Returns:
      This Images instance for method chaining.
    • stroke

      public Images stroke(Color color, Stroke stroke)
      Adds a stroke (border) to the image with a specified Stroke object.
      Parameters:
      color - The color of the stroke.
      stroke - The Stroke object defining the border properties.
      Returns:
      This Images instance for method chaining.
    • getImg

      public Image getImg()
      Retrieves the processed image.
      Returns:
      The processed Image.
    • write

      public Images write(OutputStream out) throws org.miaixz.bus.core.lang.exception.InternalException
      Writes the processed image to an OutputStream.
      Parameters:
      out - The output stream to write to.
      Returns:
      This Images instance 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 an ImageOutputStream.
      Parameters:
      targetImageStream - The target image output stream.
      Returns:
      This Images instance for method chaining.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • write

      public Images write(File destFile) throws org.miaixz.bus.core.lang.exception.InternalException
      Writes the processed image to a file.
      Parameters:
      destFile - The destination file.
      Returns:
      This Images instance for method chaining.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • flush

      public void flush()
      Specified by:
      flush in interface Flushable