import java.awt.Color; import java.awt.image.BufferedImage; import java.awt.image.WritableRaster; public class ImageProcessor { public static BufferedImage removeBackground(BufferedImage image, Color bgColor) { int width = image.getWidth(); int height = image.getHeight(); WritableRaster raster = image.getRaster(); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { int[] pixel = raster.getPixel(x, y, (int[]) null); int red = pixel[0]; int green = pixel[1]; int blue = pixel[2]; if (isBackgroundColor(red, green, blue, bgColor)) { pixel[3] = 0; raster.setPixel(x, y, pixel); } } } return image; } private static boolean isBackgroundColor(int red, int green, int blue, Color bgColor) { return (Math.abs(red - bgColor.getRed()) <= 10 && Math.abs(green - bgColor.getGreen()) <= 10 && Math.abs(blue - bgColor.getBlue()) <= 10); } }