Module bus.extra

Class ImageKit

java.lang.Object
org.miaixz.bus.extra.image.ImageKit

public class ImageKit extends Object
Image processing utility class.

Features include: scaling, cropping, rotating, converting image types, converting to grayscale, adding text watermarks, adding image watermarks, etc. Reference: http://blog.csdn.net/zhangzhikaixinya/article/details/8459400

Since:
Java 17+
Author:
Kimi Liu
  • Field Details

  • Constructor Details

    • ImageKit

      public ImageKit()
  • Method Details

    • scale

      public static void scale(File srcImageFile, File destImageFile, float scale)
      Scales an image by a given ratio. The target file type is determined by the extension of the destination file.
      Parameters:
      srcImageFile - The source image file.
      destImageFile - The destination image file for the scaled image. Its extension determines the output format.
      scale - The scaling ratio. A ratio greater than 1 enlarges the image, while a ratio between 0 and 1 shrinks it.
    • scale

      public static void scale(InputStream srcStream, OutputStream targetStream, float scale)
      Scales an image by a given ratio. The default output format is JPEG. This method does not close the streams.
      Parameters:
      srcStream - The input stream of the source image.
      targetStream - The output stream for the scaled image.
      scale - The scaling ratio. A ratio greater than 1 enlarges the image, while a ratio between 0 and 1 shrinks it.
    • scale

      public static void scale(ImageInputStream srcStream, ImageOutputStream targetStream, float scale)
      Scales an image by a given ratio. The default output format is JPEG. This method does not close the streams.
      Parameters:
      srcStream - The source ImageInputStream.
      targetStream - The destination ImageOutputStream.
      scale - The scaling ratio. A ratio greater than 1 enlarges the image, while a ratio between 0 and 1 shrinks it.
    • scale

      public static void scale(Image srcImg, File destFile, float scale) throws org.miaixz.bus.core.lang.exception.InternalException
      Scales an image by a given ratio. The default output format is JPEG. Remember to manually call flush(Image) to release resources.
      Parameters:
      srcImg - The source image.
      destFile - The destination file for the scaled image.
      scale - The scaling ratio. A ratio greater than 1 enlarges the image, while a ratio between 0 and 1 shrinks it.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • scale

      public static void scale(Image srcImg, OutputStream out, float scale) throws org.miaixz.bus.core.lang.exception.InternalException
      Scales an image by a given ratio. The default output format is JPEG. This method does not close the output stream. Remember to manually call flush(Image) to release resources.
      Parameters:
      srcImg - The source image.
      out - The output stream for the scaled image.
      scale - The scaling ratio. A ratio greater than 1 enlarges the image, while a ratio between 0 and 1 shrinks it.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • scale

      public static void scale(Image srcImg, ImageOutputStream destImageStream, float scale) throws org.miaixz.bus.core.lang.exception.InternalException
      Scales an image by a given ratio. The default output format is JPEG. This method does not close the output stream. Remember to manually call flush(Image) to release resources.
      Parameters:
      srcImg - The source image.
      destImageStream - The destination ImageOutputStream.
      scale - The scaling ratio. A ratio greater than 1 enlarges the image, while a ratio between 0 and 1 shrinks it.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • scale

      public static Image scale(Image srcImg, float scale)
      Scales an image by a given ratio. Remember to manually call flush(Image) to release resources.
      Parameters:
      srcImg - The source image.
      scale - The scaling ratio. A ratio greater than 1 enlarges the image, while a ratio between 0 and 1 shrinks it.
      Returns:
      The scaled Image.
    • scale

      public static Image scale(Image srcImg, int width, int height)
      Scales an image to a specific width and height. Note: This may distort the image if the aspect ratio is not maintained. Remember to manually call flush(Image) to release resources.
      Parameters:
      srcImg - The source image.
      width - The target width.
      height - The target height.
      Returns:
      The scaled Image.
    • scale

      public static void scale(File srcImageFile, File targetImageFile, int width, int height, Color fixedColor) throws org.miaixz.bus.core.lang.exception.InternalException
      Scales an image to a specific width and height. The output format is determined by the destination file's extension.
      Parameters:
      srcImageFile - The source image file.
      targetImageFile - The destination file for the scaled image.
      width - The target width.
      height - The target height.
      fixedColor - The color to fill the background if the aspect ratio changes, or null for no fill.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • scale

      public static void scale(InputStream srcStream, OutputStream targetStream, int width, int height, Color fixedColor) throws org.miaixz.bus.core.lang.exception.InternalException
      Scales an image to a specific width and height. The default output format is JPEG. This method does not close the streams.
      Parameters:
      srcStream - The input stream of the source image.
      targetStream - The output stream for the scaled image.
      width - The target width.
      height - The target height.
      fixedColor - The color to fill the background if the aspect ratio changes, or null for no fill.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • scale

      public static void scale(ImageInputStream srcStream, ImageOutputStream targetStream, int width, int height, Color fixedColor) throws org.miaixz.bus.core.lang.exception.InternalException
      Scales an image to a specific width and height. The default output format is JPEG. This method does not close the streams.
      Parameters:
      srcStream - The source ImageInputStream.
      targetStream - The destination ImageOutputStream.
      width - The target width.
      height - The target height.
      fixedColor - The color to fill the background if the aspect ratio changes, or null for no fill.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • scale

      public static void scale(Image srcImage, ImageOutputStream targetImageStream, int width, int height, Color fixedColor) throws org.miaixz.bus.core.lang.exception.InternalException
      Scales an image to a specific width and height. The default output format is JPEG. This method does not close the output stream. Remember to manually call flush(Image) to release resources.
      Parameters:
      srcImage - The source image.
      targetImageStream - The destination ImageOutputStream.
      width - The target width.
      height - The target height.
      fixedColor - The color to fill the background if the aspect ratio changes, or null for no fill.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • scale

      public static Image scale(Image srcImage, int width, int height, Color fixedColor)
      Scales an image to a specific width and height. The default output format is JPEG. Remember to manually call flush(Image) to release resources.
      Parameters:
      srcImage - The source image.
      width - The target width.
      height - The target height.
      fixedColor - The color to fill the background if the aspect ratio changes, or null for no fill.
      Returns:
      The scaled Image.
    • cut

      public static void cut(File srcImgFile, File targetImgFile, Rectangle rectangle)
      Crops an image using the specified coordinates and dimensions.
      Parameters:
      srcImgFile - The source image file.
      targetImgFile - The destination file for the cropped image.
      rectangle - A Rectangle object specifying the x, y, width, and height of the crop area.
    • cut

      public static void cut(InputStream srcStream, OutputStream targetStream, Rectangle rectangle)
      Crops an image using the specified coordinates and dimensions. This method does not close the streams.
      Parameters:
      srcStream - The input stream of the source image.
      targetStream - The output stream for the cropped image.
      rectangle - A Rectangle object specifying the x, y, width, and height of the crop area.
    • cut

      public static void cut(ImageInputStream srcStream, ImageOutputStream targetStream, Rectangle rectangle)
      Crops an image using the specified coordinates and dimensions. This method does not close the streams.
      Parameters:
      srcStream - The source ImageInputStream.
      targetStream - The destination ImageOutputStream.
      rectangle - A Rectangle object specifying the x, y, width, and height of the crop area.
    • cut

      public static void cut(Image srcImage, File destFile, Rectangle rectangle) throws org.miaixz.bus.core.lang.exception.InternalException
      Crops an image using the specified coordinates and dimensions. This method does not close the output stream. Remember to manually call flush(Image) to release resources.
      Parameters:
      srcImage - The source image.
      destFile - The destination file for the cropped image.
      rectangle - A Rectangle object specifying the x, y, width, and height of the crop area.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • cut

      public static void cut(Image srcImage, OutputStream out, Rectangle rectangle) throws org.miaixz.bus.core.lang.exception.InternalException
      Crops an image using the specified coordinates and dimensions. This method does not close the output stream.
      Parameters:
      srcImage - The source image.
      out - The output stream for the cropped image.
      rectangle - A Rectangle object specifying the x, y, width, and height of the crop area.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • cut

      public static void cut(Image srcImage, ImageOutputStream targetImageStream, Rectangle rectangle) throws org.miaixz.bus.core.lang.exception.InternalException
      Crops an image using the specified coordinates and dimensions. This method does not close the output stream. Remember to manually call flush(Image) to release resources.
      Parameters:
      srcImage - The source image.
      targetImageStream - The destination ImageOutputStream.
      rectangle - A Rectangle object specifying the x, y, width, and height of the crop area.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • cut

      public static Image cut(Image srcImage, Rectangle rectangle)
      Crops an image using the specified coordinates and dimensions. Remember to manually call flush(Image) to release resources.
      Parameters:
      srcImage - The source image.
      rectangle - A Rectangle object specifying the x, y, width, and height of the crop area.
      Returns:
      The cropped BufferedImage.
    • cut

      public static Image cut(Image srcImage, int x, int y)
      Crops a circular area from the image. The diameter is the smaller of the image's width and height. Remember to manually call flush(Image) to release resources.
      Parameters:
      srcImage - The source image.
      x - The x-coordinate of the top-left corner of the crop area.
      y - The y-coordinate of the top-left corner of the crop area.
      Returns:
      The cropped Image.
    • cut

      public static Image cut(Image srcImage, int x, int y, int radius)
      Crops a circular area from the image. Remember to manually call flush(Image) to release resources.
      Parameters:
      srcImage - The source image.
      x - The x-coordinate of the center of the circle.
      y - The y-coordinate of the center of the circle.
      radius - The radius of the circle. If less than 0, the diameter will be the smaller of the image's width and height.
      Returns:
      The cropped Image.
    • slice

      public static void slice(File srcImageFile, File descDir, int targetWidth, int targetHeight)
      Slices an image into smaller pieces of a specified width and height.
      Parameters:
      srcImageFile - The source image file.
      descDir - The destination directory for the sliced images.
      targetWidth - The width of each slice (default is 200).
      targetHeight - The height of each slice (default is 150).
    • slice

      public static void slice(Image srcImage, File descDir, int targetWidth, int targetHeight)
      Slices an image into smaller pieces of a specified width and height. Remember to manually call flush(Image) to release resources.
      Parameters:
      srcImage - The source image.
      descDir - The destination directory for the sliced images.
      targetWidth - The width of each slice (default is 200).
      targetHeight - The height of each slice (default is 150).
    • sliceByRowsAndCols

      public static void sliceByRowsAndCols(File srcImageFile, File targetDir, String formatName, int rows, int cols)
      Slices an image into a specified number of rows and columns.
      Parameters:
      srcImageFile - The source image file.
      targetDir - The destination directory for the sliced images.
      formatName - The format name (e.g., "jpg", "png").
      rows - The number of rows (default is 2, must be between 1 and 20).
      cols - The number of columns (default is 2, must be between 1 and 20).
    • sliceByRowsAndCols

      public static void sliceByRowsAndCols(Image srcImage, File destDir, String formatName, int rows, int cols)
      Slices an image into a specified number of rows and columns, with default RGB mode. Remember to manually call flush(Image) to release resources.
      Parameters:
      srcImage - The source image. If not a BufferedImage, it defaults to RGB mode.
      destDir - The destination directory for the sliced images.
      formatName - The format name (e.g., "jpg", "png").
      rows - The number of rows (default is 2, must be between 1 and 20).
      cols - The number of columns (default is 2, must be between 1 and 20).
    • convert

      public static void convert(File srcImageFile, File targetImageFile)
      Converts the image type, e.g., GIF to JPG, GIF to PNG, PNG to JPG, etc.
      Parameters:
      srcImageFile - The source image file.
      targetImageFile - The destination image file.
    • convert

      public static void convert(InputStream srcStream, String formatName, OutputStream targetStream)
      Converts the image type. This method does not close the streams.
      Parameters:
      srcStream - The source image stream.
      formatName - A string containing the informal format name, e.g., "JPG", "JPEG", "GIF".
      targetStream - The destination image output stream.
    • convert

      public static void convert(Image srcImage, String formatName, ImageOutputStream targetImageStream)
      Converts the image type. This method does not close the streams. Remember to manually call flush(Image) to release resources.
      Parameters:
      srcImage - The source image.
      formatName - A string containing the informal format name, e.g., "JPG", "JPEG", "GIF".
      targetImageStream - The destination image output stream.
    • gray

      public static void gray(File srcImageFile, File targetImageFile)
      Converts a color image to grayscale.
      Parameters:
      srcImageFile - The source image file.
      targetImageFile - The destination file for the grayscale image.
    • gray

      public static void gray(InputStream srcStream, OutputStream targetStream)
      Converts a color image to grayscale. This method does not close the streams.
      Parameters:
      srcStream - The source image stream.
      targetStream - The destination image stream.
    • gray

      public static void gray(ImageInputStream srcStream, ImageOutputStream targetStream)
      Converts a color image to grayscale. This method does not close the streams.
      Parameters:
      srcStream - The source ImageInputStream.
      targetStream - The destination ImageOutputStream.
    • gray

      public static void gray(Image srcImage, File outFile)
      Converts a color image to grayscale. Remember to manually call flush(Image) to release resources.
      Parameters:
      srcImage - The source image.
      outFile - The destination file.
    • gray

      public static void gray(Image srcImage, OutputStream out)
      Converts a color image to grayscale. This method does not close the output stream. Remember to manually call flush(Image) to release resources.
      Parameters:
      srcImage - The source image.
      out - The destination output stream.
    • gray

      public static void gray(Image srcImage, ImageOutputStream targetImageStream) throws org.miaixz.bus.core.lang.exception.InternalException
      Converts a color image to grayscale. This method does not close the output stream. Remember to manually call flush(Image) to release resources.
      Parameters:
      srcImage - The source image.
      targetImageStream - The destination image stream.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • gray

      public static Image gray(Image srcImage)
      Converts a color image to grayscale. Remember to manually call flush(Image) to release resources.
      Parameters:
      srcImage - The source image.
      Returns:
      The grayscaled Image.
    • binary

      public static void binary(File srcImageFile, File targetImageFile)
      Converts a color image to a binary (black and white) image. The output format is determined by the destination file's extension.
      Parameters:
      srcImageFile - The source image file.
      targetImageFile - The destination file for the binary image.
    • binary

      public static void binary(InputStream srcStream, OutputStream targetStream, String imageType)
      Converts a color image to a binary (black and white) image. This method does not close the streams.
      Parameters:
      srcStream - The source image stream.
      targetStream - The destination image stream.
      imageType - The image format (extension).
    • binary

      public static void binary(ImageInputStream srcStream, ImageOutputStream targetStream, String imageType)
      Converts a color image to a binary (black and white) image. This method does not close the streams.
      Parameters:
      srcStream - The source ImageInputStream.
      targetStream - The destination ImageOutputStream.
      imageType - The image format (extension).
    • binary

      public static void binary(Image srcImage, File outFile)
      Converts a color image to a binary (black and white) image. The output format is determined by the destination file's extension. Remember to manually call flush(Image) to release resources.
      Parameters:
      srcImage - The source image.
      outFile - The destination file.
    • binary

      public static void binary(Image srcImage, OutputStream out, String imageType)
      Converts a color image to a binary (black and white) image. The output format is JPG. This method does not close the output stream. Remember to manually call flush(Image) to release resources.
      Parameters:
      srcImage - The source image.
      out - The destination output stream.
      imageType - The image format (extension).
    • binary

      public static void binary(Image srcImage, ImageOutputStream targetImageStream, String imageType) throws org.miaixz.bus.core.lang.exception.InternalException
      Converts a color image to a binary (black and white) image. The output format is JPG. This method does not close the output stream. Remember to manually call flush(Image) to release resources.
      Parameters:
      srcImage - The source image.
      targetImageStream - The destination image stream.
      imageType - The image format (extension).
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • binary

      public static Image binary(Image srcImage)
      Converts a color image to a binary (black and white) image. Remember to manually call flush(Image) to release resources.
      Parameters:
      srcImage - The source image.
      Returns:
      The binary Image.
    • pressText

      public static void pressText(File imageFile, File destFile, ImageText pressText)
      Adds a text watermark to an image.
      Parameters:
      imageFile - The source image file.
      destFile - The destination file for the watermarked image.
      pressText - The text watermark information.
    • pressText

      public static void pressText(InputStream srcStream, OutputStream targetStream, ImageText pressText)
      Adds a text watermark to an image. This method does not close the streams.
      Parameters:
      srcStream - The source image stream.
      targetStream - The destination image stream.
      pressText - The text watermark information.
    • pressText

      public static void pressText(Image srcImage, File destFile, ImageText pressText) throws org.miaixz.bus.core.lang.exception.InternalException
      Adds a text watermark to an image. This method does not close the output stream. Remember to manually call flush(Image) to release resources.
      Parameters:
      srcImage - The source image.
      destFile - The destination file.
      pressText - The text watermark information.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • pressText

      public static void pressText(Image srcImage, OutputStream to, ImageText pressText) throws org.miaixz.bus.core.lang.exception.InternalException
      Adds a text watermark to an image. This method does not close the output stream. Remember to manually call flush(Image) to release resources.
      Parameters:
      srcImage - The source image.
      to - The destination output stream.
      pressText - The text watermark information.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • pressText

      public static void pressText(Image srcImage, ImageOutputStream targetImageStream, ImageText pressText) throws org.miaixz.bus.core.lang.exception.InternalException
      Adds a text watermark to an image. This method does not close the output stream. Remember to manually call flush(Image) to release resources.
      Parameters:
      srcImage - The source image.
      targetImageStream - The destination image stream.
      pressText - The text watermark information.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • pressText

      public static Image pressText(Image srcImage, ImageText pressText)
      Adds a text watermark to an image. This method does not close the output stream. Remember to manually call flush(Image) to release resources.
      Parameters:
      srcImage - The source image.
      pressText - The text watermark information.
      Returns:
      The watermarked image.
    • pressTextFull

      public static Image pressTextFull(Image srcImage, String pressText, Color color, Font font, int lineHeight, int degree, float alpha)
      Adds a full-screen text watermark to an image. This method does not close the output stream.
      Parameters:
      srcImage - The source image. Remember to manually call flush(Image) to release resources.
      pressText - The watermark text.
      color - The font color of the watermark.
      font - The Font information. If null, a default font is used.
      lineHeight - The line height.
      degree - The rotation angle in degrees. Positive values are clockwise, negative are counter-clockwise from the origin (0,0).
      alpha - The transparency, a float between 0.0 and 1.0 (inclusive).
      Returns:
      The processed image.
    • pressImage

      public static void pressImage(File srcImageFile, File targetImageFile, Image pressImg, int x, int y, float alpha)
      Adds an image watermark to an image.
      Parameters:
      srcImageFile - The source image file.
      targetImageFile - The destination file for the watermarked image.
      pressImg - The watermark image. Remember to manually call flush(Image) to release resources.
      x - The x-offset from the center.
      y - The y-offset from the center.
      alpha - The transparency, a float between 0.0 and 1.0 (inclusive).
    • pressImage

      public static void pressImage(InputStream srcStream, OutputStream targetStream, Image pressImg, int x, int y, float alpha)
      Adds an image watermark to an image. This method does not close the streams.
      Parameters:
      srcStream - The source image stream.
      targetStream - The destination image stream.
      pressImg - The watermark image, which can be read using ImageIO.read(File).
      x - The x-offset from the center.
      y - The y-offset from the center.
      alpha - The transparency, a float between 0.0 and 1.0 (inclusive).
    • pressImage

      public static void pressImage(Image srcImage, File outFile, Image pressImg, int x, int y, float alpha) throws org.miaixz.bus.core.lang.exception.InternalException
      Adds an image watermark to an image. This method does not close the output stream. Remember to manually call flush(Image) to release resources.
      Parameters:
      srcImage - The source image.
      outFile - The destination file.
      pressImg - The watermark image, which can be read using ImageIO.read(File).
      x - The x-offset from the center.
      y - The y-offset from the center.
      alpha - The transparency, a float between 0.0 and 1.0 (inclusive).
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • pressImage

      public static void pressImage(Image srcImage, OutputStream out, Image pressImg, int x, int y, float alpha) throws org.miaixz.bus.core.lang.exception.InternalException
      Adds an image watermark to an image, writing the output in JPG format. This method does not close the output stream. Remember to manually call flush(Image) to release resources.
      Parameters:
      srcImage - The source image.
      out - The destination output stream.
      pressImg - The watermark image, which can be read using ImageIO.read(File).
      x - The x-offset from the center.
      y - The y-offset from the center.
      alpha - The transparency, a float between 0.0 and 1.0 (inclusive).
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • pressImage

      public static void pressImage(Image srcImage, ImageOutputStream targetImageStream, Image pressImg, int x, int y, float alpha) throws org.miaixz.bus.core.lang.exception.InternalException
      Adds an image watermark to an image, writing the output in JPG format. This method does not close the output stream. Remember to manually call flush(Image) to release resources.
      Parameters:
      srcImage - The source image.
      targetImageStream - The destination image stream.
      pressImg - The watermark image, which can be read using ImageIO.read(File).
      x - The x-offset from the center.
      y - The y-offset from the center.
      alpha - The transparency, a float between 0.0 and 1.0 (inclusive).
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • pressImage

      public static Image pressImage(Image srcImage, Image pressImg, int x, int y, float alpha)
      Adds an image watermark to an image. This method does not close the output stream. Remember to manually call flush(Image) to release resources.
      Parameters:
      srcImage - The source image.
      pressImg - The watermark image, which can be read using ImageIO.read(File).
      x - The x-offset from the center.
      y - The y-offset from the center.
      alpha - The transparency, a float between 0.0 and 1.0 (inclusive).
      Returns:
      The resulting watermarked image.
    • pressImage

      public static Image pressImage(Image srcImage, Image pressImg, Rectangle rectangle, float alpha)
      Adds an image watermark to an image. This method does not close the output stream. Remember to manually call flush(Image) to release resources.
      Parameters:
      srcImage - The source image.
      pressImg - The watermark image, which can be read using ImageIO.read(File).
      rectangle - A Rectangle object specifying the x, y, width, and height of the watermark, with x and y calculated from the center of the background image.
      alpha - The transparency, a float between 0.0 and 1.0 (inclusive).
      Returns:
      The resulting watermarked image.
    • pressImageFull

      public static void pressImageFull(File imageFile, File destFile, File pressImageFile, int lineHeight, int degree, float alpha) throws org.miaixz.bus.core.lang.exception.InternalException
      Adds a full-screen image watermark to an image.
      Parameters:
      imageFile - The source image file.
      destFile - The destination file for the watermarked image.
      pressImageFile - The watermark image file.
      lineHeight - The line height.
      degree - The rotation angle of the watermark image in degrees. Positive values are clockwise, negative are counter-clockwise from the origin (0,0).
      alpha - The transparency, a float between 0.0 and 1.0 (inclusive).
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • pressImageFull

      public static void pressImageFull(InputStream srcStream, OutputStream targetStream, InputStream pressStream, int lineHeight, int degree, float alpha) throws org.miaixz.bus.core.lang.exception.InternalException
      Adds a full-screen image watermark to an image, writing in JPG format. This method does not close the streams.
      Parameters:
      srcStream - The source image stream.
      targetStream - The destination image stream.
      pressStream - The watermark image stream.
      lineHeight - The line height.
      degree - The rotation angle of the watermark image in degrees. Positive values are clockwise, negative are counter-clockwise from the origin (0,0).
      alpha - The transparency, a float between 0.0 and 1.0 (inclusive).
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • pressImageFull

      public static Image pressImageFull(Image srcImage, Image pressImage, int lineHeight, int degree, float alpha)
      Adds a full-screen image watermark to an image. This method does not close the output stream.
      Parameters:
      srcImage - The source image. Remember to manually call flush(Image) to release resources.
      pressImage - The watermark image. Remember to manually call flush(Image) to release resources.
      lineHeight - The line height.
      degree - The rotation angle of the watermark image in degrees. Positive values are clockwise, negative are counter-clockwise from the origin (0,0).
      alpha - The transparency, a float between 0.0 and 1.0 (inclusive).
      Returns:
      The processed image.
    • rotate

      public static void rotate(File imageFile, int degree, File outFile) throws org.miaixz.bus.core.lang.exception.InternalException
      Rotates an image by a specified angle. This method does not close the output stream.
      Parameters:
      imageFile - The image file to be rotated.
      degree - The rotation angle in degrees.
      outFile - The output file.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • rotate

      public static void rotate(Image image, int degree, File outFile) throws org.miaixz.bus.core.lang.exception.InternalException
      Rotates an image by a specified angle. This method does not close the output stream.
      Parameters:
      image - The image to be rotated. Remember to manually call flush(Image) to release resources.
      degree - The rotation angle in degrees.
      outFile - The output file.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • rotate

      public static void rotate(Image image, int degree, OutputStream out) throws org.miaixz.bus.core.lang.exception.InternalException
      Rotates an image by a specified angle. This method does not close the output stream.
      Parameters:
      image - The image to be rotated. Remember to manually call flush(Image) to release resources.
      degree - The rotation angle in degrees.
      out - The output stream.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • rotate

      public static void rotate(Image image, int degree, ImageOutputStream out) throws org.miaixz.bus.core.lang.exception.InternalException
      Rotates an image by a specified angle. This method does not close the output stream, and the output format is JPG.
      Parameters:
      image - The image to be rotated. Remember to manually call flush(Image) to release resources.
      degree - The rotation angle in degrees.
      out - The output image stream.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • rotate

      public static Image rotate(Image image, int degree)
      Rotates an image by a specified angle. From: http://blog.51cto.com/cping1982/130066
      Parameters:
      image - The image to be rotated. Remember to manually call flush(Image) to release resources.
      degree - The rotation angle in degrees.
      Returns:
      The rotated image.
    • flip

      public static void flip(File imageFile, File outFile) throws org.miaixz.bus.core.lang.exception.InternalException
      Flips an image horizontally.
      Parameters:
      imageFile - The image file.
      outFile - The output file.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • flip

      public static void flip(Image image, File outFile) throws org.miaixz.bus.core.lang.exception.InternalException
      Flips an image horizontally.
      Parameters:
      image - The image. Remember to manually call flush(Image) to release resources.
      outFile - The output file.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • flip

      public static void flip(Image image, OutputStream out) throws org.miaixz.bus.core.lang.exception.InternalException
      Flips an image horizontally.
      Parameters:
      image - The image. Remember to manually call flush(Image) to release resources.
      out - The output stream.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • flip

      public static void flip(Image image, ImageOutputStream out) throws org.miaixz.bus.core.lang.exception.InternalException
      Flips an image horizontally, writing in JPG format.
      Parameters:
      image - The image. Remember to manually call flush(Image) to release resources.
      out - The output stream.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • flip

      public static Image flip(Image image)
      Flips an image horizontally.
      Parameters:
      image - The image. Remember to manually call flush(Image) to release resources.
      Returns:
      The flipped image.
    • compress

      public static void compress(File imageFile, File outFile, float quality) throws org.miaixz.bus.core.lang.exception.InternalException
      Compresses an image. The output image only supports JPG format.
      Parameters:
      imageFile - The image file.
      outFile - The output file (only JPG is supported).
      quality - The compression quality, a float between 0 and 1.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • castToRenderedImage

      public static RenderedImage castToRenderedImage(Image img, String imageType)
      Converts an Image to a RenderedImage. It first attempts a direct cast; otherwise, it creates a new BufferedImage and redraws the image using BufferedImage.TYPE_INT_RGB mode.
      Parameters:
      img - The Image.
      imageType - The target image type, e.g., "jpg" or "png".
      Returns:
      A BufferedImage.
    • castToBufferedImage

      public static BufferedImage castToBufferedImage(Image img, String imageType)
      Converts an Image to a BufferedImage. It first attempts a direct cast; otherwise, it creates a new BufferedImage and redraws the image using the specified image type.
      Parameters:
      img - The Image.
      imageType - The target image type, e.g., "jpg" or "png".
      Returns:
      A BufferedImage.
    • toBufferedImage

      public static BufferedImage toBufferedImage(Image image, String imageType)
      Converts an Image to a BufferedImage. If the source image's RGB mode matches the target mode, it converts directly; otherwise, it redraws. By default, PNG images use BufferedImage.TYPE_INT_ARGB mode, while others use BufferedImage.TYPE_INT_RGB.
      Parameters:
      image - The Image.
      imageType - The target image type, e.g., "jpg" or "png".
      Returns:
      A BufferedImage.
    • toBufferedImage

      public static BufferedImage toBufferedImage(Image image, String imageType, Color backgroundColor)
      Converts an Image to a BufferedImage. If the source image's RGB mode matches the target mode, it converts directly; otherwise, it redraws. By default, PNG images use BufferedImage.TYPE_INT_ARGB mode, while others use BufferedImage.TYPE_INT_RGB.
      Parameters:
      image - The Image.
      imageType - The target image type, e.g., "jpg" or "png".
      backgroundColor - The background color Color. null indicates the default background (black or transparent).
      Returns:
      A BufferedImage.
    • toBufferedImage

      public static BufferedImage toBufferedImage(Image image, int imageType)
      Converts an Image to a BufferedImage. If the source image's RGB mode matches the target mode, it converts directly; otherwise, it redraws.
      Parameters:
      image - The Image.
      imageType - The target image type, a constant from BufferedImage (e.g., for grayscale).
      Returns:
      A BufferedImage.
    • toBufferedImage

      public static BufferedImage toBufferedImage(Image image, int imageType, Color backgroundColor)
      Converts an Image to a BufferedImage. If the source image's RGB mode matches the target mode, it converts directly; otherwise, it redraws.
      Parameters:
      image - The Image.
      imageType - The target image type, a constant from BufferedImage (e.g., for grayscale).
      backgroundColor - The background color Color. null indicates the default background (black or transparent).
      Returns:
      A BufferedImage.
    • copyImage

      public static BufferedImage copyImage(Image img, int imageType)
      Creates a new copy of an existing Image.
      Parameters:
      img - The Image.
      imageType - The target image type, a constant from BufferedImage.
      Returns:
      A BufferedImage.
      See Also:
    • copyImage

      public static BufferedImage copyImage(Image img, int imageType, Color backgroundColor)
      Creates a new copy of an existing Image.
      Parameters:
      img - The Image.
      imageType - The target image type, a constant from BufferedImage.
      backgroundColor - The background color, or null for the default (black or transparent).
      Returns:
      A BufferedImage.
      See Also:
    • toImage

      public static BufferedImage toImage(String base64) throws org.miaixz.bus.core.lang.exception.InternalException
      Converts a Base64 encoded image string to a BufferedImage.
      Parameters:
      base64 - The Base64 representation of the image.
      Returns:
      A BufferedImage.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • toImage

      public static BufferedImage toImage(byte[] imageBytes) throws org.miaixz.bus.core.lang.exception.InternalException
      Converts image bytes to a BufferedImage.
      Parameters:
      imageBytes - The image bytes.
      Returns:
      A BufferedImage.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • toStream

      public static ByteArrayInputStream toStream(Image image, String imageType)
      Converts an Image object to an InputStream.
      Parameters:
      image - The image object.
      imageType - The image type.
      Returns:
      The Base64 string representation.
    • toBase64DataUri

      public static String toBase64DataUri(Image image, String imageType)
      Converts an Image object to a Base64 Data URI, in the format: data:image/[imageType];base64,[data].
      Parameters:
      image - The image object.
      imageType - The image type.
      Returns:
      The Base64 string representation.
    • toBase64

      public static String toBase64(Image image, String imageType)
      Converts an Image object to a Base64 string.
      Parameters:
      image - The image object.
      imageType - The image type.
      Returns:
      The Base64 string representation.
    • toBytes

      public static byte[] toBytes(Image image, String imageType)
      Converts an Image object to a byte array.
      Parameters:
      image - The image object.
      imageType - The image type.
      Returns:
      The Base64 string representation.
    • createCompatibleImage

      public static BufferedImage createCompatibleImage(int width, int height, int transparency)
      Creates a BufferedImage compatible with the current device's color model.
      Parameters:
      width - The width.
      height - The height.
      transparency - The transparency mode, see Transparency.
      Returns:
      A BufferedImage.
    • createTransparentImage

      public static void createTransparentImage(String text, Font font, Color fontColor, ImageOutputStream out) throws org.miaixz.bus.core.lang.exception.InternalException
      Creates a PNG image with a transparent background from text.
      Parameters:
      text - The text.
      font - The Font.
      fontColor - The font color (defaults to black).
      out - The image output destination.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • createImage

      public static void createImage(String text, Font font, Color backgroundColor, Color fontColor, ImageOutputStream out) throws org.miaixz.bus.core.lang.exception.InternalException
      Creates a PNG image from text.
      Parameters:
      text - The text.
      font - The Font.
      backgroundColor - The background color (defaults to transparent).
      fontColor - The font color (defaults to black).
      out - The image output destination.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • createImage

      public static BufferedImage createImage(String text, Font font, Color backgroundColor, Color fontColor, int imageType) throws org.miaixz.bus.core.lang.exception.InternalException
      Creates an image from text.
      Parameters:
      text - The text.
      font - The Font.
      backgroundColor - The background color (defaults to transparent).
      fontColor - The font color (defaults to black).
      imageType - The image type, see BufferedImage.
      Returns:
      The image.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • writeJpg

      public static void writeJpg(Image image, ImageOutputStream targetImageStream) throws org.miaixz.bus.core.lang.exception.InternalException
      Writes an image in JPG format.
      Parameters:
      image - The Image.
      targetImageStream - The target output stream.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • writePng

      public static void writePng(Image image, ImageOutputStream targetImageStream) throws org.miaixz.bus.core.lang.exception.InternalException
      Writes an image in PNG format.
      Parameters:
      image - The Image. Remember to manually call flush(Image) to release resources.
      targetImageStream - The target output stream.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • writeJpg

      public static void writeJpg(Image image, OutputStream out) throws org.miaixz.bus.core.lang.exception.InternalException
      Writes an image in JPG format.
      Parameters:
      image - The Image. Remember to manually call flush(Image) to release resources.
      out - The target output stream.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • writePng

      public static void writePng(Image image, OutputStream out) throws org.miaixz.bus.core.lang.exception.InternalException
      Writes an image in PNG format.
      Parameters:
      image - The Image. Remember to manually call flush(Image) to release resources.
      out - The target output stream.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • write

      public static void write(ImageInputStream srcStream, String formatName, ImageOutputStream targetStream)
      Writes an image in the specified format. This method does not close the streams.
      Parameters:
      srcStream - The source image stream.
      formatName - A string containing the informal format name, e.g., "JPG", "JPEG", "GIF".
      targetStream - The target image output stream.
    • write

      public static void write(Image image, String imageType, OutputStream out) throws org.miaixz.bus.core.lang.exception.InternalException
      Writes an image in the specified format. This method does not close the stream.
      Parameters:
      image - The Image. Remember to manually call flush(Image) to release resources.
      imageType - The image type (extension).
      out - The target output stream.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • write

      public static void write(Image image, File destFile) throws org.miaixz.bus.core.lang.exception.InternalException
      Writes an image in the format corresponding to the destination file's extension.
      Parameters:
      image - The Image. Remember to manually call flush(Image) to release resources.
      destFile - The destination file.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • write

      public static void write(Image image, String imageType, ImageOutputStream targetImageStream) throws org.miaixz.bus.core.lang.exception.InternalException
      Writes an image in the specified format. This method does not close the stream.
      Parameters:
      image - The Image. Remember to manually call flush(Image) to release resources.
      imageType - The image type (extension).
      targetImageStream - The target output stream.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • write

      public static void write(Image image, String imageType, ImageOutputStream out, float quality, Color backgroundColor) throws org.miaixz.bus.core.lang.exception.InternalException
      Writes an image in the specified format.
      Parameters:
      image - The Image. Remember to manually call flush(Image) to release resources.
      imageType - The image type (extension), or null for RGB mode (JPG).
      out - The target output stream.
      quality - The quality, a float between 0 and 1 for compression, or any other number for no compression.
      backgroundColor - The background color Color.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • write

      public static void write(Image image, String imageType, ImageOutputStream output, float quality)
      Writes an image to an output stream using an ImageWriter.
      Parameters:
      image - The image. Remember to manually call flush(Image) to release resources.
      imageType - The image type.
      output - The ImageOutputStream.
      quality - The quality, a float between 0 and 1 for compression, or any other number for no compression.
    • getWriter

      public static ImageWriter getWriter(Image img, String formatName)
      Gets the appropriate ImageWriter for a given image and format. Returns null if no suitable writer is found.
      Parameters:
      img - The Image. Remember to manually call flush(Image) to release resources.
      formatName - The image format, e.g., "jpg", "png". If null, "jpg" is used by default.
      Returns:
      An ImageWriter.
    • getWriter

      public static ImageWriter getWriter(String formatName)
      Gets the appropriate ImageWriter for a given format or extension. Returns null if no suitable writer is found.
      Parameters:
      formatName - The image format or extension, e.g., "jpg", "png". If null, "jpg" is used by default.
      Returns:
      An ImageWriter.
    • read

      public static BufferedImage read(String imageFilePath)
      Reads an image from a file. Use an absolute path; relative paths are resolved relative to the classpath.
      Parameters:
      imageFilePath - The path to the image file.
      Returns:
      The image.
    • read

      public static BufferedImage read(File imageFile)
      Reads an image from a file.
      Parameters:
      imageFile - The image file.
      Returns:
      The image.
    • read

      public static BufferedImage read(org.miaixz.bus.core.io.resource.Resource resource)
      Reads an image from a Resource.
      Parameters:
      resource - The image resource.
      Returns:
      The image.
    • read

      public static BufferedImage read(InputStream imageStream)
      Reads an image from a stream.
      Parameters:
      imageStream - The image stream.
      Returns:
      The image.
    • read

      public static BufferedImage read(ImageInputStream imageStream)
      Reads an image from an image stream.
      Parameters:
      imageStream - The image stream.
      Returns:
      The image.
    • read

      public static BufferedImage read(URL imageUrl)
      Reads an image from a URL.
      Parameters:
      imageUrl - The image URL.
      Returns:
      The image.
    • getReader

      public static ImageReader getReader(String type)
      Gets an ImageReader.
      Parameters:
      type - The image file type, e.g., "jpeg" or "tiff".
      Returns:
      An ImageReader.
    • getWidthAndHeight

      public static org.miaixz.bus.core.lang.tuple.Pair<Integer,Integer> getWidthAndHeight(InputStream imageStream, String type)
      Gets the width and height of an image from an ImageInputStream.
      Parameters:
      imageStream - The InputStream.
      type - The image type.
      Returns:
      The width and height as a Pair.
    • getWidthAndHeight

      public static org.miaixz.bus.core.lang.tuple.Pair<Integer,Integer> getWidthAndHeight(ImageInputStream imageStream, String type)
      Gets the width and height of an image from an ImageInputStream.
      Parameters:
      imageStream - The ImageInputStream.
      type - The image type.
      Returns:
      The width and height as a Pair.
    • getImage

      public static Image getImage(URL url)
      Gets or reads an image object from a URL.
      Parameters:
      url - The URL.
      Returns:
      An Image.
    • getImageOutputStream

      public static ImageOutputStream getImageOutputStream(OutputStream out) throws org.miaixz.bus.core.lang.exception.InternalException
      Parameters:
      out - The OutputStream.
      Returns:
      An ImageOutputStream.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • getImageOutputStream

      public static ImageOutputStream getImageOutputStream(File outFile) throws org.miaixz.bus.core.lang.exception.InternalException
      Parameters:
      outFile - The File.
      Returns:
      An ImageOutputStream.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • getImageInputStream

      public static ImageInputStream getImageInputStream(InputStream in) throws org.miaixz.bus.core.lang.exception.InternalException
      Parameters:
      in - The InputStream.
      Returns:
      An ImageInputStream.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • getPointBaseCentre

      public static Point getPointBaseCentre(Rectangle rectangle, int backgroundWidth, int backgroundHeight)
      Gets the corrected coordinates of a rectangle, making them relative to the center of the background.
      Parameters:
      rectangle - The rectangle.
      backgroundWidth - The reference width (background width).
      backgroundHeight - The reference height (background height).
      Returns:
      The corrected Point.
    • backgroundRemoval

      public static void backgroundRemoval(String inputPath, String outputPath, int tolerance)
      Removes the background of an image, converting a solid-color background to transparent.
      Parameters:
      inputPath - The path to the image to process.
      outputPath - The path to save the output image.
      tolerance - The tolerance value (0-255) for color matching.
    • backgroundRemoval

      public static void backgroundRemoval(File input, File output, int tolerance)
      Removes the background of an image, converting a solid-color background to transparent.
      Parameters:
      input - The image file to process.
      output - The output file.
      tolerance - The tolerance value (0-255) for color matching.
    • backgroundRemoval

      public static void backgroundRemoval(File input, File output, Color override, int tolerance)
      Removes the background of an image, replacing it with a specified color or transparency.
      Parameters:
      input - The image file to process.
      output - The output file.
      override - The color to replace the background with. If null, the background becomes transparent.
      tolerance - The tolerance value (0-255) for color matching.
    • backgroundRemoval

      public static BufferedImage backgroundRemoval(BufferedImage bufferedImage, Color override, int tolerance)
      Removes the background of an image, replacing it with a specified color or transparency.
      Parameters:
      bufferedImage - The image stream to process.
      override - The color to replace the background with. If null, the background becomes transparent.
      tolerance - The tolerance value (0-255) for color matching.
      Returns:
      The processed image stream.
    • backgroundRemoval

      public static BufferedImage backgroundRemoval(ByteArrayOutputStream outputStream, Color override, int tolerance)
      Removes the background of an image, replacing it with a specified color or transparency.
      Parameters:
      outputStream - The byte array output stream of the image to process.
      override - The color to replace the background with. If null, the background becomes transparent.
      tolerance - The tolerance value (0-255) for color matching.
      Returns:
      The processed image stream.
    • colorConvert

      public static BufferedImage colorConvert(ColorSpace colorSpace, BufferedImage image)
      Converts the color space of an image (e.g., to grayscale).
      Parameters:
      colorSpace - The target color space.
      image - The image to convert.
      Returns:
      The converted image.
    • transform

      public static BufferedImage transform(AffineTransform xform, BufferedImage image)
      Applies an affine transformation to an image (e.g., translation, scale, flip, rotation, shear).
      Parameters:
      xform - The 2D affine transform.
      image - The image to transform.
      Returns:
      The transformed image.
    • filter

      public static BufferedImage filter(BufferedImageOp op, BufferedImage image)
      Applies a filter to an image.
      Parameters:
      op - The filter operation (e.g., AffineTransformOp).
      image - The original image.
      Returns:
      The filtered image.
    • filter

      public static Image filter(ImageFilter filter, Image image)
      Applies a filter to an image using an ImageFilter.
      Parameters:
      filter - The filter implementation.
      image - The image.
      Returns:
      The filtered image.
    • flush

      public static void flush(Image image)
      Flushes and releases the resources of an Image.
      Parameters:
      image - The Image.
    • createGraphics

      public static Graphics2D createGraphics(BufferedImage image, Color color)
      Creates a Graphics2D object for a BufferedImage.
      Parameters:
      image - The BufferedImage.
      color - The background and current drawing color. If null, no background is set.
      Returns:
      A Graphics2D object.
    • getCenterY

      public static int getCenterY(Graphics g, int backgroundHeight)
      Gets the Y-coordinate for vertically centering text. This method relies on FontMetrics.
      Parameters:
      g - The Graphics2D object.
      backgroundHeight - The height of the background.
      Returns:
      The minimum height, or -1 if it cannot be determined.
    • drawStringColourful

      public static Graphics drawStringColourful(Graphics g, String text, Font font, int width, int height)
      Draws a string with random colors and default anti-aliasing.
      Parameters:
      g - The Graphics object.
      text - The string.
      font - The font.
      width - The total width for the string.
      height - The background height for the string.
      Returns:
      The Graphics object.
    • drawStringColourful

      public static Graphics drawStringColourful(Graphics g, String str, Font font, int width, int height, Color compareColor, int minColorDistance)
      Draws a string with random colors and default anti-aliasing.
      Parameters:
      g - The Graphics object.
      str - The string.
      font - The font.
      width - The total width for the string.
      height - The background height for the string.
      compareColor - A color to compare against for ensuring minimum color distance.
      minColorDistance - The minimum color distance from the compare color.
      Returns:
      The Graphics object.
    • drawString

      public static Graphics drawString(Graphics g, String text, Font font, Color color, int width, int height)
      Draws a string with default anti-aliasing.
      Parameters:
      g - The Graphics object.
      text - The string.
      font - The font.
      color - The font color, or null for random colors.
      width - The background width for the string.
      height - The background height for the string.
      Returns:
      The Graphics object.
    • drawString

      public static Graphics drawString(Graphics g, String text, Font font, Color color, Rectangle rectangle)
      Draws a string with default anti-aliasing within a specified rectangle.
      Parameters:
      g - The Graphics object.
      text - The string.
      font - The font.
      color - The font color, or null for black.
      rectangle - The rectangle defining the drawing area and position.
      Returns:
      The Graphics object.
    • drawString

      public static Graphics drawString(Graphics g, String text, Font font, Color color, Point point)
      Draws a string with default anti-aliasing.
      Parameters:
      g - The Graphics object.
      text - The string.
      font - The font.
      color - The font color, or null for black.
      point - The position to draw the string.
      Returns:
      The Graphics object.
    • drawImg

      public static Graphics drawImg(Graphics g, Image img, Point point)
      Draws an image.
      Parameters:
      g - The graphics context.
      img - The image to draw.
      point - The top-left position to draw at.
      Returns:
      The Graphics object.
    • drawImg

      public static Graphics drawImg(Graphics g, Image img, Rectangle rectangle)
      Draws an image.
      Parameters:
      g - The graphics context.
      img - The image to draw.
      rectangle - The rectangle defining the drawing area and position.
      Returns:
      The graphics object.
    • setAlpha

      public static Graphics2D setAlpha(Graphics2D g, float alpha)
      Sets the transparency of the graphics context.
      Parameters:
      g - The graphics context.
      alpha - The transparency, a float between 0.0 and 1.0 (inclusive).
      Returns:
      The graphics context.
    • getAllFonts

      public static Font[] getAllFonts()
      Gets all fonts available in the system.
      Returns:
      An array of fonts.
    • registerFont

      public static void registerFont(Font font)
      Registers a font with the system.
      Parameters:
      font - The font to register.
    • createFont

      public static Font createFont()
      Creates a default font.
      Returns:
      The default font.
    • createSansSerifFont

      public static Font createSansSerifFont(int size)
      Creates a SansSerif font.
      Parameters:
      size - The font size.
      Returns:
      The font.
    • createFont

      public static Font createFont(String name, int size)
      Creates a font with the specified name.
      Parameters:
      name - The font name.
      size - The font size.
      Returns:
      The font.
    • createFont

      public static Font createFont(File fontFile)
      Creates a font from a file.
      Parameters:
      fontFile - The font file.
      Returns:
      A Font.
    • createFont

      public static Font createFont(InputStream fontStream)
      Creates a font from a stream.
      Parameters:
      fontStream - The font stream.
      Returns:
      A Font.
    • getDimension

      public static Dimension getDimension(FontMetrics metrics, String text)
      Gets the dimensions of a string with a given font.
      Parameters:
      metrics - The FontMetrics.
      text - The string.
      Returns:
      The dimensions.
    • getRectangle

      public static Rectangle2D getRectangle(String text, Font font)
      Gets the bounding rectangle of a string with a given font.
      Parameters:
      text - The string (must not be null).
      font - The font (must not be null).
      Returns:
      A Rectangle2D.