Skip to content

Commit

Permalink
v1.0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
by-syk committed Dec 7, 2016
1 parent dfefe94 commit 6adcd44
Show file tree
Hide file tree
Showing 19 changed files with 279 additions and 2 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
| 第3类 | [![bbmc](http://211.70.128.23/JWWEB/sys/ValidateCode.aspx)](http://211.70.128.23/JWWEB/sys/ValidateCode.aspx) | [蚌埠医学院教务网络管理系统](http://211.70.128.23/JWWEB) | 91 / 100 = **91%**,单:389 / 400 = 97% |
| 第4类 | [![sdjtu](http://211.64.127.221/validateCodeAction.do?random=0.44780089727138211)](http://211.64.127.221/validateCodeAction.do?random=0.44780089727138211) | [山东交通学院综合教务系统](http://211.64.127.221) | 97 / 100 = **97%**,单:397 / 400 = 99% |
| 第5类 | [![ncu](http://218.64.56.18/jsxsd/verifycode.servlet?t=0.7339572516226678)](http://218.64.56.18/jsxsd/verifycode.servlet?t=0.7339572516226678) | [南昌大学教学一体化服务平台](http://218.64.56.18/jsxsd) | 100 / 100 = **100%**,单:400 / 400 = 100% |
| 第6类 | [![ahut](http://211.70.149.135:88/CheckCode.aspx)](http://211.70.149.135:88/CheckCode.aspx) | [安徽工业大学教务管理系统](http://211.70.149.135:88) | 100 / 100 = **100%**,单:400 / 400 = 100% |

图形样本特征分析:
* 第1类
Expand Down Expand Up @@ -45,6 +46,12 @@
:smile: 每个字符相对位置固定
:smile: 有干扰线,但可忽略
![graphicc5](art/ps_graphicc5.png)
* 第6类
:smile: 字符数固定
:smile: 字符饱和度基本一致,阀值 `0.2`
:smile: 无字形变化,只含数字
:smile: 每个字符位置固定
![graphicc6](art/ps_graphicc6.png)


### 开始使用
Expand Down
Binary file added art/ps_graphicc6.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added out/graphicc6_raw.zip
Binary file not shown.
Binary file not shown.
Binary file renamed out/graphiccr-1.0.5.jar → out/graphiccr-1.0.6.jar
Binary file not shown.
31 changes: 29 additions & 2 deletions src/com/by_syk/graphiccr/Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@
import com.by_syk.graphiccr.core.GraphicC3Translator;
import com.by_syk.graphiccr.core.GraphicC4Translator;
import com.by_syk.graphiccr.core.GraphicC5Translator;
import com.by_syk.graphiccr.core.GraphicC6Translator;
import com.by_syk.graphiccr.util.ExtraUtil;

public class Test {
public static void main(String[] args) {
testGraphicC5();
testGraphicC6();
}

private static void testGraphicC1() {
Expand Down Expand Up @@ -66,7 +67,7 @@ private static void testGraphicC2() {

System.out.println("TRAIN DONE");

for (int i = 0; i < 80; ++i) {
for (int i = 0; i < 10; ++i) {
ExtraUtil.downloadFile("http://61.139.105.138/CheckCode.aspx",
new File(testDir, System.currentTimeMillis() + ".gif"));
System.out.println("DOWNLOAD TEST " + (i + 1));
Expand Down Expand Up @@ -171,4 +172,30 @@ private static void testGraphicC5() {

System.out.println("TRANSLATE DONE");
}

private static void testGraphicC6() {
File testDir = new File("E:/JavaProjects/GraphicCR/reserve/GraphicC/6/test");

for (int i = 0; i < 10; ++i) {
ExtraUtil.downloadFile("http://211.70.149.135:88/CheckCode.aspx",
new File(testDir, System.currentTimeMillis() + ".gif"));
System.out.println("DOWNLOAD TEST " + (i + 1));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

System.out.println("DOWNLOAD TEST DONE");

GraphicC6Translator translator = GraphicC6Translator.getInstance();
for (File file : testDir.listFiles()) {
String result = translator.translate(file);
file.renameTo(new File(file.getParentFile(), result + ".gif"));
System.out.println("TRANSLATE " + result);
}

System.out.println("TRANSLATE DONE");
}
}
224 changes: 224 additions & 0 deletions src/com/by_syk/graphiccr/core/GraphicC6Translator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
/**
* Copyright 2016 By_syk
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.by_syk.graphiccr.core;

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;

import com.by_syk.graphiccr.util.ExtraUtil;

/**
* 第6类图形验证码识别
* <br />针对截至 2016-12-07 为止安徽工业大学教务管理系统登录用的验证码
* <br />图形尺寸为 60*22
*
* @author By_syk
*/
public class GraphicC6Translator {
private static GraphicC6Translator translator = null;

private Map<BufferedImage, Character> trainMap = null;

/**
* 元字符宽度
*/
private static final int UNIT_W = 8;
/**
* 元字符高度
*/
private static final int UNIT_H = 12;

/**
* 有效像素颜色值
*/
private static final int TARGET_COLOR = Color.BLACK.getRGB();

/**
* 无效像素颜色值
*/
private static final int USELESS_COLOR = Color.WHITE.getRGB();

private GraphicC6Translator() {}

public static GraphicC6Translator getInstance() {
if (translator == null) {
translator = new GraphicC6Translator();
}

return translator;
}

/**
* 目标像素判断
* <br />(基于饱和度)
*
* @param colorInt
* @return
*/
private boolean isTarget(int colorInt) {
Color color = new Color(colorInt);
float[] hsb = new float[3];
Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), hsb);
return hsb[1] > 0.2f; // 饱和度
}

/**
* 去噪
*
* @param picFile 图形验证码文件
* @return
* @throws Exception
*/
private BufferedImage denoise(File picFile) throws Exception {
BufferedImage img = ImageIO.read(picFile);
int width = img.getWidth();
int height = img.getHeight();
for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
if (isTarget(img.getRGB(x, y))) {
img.setRGB(x, y, TARGET_COLOR);
} else {
img.setRGB(x, y, USELESS_COLOR);
}
}
}
return img;
}

/**
* 分割元字符
*
* @param img
* @return
* @throws Exception
*/
private List<BufferedImage> split(BufferedImage img) throws Exception {
List<BufferedImage> subImgs = new ArrayList<BufferedImage>();
subImgs.add(img.getSubimage(5, 5, UNIT_W, UNIT_H));
subImgs.add(img.getSubimage(14, 5, UNIT_W, UNIT_H));
subImgs.add(img.getSubimage(23, 5, UNIT_W, UNIT_H));
subImgs.add(img.getSubimage(32, 5, UNIT_W, UNIT_H));
subImgs.add(img.getSubimage(41, 5, UNIT_W, UNIT_H));
return subImgs;
}

/**
* 取出训练数据
*
* @return
* @throws Exception
*/
private Map<BufferedImage, Character> loadTrainData() throws Exception {
if (trainMap == null) {
trainMap = new HashMap<>();
String trainLog = ExtraUtil.readFile(this.getClass()
.getResourceAsStream("/resources/img/6/6.txt"));
if (trainLog == null) {
return trainMap;
}
for (String name : trainLog.split("\n")) {
trainMap.put(ImageIO.read(this.getClass()
.getResourceAsStream("/resources/img/6/" + name)), name.charAt(0));
}
}

return trainMap;
}

/**
* 训练
*/
public void train() {
// 由于样本具有很强的规律性,已通过 PS 训练完成
}

/**
* 单元识别
*
* @param img
* @param trainImgMap
* @return
*/
private char recognize(BufferedImage img, Map<BufferedImage, Character> trainImgMap) {
char result = ' ';
int width = img.getWidth();
int height = img.getHeight();
int min = width * height; // 最小差异像素数
for (BufferedImage bi : trainImgMap.keySet()) {
int count = 0; // 差异像素数
for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
count += (img.getRGB(x, y) != bi.getRGB(x, y) ? 1 : 0);
if (count >= min) {
break;
}
}
}
if (count < min) {
min = count;
result = trainImgMap.get(bi);
}
}
return result;
}

/**
* 识别
*
* @param picFile 图形验证码文件
* @return
*/
public String translate(File picFile) {
String result = "";
try {
BufferedImage img = denoise(picFile);
List<BufferedImage> listImg = split(img);
Map<BufferedImage, Character> map = loadTrainData();
for (BufferedImage bi : listImg) {
result += recognize(bi, map);
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}

// public static void main(String[] args) {
// GraphicC6Translator translator = GraphicC6Translator.getInstance();
// File testDir = new File("E:/JavaProjects/GraphicCR/reserve/GraphicC/6/raw");
// for (File file : testDir.listFiles()) {
// if (file.getName().contains("-2")) {
// continue;
// }
// try {
// BufferedImage img = translator.denoise(file);
// ImageIO.write(img, "PNG", new File(file.getParentFile(),
// file.getName().split("\\.")[0] + "-2.gif"));
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
// System.out.println("DONE");
// }
}
9 changes: 9 additions & 0 deletions src/com/by_syk/graphiccr/core/GraphicCTranslator.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ public class GraphicCTranslator {
*/
public static final int TYPE_5 = 5;

/**
* 第6类图形验证码识别
* <br />针对截至 2016-12-07 为止安徽工业大学教务管理系统登录用的验证码
* <br />图形尺寸为 60*22
*/
public static final int TYPE_6 = 6;

/**
* 识别指定文件的图形验证码
*
Expand All @@ -75,6 +82,8 @@ public static String translate(File picFile, int type) {
return GraphicC4Translator.getInstance().translate(picFile);
case TYPE_5:
return GraphicC5Translator.getInstance().translate(picFile);
case TYPE_6:
return GraphicC6Translator.getInstance().translate(picFile);
}
return "";
}
Expand Down
Binary file added src/resources/img/6/0.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/resources/img/6/1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/resources/img/6/2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/resources/img/6/3.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/resources/img/6/4.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/resources/img/6/5.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/resources/img/6/6.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions src/resources/img/6/6.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
0.png
1.png
2.png
3.png
4.png
5.png
6.png
7.png
8.png
9.png
Binary file added src/resources/img/6/7.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/resources/img/6/8.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/resources/img/6/9.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6adcd44

Please sign in to comment.