设为首页 加入收藏

TOP

Java识别简单的验证码(二)
2015-11-10 13:46:04 来源: 作者: 【 】 浏览:19
Tags:Java 识别 简单 验证
urn false;
43? ? ? ? } else {
44? ? ? ? ? ? int[][] weightA = getImgWeight(imageA);
45? ? ? ? ? ? int[][] weightB = getImgWeight(imageB);
46? ? ? ? ? ? int count = 0;
47? ? ? ? ? ? for (int i = 0; i < widthA; i++) {
48? ? ? ? ? ? ? ? for (int j = 0; j < heightB; j++) {
49? ? ? ? ? ? ? ? ? ? if (weightA[i][j] != weightB[i][j]) {
50? ? ? ? ? ? ? ? ? ? ? ? count++;
51? ? ? ? ? ? ? ? ? ? }
52? ? ? ? ? ? ? ? }
53? ? ? ? ? ? }
54? ? ? ? ? ? if ((double) count / (widthA * widthB) > (1 - Global.SIMILARITY)) {
55? ? ? ? ? ? ? ? return false;
56? ? ? ? ? ? } else {
57? ? ? ? ? ? ? ? return true;
58? ? ? ? ? ? }
59? ? ? ? }
60? ? }


?


4.完整代码


? 1 import javax.imageio.ImageIO;
? 2 import java.awt.image.BufferedImage;
? 3 import java.io.File;
? 4 import java.io.IOException;
? 5 import java.util.HashMap;
? 6 import java.util.Map;
? 7
? 8 public class Global {
? 9? ? public static final String LIB_PATH = "C:/lib";
?10? ? public static final String LIB_NO = "C:/no";
?11? ? public static final double SIMILARITY = 0.9;
?12? ? public static Map trainedMap;
?13? ? public static Map noTrainedMap = new HashMap<>();
?14
?15? ? static {
?16? ? ? ? trainedMap = getMap(LIB_PATH);
?17? ? ? ? noTrainedMap = getMap(LIB_NO);
?18? ? }
?19
?20? ? private static Map? getMap(String path) {
?21? ? ? ? Map map = new HashMap<>();
?22? ? ? ? File parentFile = new File(path);
?23? ? ? ? for (String filePath : parentFile.list()) {
?24? ? ? ? ? ? File file = new File(path + File.separator + filePath);
?25? ? ? ? ? ? String fileName = file.getName();
?26? ? ? ? ? ? String key = fileName.substring(0,fileName.indexOf(".")).trim();
?27? ? ? ? ? ? try {
?28? ? ? ? ? ? ? ? map.put(key, ImageIO.read(file));
?29? ? ? ? ? ? } catch (IOException e) {
?30? ? ? ? ? ? ? ? e.printStackTrace();
?31? ? ? ? ? ? }
?32? ? ? ? }
?33? ? ? ? return map;
?34? ? }
?35 }
?36 import javax.imageio.ImageIO;
?37 import java.awt.*;
?38 import java.awt.image.BufferedImage;
?39 import java.io.File;
?40 import java.io.IOException;
?41 import java.util.*;
?42
?43 /**
?44? * 识别验证码
?45? */
?46 public class ImageProcess {
?47? ? private String imgPath;
?48
?49? ? public ImageProcess(String imgPath) {
?50? ? ? ? this.imgPath = imgPath;
?51? ? }
?52
?53? ? public String getResult() {
?54? ? ? ? java.util.List imgList = null;
?55? ? ? ? try {
?56? ? ? ? ? ? BufferedImage img = ImageIO.read(new File(imgPath));
?57? ? ? ? ? ? imgList = splitImage(img);
?58? ? ? ? } catch (IOException e) {
?59? ? ? ? ? ? e.printStackTrace();
?60? ? ? ? } catch (Exception e) {
?61? ? ? ? ? ? e.printStackTrace();
?62? ? ? ? }
?63? ? ? ? return realize(imgList);
?64? ? }
?65
?66? ? //分析识别
?67? ? private String realize(java.util.List imgList) {
?68? ? ? ? String resultStr = "";
?69? ? ? ? for (BufferedImage img : imgList) {
?70? ? ? ? ? ? String key = getKey(Global.trainedMap, img);
?71? ? ? ? ? ? if (key == null) {
?72? ? ? ? ? ? ? ? String noTrainedKey = getKey(Global.noTrainedMap, img);
?73? ? ? ? ? ? ? ? if(noTrainedKey == null){
?74? ? ? ? ? ? ? ? ? ? try {
?75? ? ? ? ? ? ? ? ? ? ? ? ImageIO.write(img, "JPG", new File(Global.LIB_NO + File.separator + UUID.randomUUID() + ".jpg"));
?76? ? ? ? ? ? ? ? ? ? } catch (IOException e) {
?77? ? ? ? ? ? ? ? ? ? ? ? e.printStackTrace();
?78? ? ? ? ? ? ? ? ? ? }
?79? ? ? ? ? ? ? ? }
?80? ? ? ? ? ? } else {
?81? ? ? ? ? ? ? ? resultStr += key;
?82? ? ? ? ? ? }
?83? ? ? ? }
?84? ? ? ? return resultStr;
?85? ? }
?86
?87? ? //获取已知值
?88? ? private String getKey(Map map, BufferedImage img){
?89? ? ? ? String resultStr = null;
?90? ? ? ? Set> entrySet = map.entrySet();
?91? ? ? ? for (Map.Entry one : entrySet) {
?92? ? ? ? ? ? if (isSimilarity(img, one.getValue())) {
?93? ? ? ? ? ? ? ? resultStr = one.getKey();
?94? ? ? ? ? ? ? ? break;
?95? ? ? ? ? ? }
?96? ? ? ? }
?97? ? ? ? return resultStr;
?98? ? }
?99
100? ? //是否相似
101? ? private boolean isSimilarity(BufferedImage imageA, BufferedIm

首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Python日志库的用法 下一篇Linux下运行C++程序出现“段错误(..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: