package cn.moqi.test; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; public class Test { public static void main(String[] args) throws Exception { BufferedImage img = ImageIO.read(new File("d:/id_card.jpg")); final int width = img.getWidth(); final int height = img.getHeight(); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { int rgb = img.getRGB(i, j); img.setRGB(i, j, grayRGB(Integer.toHexString(rgb))); } } ImageIO.write(img, "jpg", new File("d:/id.jpg")); } private static int grayRGB(String argb) { //ARGB前两位是透明度,后面开始是RGB int r = Integer.parseInt(argb.substring(2, 4), 16); int g = Integer.parseInt(argb.substring(4, 6), 16); int b = Integer.parseInt(argb.substring(6, 8), 16); //平均值 String average = Integer.toHexString((r + g + b) / 3); if (average.length() == 1) { average = "0" + average; } //RGB都变成平均值 return Integer.parseInt(average + average + average, 16); } }