java.lang.Object
org.miaixz.bus.extra.image.ImageKit
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 Summary
FieldsModifier and TypeFieldDescriptionstatic final StringBitmap, the standard image file format in Windows OS: BMPstatic final StringGraphics Interchange Format: GIFstatic final StringJoint Photographic Experts Group: JPEGstatic final StringJoint Photographic Experts Group: JPGstatic final StringPortable Network Graphics: PNGstatic final StringPhotoshop's proprietary format: PSD -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic BufferedImagebackgroundRemoval(BufferedImage bufferedImage, Color override, int tolerance) Removes the background of an image, replacing it with a specified color or transparency.static BufferedImagebackgroundRemoval(ByteArrayOutputStream outputStream, Color override, int tolerance) Removes the background of an image, replacing it with a specified color or transparency.static voidbackgroundRemoval(File input, File output, int tolerance) Removes the background of an image, converting a solid-color background to transparent.static voidbackgroundRemoval(File input, File output, Color override, int tolerance) Removes the background of an image, replacing it with a specified color or transparency.static voidbackgroundRemoval(String inputPath, String outputPath, int tolerance) Removes the background of an image, converting a solid-color background to transparent.static ImageConverts a color image to a binary (black and white) image.static voidConverts a color image to a binary (black and white) image.static voidbinary(Image srcImage, OutputStream out, String imageType) Converts a color image to a binary (black and white) image.static voidbinary(Image srcImage, ImageOutputStream targetImageStream, String imageType) Converts a color image to a binary (black and white) image.static voidConverts a color image to a binary (black and white) image.static voidbinary(InputStream srcStream, OutputStream targetStream, String imageType) Converts a color image to a binary (black and white) image.static voidbinary(ImageInputStream srcStream, ImageOutputStream targetStream, String imageType) Converts a color image to a binary (black and white) image.static BufferedImagecastToBufferedImage(Image img, String imageType) Converts anImageto aBufferedImage.static RenderedImagecastToRenderedImage(Image img, String imageType) Converts anImageto aRenderedImage.static BufferedImagecolorConvert(ColorSpace colorSpace, BufferedImage image) Converts the color space of an image (e.g., to grayscale).static voidCompresses an image.static voidconvert(Image srcImage, String formatName, ImageOutputStream targetImageStream) Converts the image type.static voidConverts the image type, e.g., GIF to JPG, GIF to PNG, PNG to JPG, etc.static voidconvert(InputStream srcStream, String formatName, OutputStream targetStream) Converts the image type.static BufferedImageCreates a new copy of an existingImage.static BufferedImageCreates a new copy of an existingImage.static BufferedImagecreateCompatibleImage(int width, int height, int transparency) Creates aBufferedImagecompatible with the current device's color model.static FontCreates a default font.static FontcreateFont(File fontFile) Creates a font from a file.static FontcreateFont(InputStream fontStream) Creates a font from a stream.static FontcreateFont(String name, int size) Creates a font with the specified name.static Graphics2DcreateGraphics(BufferedImage image, Color color) Creates aGraphics2Dobject for aBufferedImage.static BufferedImagecreateImage(String text, Font font, Color backgroundColor, Color fontColor, int imageType) Creates an image from text.static voidcreateImage(String text, Font font, Color backgroundColor, Color fontColor, ImageOutputStream out) Creates a PNG image from text.static FontcreateSansSerifFont(int size) Creates a SansSerif font.static voidcreateTransparentImage(String text, Font font, Color fontColor, ImageOutputStream out) Creates a PNG image with a transparent background from text.static ImageCrops a circular area from the image.static ImageCrops a circular area from the image.static ImageCrops an image using the specified coordinates and dimensions.static voidCrops an image using the specified coordinates and dimensions.static voidcut(Image srcImage, OutputStream out, Rectangle rectangle) Crops an image using the specified coordinates and dimensions.static voidcut(Image srcImage, ImageOutputStream targetImageStream, Rectangle rectangle) Crops an image using the specified coordinates and dimensions.static voidCrops an image using the specified coordinates and dimensions.static voidcut(InputStream srcStream, OutputStream targetStream, Rectangle rectangle) Crops an image using the specified coordinates and dimensions.static voidcut(ImageInputStream srcStream, ImageOutputStream targetStream, Rectangle rectangle) Crops an image using the specified coordinates and dimensions.static GraphicsDraws an image.static GraphicsDraws an image.static GraphicsdrawString(Graphics g, String text, Font font, Color color, int width, int height) Draws a string with default anti-aliasing.static GraphicsDraws a string with default anti-aliasing.static GraphicsDraws a string with default anti-aliasing within a specified rectangle.static GraphicsdrawStringColourful(Graphics g, String text, Font font, int width, int height) Draws a string with random colors and default anti-aliasing.static GraphicsdrawStringColourful(Graphics g, String str, Font font, int width, int height, Color compareColor, int minColorDistance) Draws a string with random colors and default anti-aliasing.static BufferedImagefilter(BufferedImageOp op, BufferedImage image) Applies a filter to an image.static Imagefilter(ImageFilter filter, Image image) Applies a filter to an image using anImageFilter.static ImageFlips an image horizontally.static voidFlips an image horizontally.static voidflip(Image image, OutputStream out) Flips an image horizontally.static voidflip(Image image, ImageOutputStream out) Flips an image horizontally, writing in JPG format.static voidFlips an image horizontally.static voidFlushes and releases the resources of anImage.static Font[]Gets all fonts available in the system.static intgetCenterY(Graphics g, int backgroundHeight) Gets the Y-coordinate for vertically centering text.static DimensiongetDimension(FontMetrics metrics, String text) Gets the dimensions of a string with a given font.static ImageGets or reads an image object from a URL.static ImageInputStreamGets anImageInputStream.static ImageOutputStreamgetImageOutputStream(File outFile) Gets anImageOutputStream.static ImageOutputStreamGets anImageOutputStream.static PointgetPointBaseCentre(Rectangle rectangle, int backgroundWidth, int backgroundHeight) Gets the corrected coordinates of a rectangle, making them relative to the center of the background.static ImageReaderGets anImageReader.static Rectangle2DgetRectangle(String text, Font font) Gets the bounding rectangle of a string with a given font.getWidthAndHeight(InputStream imageStream, String type) Gets the width and height of an image from anImageInputStream.getWidthAndHeight(ImageInputStream imageStream, String type) Gets the width and height of an image from anImageInputStream.static ImageWriterGets the appropriateImageWriterfor a given image and format.static ImageWriterGets the appropriateImageWriterfor a given format or extension.static ImageConverts a color image to grayscale.static voidConverts a color image to grayscale.static voidgray(Image srcImage, OutputStream out) Converts a color image to grayscale.static voidgray(Image srcImage, ImageOutputStream targetImageStream) Converts a color image to grayscale.static voidConverts a color image to grayscale.static voidgray(InputStream srcStream, OutputStream targetStream) Converts a color image to grayscale.static voidgray(ImageInputStream srcStream, ImageOutputStream targetStream) Converts a color image to grayscale.static ImagepressImage(Image srcImage, Image pressImg, int x, int y, float alpha) Adds an image watermark to an image.static ImagepressImage(Image srcImage, Image pressImg, Rectangle rectangle, float alpha) Adds an image watermark to an image.static voidpressImage(Image srcImage, File outFile, Image pressImg, int x, int y, float alpha) Adds an image watermark to an image.static voidpressImage(Image srcImage, OutputStream out, Image pressImg, int x, int y, float alpha) Adds an image watermark to an image, writing the output in JPG format.static voidpressImage(Image srcImage, ImageOutputStream targetImageStream, Image pressImg, int x, int y, float alpha) Adds an image watermark to an image, writing the output in JPG format.static voidpressImage(File srcImageFile, File targetImageFile, Image pressImg, int x, int y, float alpha) Adds an image watermark to an image.static voidpressImage(InputStream srcStream, OutputStream targetStream, Image pressImg, int x, int y, float alpha) Adds an image watermark to an image.static ImagepressImageFull(Image srcImage, Image pressImage, int lineHeight, int degree, float alpha) Adds a full-screen image watermark to an image.static voidpressImageFull(File imageFile, File destFile, File pressImageFile, int lineHeight, int degree, float alpha) Adds a full-screen image watermark to an image.static voidpressImageFull(InputStream srcStream, OutputStream targetStream, InputStream pressStream, int lineHeight, int degree, float alpha) Adds a full-screen image watermark to an image, writing in JPG format.static voidAdds a text watermark to an image.static voidpressText(Image srcImage, OutputStream to, ImageText pressText) Adds a text watermark to an image.static voidpressText(Image srcImage, ImageOutputStream targetImageStream, ImageText pressText) Adds a text watermark to an image.static ImageAdds a text watermark to an image.static voidAdds a text watermark to an image.static voidpressText(InputStream srcStream, OutputStream targetStream, ImageText pressText) Adds a text watermark to an image.static ImagepressTextFull(Image srcImage, String pressText, Color color, Font font, int lineHeight, int degree, float alpha) Adds a full-screen text watermark to an image.static BufferedImageReads an image from a file.static BufferedImageread(InputStream imageStream) Reads an image from a stream.static BufferedImageReads an image from a file.static BufferedImageReads an image from a URL.static BufferedImageread(ImageInputStream imageStream) Reads an image from an image stream.static BufferedImageread(org.miaixz.bus.core.io.resource.Resource resource) Reads an image from aResource.static voidregisterFont(Font font) Registers a font with the system.static ImageRotates an image by a specified angle.static voidRotates an image by a specified angle.static voidrotate(Image image, int degree, OutputStream out) Rotates an image by a specified angle.static voidrotate(Image image, int degree, ImageOutputStream out) Rotates an image by a specified angle.static voidRotates an image by a specified angle.static ImageScales an image by a given ratio.static ImageScales an image to a specific width and height.static ImageScales an image to a specific width and height.static voidScales an image by a given ratio.static voidscale(Image srcImg, OutputStream out, float scale) Scales an image by a given ratio.static voidscale(Image srcImg, ImageOutputStream destImageStream, float scale) Scales an image by a given ratio.static voidscale(Image srcImage, ImageOutputStream targetImageStream, int width, int height, Color fixedColor) Scales an image to a specific width and height.static voidScales an image by a given ratio.static voidScales an image to a specific width and height.static voidscale(InputStream srcStream, OutputStream targetStream, float scale) Scales an image by a given ratio.static voidscale(InputStream srcStream, OutputStream targetStream, int width, int height, Color fixedColor) Scales an image to a specific width and height.static voidscale(ImageInputStream srcStream, ImageOutputStream targetStream, float scale) Scales an image by a given ratio.static voidscale(ImageInputStream srcStream, ImageOutputStream targetStream, int width, int height, Color fixedColor) Scales an image to a specific width and height.static Graphics2DsetAlpha(Graphics2D g, float alpha) Sets the transparency of the graphics context.static voidSlices an image into smaller pieces of a specified width and height.static voidSlices an image into smaller pieces of a specified width and height.static voidsliceByRowsAndCols(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.static voidsliceByRowsAndCols(File srcImageFile, File targetDir, String formatName, int rows, int cols) Slices an image into a specified number of rows and columns.static StringConverts anImageobject to a Base64 string.static StringtoBase64DataUri(Image image, String imageType) Converts anImageobject to a Base64 Data URI, in the format: data:image/[imageType];base64,[data].static BufferedImagetoBufferedImage(Image image, int imageType) Converts anImageto aBufferedImage.static BufferedImagetoBufferedImage(Image image, int imageType, Color backgroundColor) Converts anImageto aBufferedImage.static BufferedImagetoBufferedImage(Image image, String imageType) Converts anImageto aBufferedImage.static BufferedImagetoBufferedImage(Image image, String imageType, Color backgroundColor) Converts anImageto aBufferedImage.static byte[]Converts anImageobject to a byte array.static BufferedImagetoImage(byte[] imageBytes) Converts image bytes to aBufferedImage.static BufferedImageConverts a Base64 encoded image string to aBufferedImage.static ByteArrayInputStreamConverts anImageobject to anInputStream.static BufferedImagetransform(AffineTransform xform, BufferedImage image) Applies an affine transformation to an image (e.g., translation, scale, flip, rotation, shear).static voidWrites an image in the format corresponding to the destination file's extension.static voidwrite(Image image, String imageType, OutputStream out) Writes an image in the specified format.static voidwrite(Image image, String imageType, ImageOutputStream targetImageStream) Writes an image in the specified format.static voidwrite(Image image, String imageType, ImageOutputStream output, float quality) Writes an image to an output stream using anImageWriter.static voidwrite(Image image, String imageType, ImageOutputStream out, float quality, Color backgroundColor) Writes an image in the specified format.static voidwrite(ImageInputStream srcStream, String formatName, ImageOutputStream targetStream) Writes an image in the specified format.static voidwriteJpg(Image image, OutputStream out) Writes an image in JPG format.static voidwriteJpg(Image image, ImageOutputStream targetImageStream) Writes an image in JPG format.static voidwritePng(Image image, OutputStream out) Writes an image in PNG format.static voidwritePng(Image image, ImageOutputStream targetImageStream) Writes an image in PNG format.
-
Field Details
-
IMAGE_TYPE_GIF
Graphics Interchange Format: GIF- See Also:
-
IMAGE_TYPE_JPG
Joint Photographic Experts Group: JPG- See Also:
-
IMAGE_TYPE_JPEG
Joint Photographic Experts Group: JPEG- See Also:
-
IMAGE_TYPE_BMP
Bitmap, the standard image file format in Windows OS: BMP- See Also:
-
IMAGE_TYPE_PNG
Portable Network Graphics: PNG- See Also:
-
IMAGE_TYPE_PSD
Photoshop's proprietary format: PSD- See Also:
-
-
Constructor Details
-
ImageKit
public ImageKit()
-
-
Method Details
-
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
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
Scales an image by a given ratio. The default output format is JPEG. This method does not close the streams.- Parameters:
srcStream- The sourceImageInputStream.targetStream- The destinationImageOutputStream.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 callflush(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 callflush(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 callflush(Image)to release resources.- Parameters:
srcImg- The source image.destImageStream- The destinationImageOutputStream.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
Scales an image by a given ratio. Remember to manually callflush(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
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 callflush(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, ornullfor 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, ornullfor 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 sourceImageInputStream.targetStream- The destinationImageOutputStream.width- The target width.height- The target height.fixedColor- The color to fill the background if the aspect ratio changes, ornullfor 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 callflush(Image)to release resources.- Parameters:
srcImage- The source image.targetImageStream- The destinationImageOutputStream.width- The target width.height- The target height.fixedColor- The color to fill the background if the aspect ratio changes, ornullfor no fill.- Throws:
org.miaixz.bus.core.lang.exception.InternalException- if an I/O error occurs.
-
scale
Scales an image to a specific width and height. The default output format is JPEG. Remember to manually callflush(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, ornullfor no fill.- Returns:
- The scaled
Image.
-
cut
Crops an image using the specified coordinates and dimensions.- Parameters:
srcImgFile- The source image file.targetImgFile- The destination file for the cropped image.rectangle- ARectangleobject specifying the x, y, width, and height of the crop area.
-
cut
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- ARectangleobject 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 sourceImageInputStream.targetStream- The destinationImageOutputStream.rectangle- ARectangleobject 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 callflush(Image)to release resources.- Parameters:
srcImage- The source image.destFile- The destination file for the cropped image.rectangle- ARectangleobject 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- ARectangleobject 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 callflush(Image)to release resources.- Parameters:
srcImage- The source image.targetImageStream- The destinationImageOutputStream.rectangle- ARectangleobject 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
Crops an image using the specified coordinates and dimensions. Remember to manually callflush(Image)to release resources.- Parameters:
srcImage- The source image.rectangle- ARectangleobject specifying the x, y, width, and height of the crop area.- Returns:
- The cropped
BufferedImage.
-
cut
Crops a circular area from the image. The diameter is the smaller of the image's width and height. Remember to manually callflush(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
Crops a circular area from the image. Remember to manually callflush(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
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
Slices an image into smaller pieces of a specified width and height. Remember to manually callflush(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 callflush(Image)to release resources.- Parameters:
srcImage- The source image. If not aBufferedImage, 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
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
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
Converts the image type. This method does not close the streams. Remember to manually callflush(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
Converts a color image to grayscale.- Parameters:
srcImageFile- The source image file.targetImageFile- The destination file for the grayscale image.
-
gray
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
Converts a color image to grayscale. This method does not close the streams.- Parameters:
srcStream- The sourceImageInputStream.targetStream- The destinationImageOutputStream.
-
gray
Converts a color image to grayscale. Remember to manually callflush(Image)to release resources.- Parameters:
srcImage- The source image.outFile- The destination file.
-
gray
Converts a color image to grayscale. This method does not close the output stream. Remember to manually callflush(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 callflush(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
Converts a color image to grayscale. Remember to manually callflush(Image)to release resources.- Parameters:
srcImage- The source image.- Returns:
- The grayscaled
Image.
-
binary
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
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 sourceImageInputStream.targetStream- The destinationImageOutputStream.imageType- The image format (extension).
-
binary
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 callflush(Image)to release resources.- Parameters:
srcImage- The source image.outFile- The destination file.
-
binary
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 callflush(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 callflush(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
Converts a color image to a binary (black and white) image. Remember to manually callflush(Image)to release resources.- Parameters:
srcImage- The source image.- Returns:
- The binary
Image.
-
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
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 callflush(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 callflush(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 callflush(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
Adds a text watermark to an image. This method does not close the output stream. Remember to manually callflush(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 callflush(Image)to release resources.pressText- The watermark text.color- The font color of the watermark.font- TheFontinformation. 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 callflush(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 usingImageIO.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 callflush(Image)to release resources.- Parameters:
srcImage- The source image.outFile- The destination file.pressImg- The watermark image, which can be read usingImageIO.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 callflush(Image)to release resources.- Parameters:
srcImage- The source image.out- The destination output stream.pressImg- The watermark image, which can be read usingImageIO.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 callflush(Image)to release resources.- Parameters:
srcImage- The source image.targetImageStream- The destination image stream.pressImg- The watermark image, which can be read usingImageIO.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
Adds an image watermark to an image. This method does not close the output stream. Remember to manually callflush(Image)to release resources.- Parameters:
srcImage- The source image.pressImg- The watermark image, which can be read usingImageIO.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
Adds an image watermark to an image. This method does not close the output stream. Remember to manually callflush(Image)to release resources.- Parameters:
srcImage- The source image.pressImg- The watermark image, which can be read usingImageIO.read(File).rectangle- ARectangleobject 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 callflush(Image)to release resources.pressImage- The watermark image. Remember to manually callflush(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 callflush(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 callflush(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 callflush(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
Rotates an image by a specified angle. From: http://blog.51cto.com/cping1982/130066- Parameters:
image- The image to be rotated. Remember to manually callflush(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 callflush(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 callflush(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 callflush(Image)to release resources.out- The output stream.- Throws:
org.miaixz.bus.core.lang.exception.InternalException- if an I/O error occurs.
-
flip
Flips an image horizontally.- Parameters:
image- The image. Remember to manually callflush(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
Converts anImageto aRenderedImage. It first attempts a direct cast; otherwise, it creates a newBufferedImageand redraws the image usingBufferedImage.TYPE_INT_RGBmode.- Parameters:
img- TheImage.imageType- The target image type, e.g., "jpg" or "png".- Returns:
- A
BufferedImage.
-
castToBufferedImage
Converts anImageto aBufferedImage. It first attempts a direct cast; otherwise, it creates a newBufferedImageand redraws the image using the specified image type.- Parameters:
img- TheImage.imageType- The target image type, e.g., "jpg" or "png".- Returns:
- A
BufferedImage.
-
toBufferedImage
Converts anImageto aBufferedImage. If the source image's RGB mode matches the target mode, it converts directly; otherwise, it redraws. By default, PNG images useBufferedImage.TYPE_INT_ARGBmode, while others useBufferedImage.TYPE_INT_RGB.- Parameters:
image- TheImage.imageType- The target image type, e.g., "jpg" or "png".- Returns:
- A
BufferedImage.
-
toBufferedImage
Converts anImageto aBufferedImage. If the source image's RGB mode matches the target mode, it converts directly; otherwise, it redraws. By default, PNG images useBufferedImage.TYPE_INT_ARGBmode, while others useBufferedImage.TYPE_INT_RGB.- Parameters:
image- TheImage.imageType- The target image type, e.g., "jpg" or "png".backgroundColor- The background colorColor.nullindicates the default background (black or transparent).- Returns:
- A
BufferedImage.
-
toBufferedImage
Converts anImageto aBufferedImage. If the source image's RGB mode matches the target mode, it converts directly; otherwise, it redraws.- Parameters:
image- TheImage.imageType- The target image type, a constant fromBufferedImage(e.g., for grayscale).- Returns:
- A
BufferedImage.
-
toBufferedImage
Converts anImageto aBufferedImage. If the source image's RGB mode matches the target mode, it converts directly; otherwise, it redraws.- Parameters:
image- TheImage.imageType- The target image type, a constant fromBufferedImage(e.g., for grayscale).backgroundColor- The background colorColor.nullindicates the default background (black or transparent).- Returns:
- A
BufferedImage.
-
copyImage
Creates a new copy of an existingImage.- Parameters:
img- TheImage.imageType- The target image type, a constant fromBufferedImage.- Returns:
- A
BufferedImage. - See Also:
-
copyImage
Creates a new copy of an existingImage.- Parameters:
img- TheImage.imageType- The target image type, a constant fromBufferedImage.backgroundColor- The background color, ornullfor 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 aBufferedImage.- 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 aBufferedImage.- Parameters:
imageBytes- The image bytes.- Returns:
- A
BufferedImage. - Throws:
org.miaixz.bus.core.lang.exception.InternalException- if an I/O error occurs.
-
toStream
Converts anImageobject to anInputStream.- Parameters:
image- The image object.imageType- The image type.- Returns:
- The Base64 string representation.
-
toBase64DataUri
Converts anImageobject 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
Converts anImageobject to a Base64 string.- Parameters:
image- The image object.imageType- The image type.- Returns:
- The Base64 string representation.
-
toBytes
Converts anImageobject to a byte array.- Parameters:
image- The image object.imageType- The image type.- Returns:
- The Base64 string representation.
-
createCompatibleImage
Creates aBufferedImagecompatible with the current device's color model.- Parameters:
width- The width.height- The height.transparency- The transparency mode, seeTransparency.- 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- TheFont.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- TheFont.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- TheFont.backgroundColor- The background color (defaults to transparent).fontColor- The font color (defaults to black).imageType- The image type, seeBufferedImage.- 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- TheImage.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- TheImage. Remember to manually callflush(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- TheImage. Remember to manually callflush(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- TheImage. Remember to manually callflush(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- TheImage. Remember to manually callflush(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- TheImage. Remember to manually callflush(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- TheImage. Remember to manually callflush(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- TheImage. Remember to manually callflush(Image)to release resources.imageType- The image type (extension), ornullfor 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 colorColor.- Throws:
org.miaixz.bus.core.lang.exception.InternalException- if an I/O error occurs.
-
write
Writes an image to an output stream using anImageWriter.- Parameters:
image- The image. Remember to manually callflush(Image)to release resources.imageType- The image type.output- TheImageOutputStream.quality- The quality, a float between 0 and 1 for compression, or any other number for no compression.
-
getWriter
Gets the appropriateImageWriterfor a given image and format. Returns null if no suitable writer is found.- Parameters:
img- TheImage. Remember to manually callflush(Image)to release resources.formatName- The image format, e.g., "jpg", "png". Ifnull, "jpg" is used by default.- Returns:
- An
ImageWriter.
-
getWriter
Gets the appropriateImageWriterfor a given format or extension. Returns null if no suitable writer is found.- Parameters:
formatName- The image format or extension, e.g., "jpg", "png". Ifnull, "jpg" is used by default.- Returns:
- An
ImageWriter.
-
read
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
Reads an image from a file.- Parameters:
imageFile- The image file.- Returns:
- The image.
-
read
Reads an image from aResource.- Parameters:
resource- The image resource.- Returns:
- The image.
-
read
Reads an image from a stream.- Parameters:
imageStream- The image stream.- Returns:
- The image.
-
read
Reads an image from an image stream.- Parameters:
imageStream- The image stream.- Returns:
- The image.
-
read
Reads an image from a URL.- Parameters:
imageUrl- The image URL.- Returns:
- The image.
-
getReader
Gets anImageReader.- 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 anImageInputStream.- Parameters:
imageStream- TheInputStream.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 anImageInputStream.- Parameters:
imageStream- TheImageInputStream.type- The image type.- Returns:
- The width and height as a
Pair.
-
getImage
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 Gets anImageOutputStream.- Parameters:
out- TheOutputStream.- 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 Gets anImageOutputStream.- Parameters:
outFile- TheFile.- 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 Gets anImageInputStream.- Parameters:
in- TheInputStream.- 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
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
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
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
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
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
Applies a filter to an image.- Parameters:
op- The filter operation (e.g.,AffineTransformOp).image- The original image.- Returns:
- The filtered image.
-
filter
Applies a filter to an image using anImageFilter.- Parameters:
filter- The filter implementation.image- The image.- Returns:
- The filtered image.
-
flush
Flushes and releases the resources of anImage.- Parameters:
image- TheImage.
-
createGraphics
Creates aGraphics2Dobject for aBufferedImage.- Parameters:
image- TheBufferedImage.color- The background and current drawing color. Ifnull, no background is set.- Returns:
- A
Graphics2Dobject.
-
getCenterY
Gets the Y-coordinate for vertically centering text. This method relies onFontMetrics.- Parameters:
g- TheGraphics2Dobject.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. -
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- TheGraphicsobject.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
Graphicsobject.
-
drawString
public static Graphics drawString(Graphics g, String text, Font font, Color color, int width, int height) Draws a string with default anti-aliasing. -
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. -
drawString
Draws a string with default anti-aliasing. -
drawImg
Draws an image.- Parameters:
g- The graphics context.img- The image to draw.point- The top-left position to draw at.- Returns:
- The
Graphicsobject.
-
drawImg
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
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
Gets all fonts available in the system.- Returns:
- An array of fonts.
-
registerFont
Registers a font with the system.- Parameters:
font- The font to register.
-
createFont
Creates a default font.- Returns:
- The default font.
-
createSansSerifFont
Creates a SansSerif font.- Parameters:
size- The font size.- Returns:
- The font.
-
createFont
Creates a font with the specified name.- Parameters:
name- The font name.size- The font size.- Returns:
- The font.
-
createFont
Creates a font from a file.- Parameters:
fontFile- The font file.- Returns:
- A
Font.
-
createFont
Creates a font from a stream.- Parameters:
fontStream- The font stream.- Returns:
- A
Font.
-
getDimension
Gets the dimensions of a string with a given font.- Parameters:
metrics- TheFontMetrics.text- The string.- Returns:
- The dimensions.
-
getRectangle
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.
-