java验证码识别--1
http://blog.csdn.net/problc/article/details/5794460
java验证码识别--2
http://blog.csdn.net/problc/article/details/5797507
java验证码识别--3
http://blog.csdn.net/problc/article/details/5800093
java验证码识别--4
http://blog.csdn.net/problc/article/details/5846614
java验证码识别--5
http://blog.csdn.net/problc/article/details/5983276
(本文仅用于学习研究图像匹配识别原理,不得用于其他用途。)
完整eclipse工程http://download.csdn.net/detail/problc/3829004
github地址 https://github.com/isee15/captcha-ocr
换一个字体固定,大小固定,位置不固定的验证码
还是四步。
1。图像预处理
这验证码还是很厚道的,都没有任何干扰。不用处理
2。分割
先纵向扫描,很容易分成四部分
再对每一部分横向扫描
3。训练就容易了
把分割的结果对应存成5.jpg,9.jpg,3.jpg,a.jpg 就可以了
4。识别
因为固定大小,识别跟 验证码识别--1 里面一样,像素比较就可以了。
识别结果如下,识别率100%:
源码:
public class ImagePreProcess2 {
private static Map<BufferedImage, String> trainMap = null;
private static int index = 0;
public static int isBlack(int colorInt) {
Color color = new Color(colorInt);
if (color.getRed() + color.getGreen() + color.getBlue() <= 100) {
return 1;
}
return 0;
}
public static int isWhite(int colorInt) {
Color color = new Color(colorInt);
if (color.getRed() + color.getGreen() + color.getBlue() > 100) {
return 1;
}
return 0;
}
public static BufferedImage removeBackgroud(String picFile)
throws Exception {
BufferedImage img = ImageIO.read(new File(picFile));
return img;
}
public static BufferedImage removeBlank(BufferedImage img) throws Exception {
int width = img.getWidth();
int height = img.getHeight();
int start = 0;
int end = 0;
Label1: for (int y = 0; y < height; ++y) {
int count = 0;
for (int x = 0; x < width; ++x) {
if (isWhite(img.getRGB(x, y)) == 1) {
count++;
}
if (count >= 1) {
start = y;
break Label1;
}
}
}
Label2: for (int y = height - 1; y >= 0; --y) {
int count = 0;
for (int x = 0; x < width; ++x) {
if (isWhite(img.getRGB(x, y)) == 1) {
count++;
}
if (count >= 1) {
end = y;
break Label2;
}
}
}
return img.getSubimage(0, start, width, end - start + 1);
}
public static List<BufferedImage> splitImage(BufferdImage img)
throws Exception {
List<BufferedImage> subImgs = new ArrayList<BufferedImage>();
int width = img.getWidth();
int height = img.getHeight();
List<Integer> weightlist = new ArrayList<Integer>();
for (int x = 0; x < width; ++x) {
int count = 0;
for (int y = 0; y < height; ++y) {
if (isWhite(img.getRGB(x, y)) == 1) {
count++;
}
}
weightlist.add(count);
}
for (int i = 0; i < weightlist.size();) {
int length = 0;
while (weightlist.get(i++) > 1) {
length++;
}
if (length > 12) {
subImgs.add(removeBlank(img.getSubimage(i - length - 1, 0,
length / 2, height)));
subImgs.add(removeBlank(img.getSubimage(i - length / 2 - 1, 0,
length / 2, height)));
} else if (length > 3) {
subImgs.add(removeBlank(img.getSubimage(i - length - 1, 0,
length, height)));
}
}
return subImgs;
}
public static Map<BufferedImage, String> loadTrainData() throws Exception {
if (trainMap == null) {
Map<BufferedImage, String> map = new HashMap<BufferedImage, String>();
File dir = new File("train2");
File[] files = dir.listFiles();
for (File file : files) {
map.put(ImageIO.read(file), file.getName().charAt(0) + "");
}
trainMap = map;
}
return trainMap;
}
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;
int widthmin = width < bi.getWidth() ? width : bi.getWidth();
int heightmin = height < bi.getHeight() ? height : bi.getHeight();
Label1: for (int x = 0; x < widthmin; ++x) {
for (int y = 0; y < heightmin; ++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();
String result = "";
for (BufferedImage bi : listImg) {
result += getSingleCharOcr(bi, map);
}
ImageIO.write(img, "JPG", new File("result2//" + result + ".jpg"));
return result;
}
public static void downloadImage() {
HttpClient httpClient = new HttpClient();
GetMethod getMethod = null;
for (int i = 0; i < 30; i++) {
getMethod = new GetMethod("http://www.pkland.net/img.php?key="
+ (2000 + i));
try {
// 执行getMethod
int statusCode = httpClient.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: "
+ getMethod.getStatusLine());
}
// 读取内容
String picName = "img2//" + i + ".jpg";
InputStream inputStream = getMethod.getResponseBodyAsStream();
OutputStream outStream = new FileOutputStream(picName);
IOUtils.copy(inputStream, outStream);
outStream.close();
System.out.println(i + "OK!");
} catch (Exception e) {
e.printStackTrace();
} finally {
// 释放连接
getMethod.releaseConnection();
}
}
}
public static void trainData() throws Exception {
File dir = new File("temp");
File[] files = dir.listFiles();
for (File file : files) {
BufferedImage img = removeBackgroud("temp//" + file.getName());
List<BufferedImage> listImg = splitImage(img);
if (listImg.size() == 4) {
for (int j = 0; j < listImg.size(); ++j) {
ImageIO.write(listImg.get(j), "JPG", new File("train2//"
+ file.getName().charAt(j) + "-" + (index++)
+ ".jpg"));
}
}
}
}
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// downloadImage();
for (int i = 0; i < 30; ++i) {
String text = getAllOcr("img2//" + i + ".jpg");
System.out.println(i + ".jpg = " + text);
}
}
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有