import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.imageio.ImageIO;
public class ImagePreProcess {
//去除噪点
public static int isWhite(int colorInt) {
Color color = new Color(colorInt);
if (color.getRed() + color.getGreen() + color.getBlue() > 590) {
return 1;
}
return 0;
}
//移除背景
public static BufferedImage removeBackgroud(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());
}
}
}
return img;
}
//将要识别的图片拆分为单个字母的图片
public static List<BufferedImage> splitImage(BufferedImage img)
throws Exception {
List<BufferedImage> subImgs = new ArrayList<BufferedImage>();
//此处的数字应该根据验证码的大小做相应调整
subImgs.add(img.getSubimage(25, 0, 12, 25));
subImgs.add(img.getSubimage(38, 0, 11, 25));
subImgs.add(img.getSubimage(48, 0, 11, 25));
subImgs.add(img.getSubimage(58, 0, 11, 25));
return subImgs;
}
//加载验证码模板库
public static Map<BufferedImage, String> loadTrainData(String trainPath) throws Exception {
Map<BufferedImage, String> map = new HashMap<BufferedImage, String>();
File dir = new File(trainPath);
File[] files = dir.listFiles();
for (File file : files) {
if(file.getName().endsWith("jpg"))
map.put(ImageIO.read(file), file.getName().charAt(0) + "");
}
return map;
}
//进行对比识别
public static String getSingleCharOcr(BufferedImage img,
Map<BufferedImage, String> map) {
String result = "";
int width = img.getWidth();
int height = img.getHeight();
int min = width * height;
for (BufferedImage bi : map.keySet()) {
int count = 0;
Label1: for (int x = 0; x < width && x < bi.getWidth(); ++x) {
for (int y = 0; y < height && y < bi.getWidth(); ++y) {
if (isWhite(img.getRGB(x, y)) != isWhite(bi.getRGB(x, y))) {
count++;
if (count >= min)
break Label1;
}
}
}
if (count < min) {
min = count;
result = map.get(bi);
}
}
return result;
}
//识别的入口
public static String getAllOcr(String file) throws Exception {
BufferedImage img = removeBackgroud(file);
List<BufferedImage> listImg = splitImage(img);
Map<BufferedImage, String> map = loadTrainData("C:/test/train");
String result = "";
for (BufferedImage bi : listImg) {
result += getSingleCharOcr(bi, map);
}
ImageIO.write(img, "JPG", new File("C:/test/result/"+result+".jpg"));
return result;
}
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
String text = getAllOcr("C:/test/src.jpeg");
System.out.println("jpg = " + text);
}
}