java.lang.Object
org.miaixz.bus.extra.image.ImagePlace
Image background recognition, replacement, and vectorization. This class calculates and replaces the background color
of an image based on a set of rules.
- Since:
- Java 17+
- Author:
- Kimi Liu
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanareColorsWithinTolerance(Color color1, Color color2, int tolerance) Determines if two colors are within a given tolerance.static booleanareColorsWithinTolerance(Color color1, Color color2, Color tolerance) Determines if two colors are within a given tolerance for each RGB component.static BufferedImagebackgroundRemoval(BufferedImage bufferedImage, Color override, int tolerance) Removes the background from a buffered image, replacing it with a specified color or transparency.static BufferedImagebackgroundRemoval(ByteArrayOutputStream outputStream, Color override, int tolerance) Removes the background from an image provided as a byte array stream.static voidbackgroundRemoval(File input, File output, int tolerance) Removes the background from an image with a solid color, making it transparent.static voidbackgroundRemoval(File input, File output, Color override, int tolerance) Removes the background from an image, replacing it with a specified color or transparency.static voidbackgroundRemoval(String inputPath, String outputPath, int tolerance) Removes the background from an image with a solid color, making it transparent.static StringgetMainColor(BufferedImage bufferedImage) Gets the approximate dominant color of an image by finding the most frequent RGB value.static StringgetMainColor(File input) Gets the approximate dominant color of an image by finding the most frequent RGB value.static StringgetMainColor(String input) Gets the approximate dominant color of an image by finding the most frequent RGB value.static ColorConverts a hexadecimal color code to an RGB Color object.
-
Field Details
-
IMAGES_TYPE
Array of currently supported image types. Results for other formats are not guaranteed.
-
-
Constructor Details
-
ImagePlace
public ImagePlace()
-
-
Method Details
-
backgroundRemoval
Removes the background from an image with a solid color, making it transparent. It samples pixels from the image edges to determine the background color, then adds a tolerance and sets the alpha of matching pixels to 0.- Parameters:
inputPath- The path of the image to process.outputPath- The path for the output image.tolerance- The tolerance value (0-255) for color matching.
-
backgroundRemoval
Removes the background from an image with a solid color, making it transparent.- Parameters:
input- The image file to process.output- The final output file.tolerance- The tolerance value (0-255) for color matching.
-
backgroundRemoval
Removes the background from an image, replacing it with a specified color or transparency. The output file must be a .png.- Parameters:
input- The image file to process.output- The final output file (must be .png).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 from a buffered 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 BufferedImage.
-
backgroundRemoval
public static BufferedImage backgroundRemoval(ByteArrayOutputStream outputStream, Color override, int tolerance) Removes the background from an image provided as a byte array stream.- 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 BufferedImage.
-
hexToRgb
Converts a hexadecimal color code to an RGB Color object.- Parameters:
hex- The hexadecimal color code (e.g., "#FF0000").- Returns:
- The corresponding
Colorobject.
-
areColorsWithinTolerance
Determines if two colors are within a given tolerance.- Parameters:
color1- The first color.color2- The second color.tolerance- The tolerance value.- Returns:
trueif the colors are within tolerance,falseotherwise.
-
areColorsWithinTolerance
Determines if two colors are within a given tolerance for each RGB component.- Parameters:
color1- The first color.color2- The second color.tolerance- The tolerance for each RGB component.- Returns:
trueif the colors are within tolerance,falseotherwise.
-
getMainColor
Gets the approximate dominant color of an image by finding the most frequent RGB value.- Parameters:
input- The path to the image file.- Returns:
- A hexadecimal color code representing the dominant color.
-
getMainColor
Gets the approximate dominant color of an image by finding the most frequent RGB value.- Parameters:
input- The image file.- Returns:
- A hexadecimal color code representing the dominant color.
-
getMainColor
Gets the approximate dominant color of an image by finding the most frequent RGB value.- Parameters:
bufferedImage- The image stream.- Returns:
- A hexadecimal color code representing the dominant color.
-