Java examples for 2D Graphics:BufferedImage
remove BufferedImage Background
//package com.java2s; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; public class Main { public static void main(String[] argv) throws Exception { String picFile = "java2s.com"; System.out.println(removeBackground(picFile)); }//from ww w . j a va 2s .co m private static BufferedImage removeBackground(String picFile) throws Exception { BufferedImage img = ImageIO.read(new File(picFile)); int width = img.getWidth(); int height = img.getHeight(); for (int x = 0; x < width; ++x) { for (int y = 0; y < height; ++y) { if (isWhite(img.getRGB(x, y)) == 1) { img.setRGB(x, y, Color.WHITE.getRGB()); } else { img.setRGB(x, y, Color.BLACK.getRGB()); } } } ImageIO.write(img, "JPG", new File("./captcha/random.jpg")); return img; } private static int isWhite(int colorInt) { Color color = new Color(colorInt); if (color.getRed() + color.getGreen() + color.getBlue() > 400) { return 1; } return 0; } }