设为首页 加入收藏

TOP

Java识别简单的验证码(三)
2015-11-10 13:46:04 来源: 作者: 【 】 浏览:18
Tags:Java 识别 简单 验证
age imageB) {
102? ? ? ? int widthA = imageA.getWidth();
103? ? ? ? int widthB = imageB.getWidth();
104? ? ? ? int heightA = imageA.getHeight();
105? ? ? ? int heightB = imageB.getHeight();
106? ? ? ? if (widthA != widthB || heightA != heightB) {
107? ? ? ? ? ? return false;
108? ? ? ? } else {
109? ? ? ? ? ? int[][] weightA = getImgWeight(imageA);
110? ? ? ? ? ? int[][] weightB = getImgWeight(imageB);
111? ? ? ? ? ? int count = 0;
112? ? ? ? ? ? for (int i = 0; i < widthA; i++) {
113? ? ? ? ? ? ? ? for (int j = 0; j < heightB; j++) {
114? ? ? ? ? ? ? ? ? ? if (weightA[i][j] != weightB[i][j]) {
115? ? ? ? ? ? ? ? ? ? ? ? count++;
116? ? ? ? ? ? ? ? ? ? }
117? ? ? ? ? ? ? ? }
118? ? ? ? ? ? }
119? ? ? ? ? ? if ((double) count / (widthA * widthB) > (1 - Global.SIMILARITY)) {
120? ? ? ? ? ? ? ? return false;
121? ? ? ? ? ? } else {
122? ? ? ? ? ? ? ? return true;
123? ? ? ? ? ? }
124? ? ? ? }
125? ? }
126
127? ? //分割图片
128? ? private java.util.List splitImage(BufferedImage originImg)
129? ? ? ? ? ? throws Exception {
130? ? ? ? java.util.List subImgList = new ArrayList<>();
131? ? ? ? int height = originImg.getHeight();
132? ? ? ? int[][] weight = getImgWeight(originImg);
133? ? ? ? int start = 0;
134? ? ? ? int end = 0;
135? ? ? ? boolean isStartReady = false;
136? ? ? ? boolean isEndReady = false;
137? ? ? ? for (int i = 0; i < weight.length; i++) {
138? ? ? ? ? ? boolean isBlank = isBlankArr(weight[i]);
139? ? ? ? ? ? if (isBlank) {
140? ? ? ? ? ? ? ? if (isStartReady && !isEndReady) {
141? ? ? ? ? ? ? ? ? ? end = i;
142? ? ? ? ? ? ? ? ? ? isEndReady = true;
143? ? ? ? ? ? ? ? }
144? ? ? ? ? ? } else {
145? ? ? ? ? ? ? ? if (!isStartReady) {
146? ? ? ? ? ? ? ? ? ? start = i;
147? ? ? ? ? ? ? ? ? ? isStartReady = true;
148? ? ? ? ? ? ? ? }
149? ? ? ? ? ? }
150? ? ? ? ? ? if (isStartReady && isEndReady) {
151? ? ? ? ? ? ? ? subImgList.add(originImg.getSubimage(start, 0, end - start, height));
152? ? ? ? ? ? ? ? isStartReady = false;
153? ? ? ? ? ? ? ? isEndReady = false;
154? ? ? ? ? ? }
155? ? ? ? }
156? ? ? ? return subImgList;
157? ? }
158
159? ? //颜色是否为空白
160? ? private boolean isBlank(int colorInt) {
161? ? ? ? Color color = new Color(colorInt);
162? ? ? ? return color.getRed() + color.getGreen() + color.getBlue() > 600;
163? ? }
164
165? ? //数组是不是全空白
166? ? private boolean isBlankArr(int[] arr) {
167? ? ? ? boolean isBlank = true;
168? ? ? ? for (int value : arr) {
169? ? ? ? ? ? if (value == 0) {
170? ? ? ? ? ? ? ? isBlank = false;
171? ? ? ? ? ? ? ? break;
172? ? ? ? ? ? }
173? ? ? ? }
174? ? ? ? return isBlank;
175? ? }
176
177? ? //获取图片权重数据
178? ? private int[][] getImgWeight(BufferedImage img) {
179? ? ? ? int width = img.getWidth();
180? ? ? ? int height = img.getHeight();
181? ? ? ? int[][] weight = new int[width][height];
182? ? ? ? for (int x = 0; x < width; ++x) {
183? ? ? ? ? ? for (int y = 0; y < height; ++y) {
184? ? ? ? ? ? ? ? if (isBlank(img.getRGB(x, y))) {
185? ? ? ? ? ? ? ? ? ? weight[x][y] = 1;
186? ? ? ? ? ? ? ? }
187? ? ? ? ? ? }
188? ? ? ? }
189? ? ? ? return weight;
190? ? }
191
192
193? ? public static void main(String[] args) throws Exception {
194? ? ? ? String result = new ImageProcess("C:/login.jpg").getResult();
195? ? ? ? System.out.println(result);
196
197? ? }
198 }


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

评论

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