Module bus.extra

Class ImagePlace

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

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

    • IMAGES_TYPE

      public static String[] IMAGES_TYPE
      Array of currently supported image types. Results for other formats are not guaranteed.
  • Constructor Details

    • ImagePlace

      public ImagePlace()
  • Method Details

    • backgroundRemoval

      public static void backgroundRemoval(String inputPath, String outputPath, int tolerance)
      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

      public static void backgroundRemoval(File input, File output, int tolerance)
      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

      public static void backgroundRemoval(File input, File output, Color override, int tolerance)
      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

      public static Color hexToRgb(String hex)
      Converts a hexadecimal color code to an RGB Color object.
      Parameters:
      hex - The hexadecimal color code (e.g., "#FF0000").
      Returns:
      The corresponding Color object.
    • areColorsWithinTolerance

      public static boolean areColorsWithinTolerance(Color color1, Color color2, int tolerance)
      Determines if two colors are within a given tolerance.
      Parameters:
      color1 - The first color.
      color2 - The second color.
      tolerance - The tolerance value.
      Returns:
      true if the colors are within tolerance, false otherwise.
    • areColorsWithinTolerance

      public static boolean areColorsWithinTolerance(Color color1, Color color2, Color tolerance)
      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:
      true if the colors are within tolerance, false otherwise.
    • getMainColor

      public static String getMainColor(String input)
      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

      public static String getMainColor(File input)
      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

      public static String getMainColor(BufferedImage bufferedImage)
      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.