滑动验证码

其他的不多说,直奔主题:

image

本次是以微信注册中所遇到滑块验证码为例,主要的目的就是让脚本实现自动识别阴影部分的位置,然后计算出距离拖动滑块完成验证操作

image

想要从1处滑动到2处,就需要知道1处和2处的中间点的x轴坐标位置,1点的坐标基本是固定的,2点的坐标是不断在变化的.我的方法也是在网上查到的,思路就是得出原图,然后和有阴影的图片进行对比从而得出阴影部分的位置.图片只需要得到3位置的部分就可以了.左边的部分不需要

Java实现

package com.wkk.test;

import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;

public class Test {
    public static void main(String[] arg) {
        String path1 = "D:\\马路.png";
        String path2 = "D:\\测试1.png";
        int dv = getDifferenceValue(path1, path2);
        System.out.println(dv);
    }


    /**
     * 通过像素对比来计算偏差值
     *
     * @param path1 原图位置
     * @param path2 滑块图位置
     * @return 偏差值
     */
    public static int getDifferenceValue(String path1, String path2) {
        int result = 0;
        File file = new File(path1);
        File file1 = new File(path2);
        if (!file.exists() || !file1.exists()) {
            System.out.println("文件不存在");
            return 0;
        }

        try {
            BufferedImage image = ImageIO.read(file);
            BufferedImage image1 = ImageIO.read(file1);
            int height = image.getHeight();
            int width = image.getWidth();

            //遍历每一个像素点,对比,相同为0,不同为1
            int[][] ints = new int[height][width];
            for (int x = 1; x < width; x++) {
                for (int y = 1; y < height; y++) {
                    int color = image.getRGB(x, y);
                    int color1 = image1.getRGB(x, y);
                    if (color == color1) {
                        ints[y - 1][x - 1] = 0;
                    } else {
                        ints[y - 1][x - 1] = 1;
                    }
                }
            }

            //通过上下左右像素的对比来去除杂色,并且计算最大值最小值
            int maxX = -1;
            int minX = 999;
            for (int y = 0; y < ints.length; y++) {
                int is[] = ints[y];
                for (int x = 0; x < is.length; x++) {
                    if (is[x] == 1) {
                        ints[y][x] = checkPixel(x, y, ints);
                        if (ints[y][x] == 1) {
                            if (x > maxX) {
                                maxX = x;
                            }
                            if (x < minX) {
                                minX = x;
                            }
                        }
                    }
                }
            }
            //此处只是为了生成效果图方便观察,实际操作中不必执行
            createImage(width,height,ints);
            result = (maxX + minX) / 2;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 通过上下左右像素的对比来检测像素点是否是杂色
     *
     * @param x
     * @param y
     * @param ints
     * @return
     */
    public static int checkPixel(int x, int y, int[][] ints) {
        boolean result1 = true;
        boolean result2 = true;

        int s1 = 0;
        for (int i = 0; i < 30; i++) {
            if ((x + i) < ints[1].length && ints[y][x + i] != 1) {
                s1++;
            }
        }
        if (s1 > 15) {
            result1 = false;
        }
        int s2 = 0;
        for (int i = 0; i < 30; i++) {
            if (x - i > 0 && ints[y][x - i] != 1) {
                s2++;
            }
        }
        if (s2 > 15) {
            result2 = false;
        }
        if (result1 || result2) {
            s1 = 0;
            for (int i = 0; i < 30; i++) {
                if (y + i < ints.length && ints[y + i][x] != 1) {
                    s1++;
                }
            }
            if (s1 > 15) {
                result1 = false;
            }

            s2 = 0;
            for (int i = 0; i < 30; i++) {
                if (y - i > 0 && ints[y - i][x] != 1) {
                    s2++;
                }
            }
            if (s2 > 15) {
                result2 = false;
            }
            if (result1 || result2) {
                return 1;
            }
        }
        return 0;
    }

    /**
     * 创建图片
     * @param width
     * @param height
     * @param ints
     * @throws IOException
     */
    public static void createImage(int width, int height, int ints[][]) throws IOException {
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
        Graphics2D graphic = bi.createGraphics();
        graphic.setColor(new Color(0xffffff));

        graphic.fillRect(0, 0, width, height);

        for (int y = 0; y < ints.length; y++) {
            int is[] = ints[y];
            for (int x = 0; x < is.length; x++) {
                if (is[x] == 1) {
                    bi.setRGB(x, y, 0x0000ff);
                }
            }
        }

        Iterator<ImageWriter> it = ImageIO.getImageWritersByFormatName("png");
        ImageWriter writer = it.next();
        File f = new File("c://img.png");
        ImageOutputStream ios = ImageIO.createImageOutputStream(f);
        writer.setOutput(ios);

        writer.write(bi);
    }


}

这个程序的重点就在像素的对比上,脚本的代码很简单没有放上去的必要,上面的代码就是核心的代码,整个流程就是脚本截取图片上传到服务器上,服务器对图片进行对比操作过后返回2点的中间位置x轴的坐标.然后脚本计算转化过后进行滑动操作.整个过程从开始到滑动结束,网络正常的情况下所需时间在3秒以内,贴一下效果图:

image
image

对比图:

image

不知道这样的操作叫不叫图片二值化-_-
上面代码中有去除杂色的部分,因为截图和原图的像素除了滑块部分外并非完全相同,所以需要上下对比15个像素来确定是不是滑块中的点,下面这一张是没有去除杂色的图片:

image

因为是计算滑块区域的x轴最大值最小值,所以杂色会对计算造成干扰.

关于如何确定当前滑块图对应的哪一张原图问题,我采取的方法,也是比较笨的方法,每一张图都截取图片左下角的一小片位置然后循环区域找图,找到了就知道对应关系了.如果有更好的方法欢迎交流.

脚本的代码只是关于点色的判断,都很简单就不贴了.看下最终效果图:

image

实时桌面反应没那么快,将就着看吧.

本文中的滑块验证码只是以微信的为例子,实际很多其他平台的滑块验证码也可以通过这种方式去解决,当前这种方式,这是解决方案的一种.如果有更好的思路,欢迎评论交流.

2017-12-21
上面对比像素的代码太乱了,而且还不大好用,现在重新整理优化下

 /**
     * 通过像素对比来计算偏差值
     *
     * @param path1 原图位置
     * @param path2 滑块图位置
     * @return 偏差值
     */
    public int getDifferenceValue(String path1, String path2) {
        int result = 0;
        File file = new File(path1);
        File file1 = new File(path2);

        try {
            BufferedImage image = ImageIO.read(file);
            BufferedImage image1 = ImageIO.read(file1);

            int width = image.getWidth();
            int height = image.getHeight();

            int[][] colors = new int[width][height];
            for (int x = 1; x < width; x++) {
                for (int y = 1; y < height; y++) {
                    int color1 = image.getRGB(x, y);
                    int color2 = image1.getRGB(x, y);
                    if (color1 == color2) {
                        colors[x - 1][y - 1] = 0;
                    } else {
                        colors[x - 1][y - 1] = 1;
                    }
                }
            }
            int min = 999;
            int max = -1;
            for (int x = 0; x < colors.length; x++) {
                for (int y = 0; y < colors[x].length; y++) {
                    if (colors[x][y] == 1) {
                        colors[x][y] = checkPixel(x, y, colors);
                        if (colors[x][y] == 1) {
                            if (x > max) {
                                max = x;
                            } else if (x < min) {
                                min = x;
                            }
                        }
                    }
                }
            }
            result = (max + min) / 2;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    public int checkPixel(int x, int y, int[][] colors) {
        int result = colors[x][y];
        int num = 0;
        if ((y + 30) < colors[x].length) {
            for (int i = 1; i <= 30; i++) {
                int color = colors[x][y + i];
                if (color == 0) {
                    num += 1;
                }
            }
            if (num > 15) {
                return 0;
            }
        }
        return result;
    }


    public static void createImage(int width, int height, int ints[][], String name) throws IOException {
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphic = bi.createGraphics();
        graphic.setColor(new Color(0x003D1CFF));
        graphic.fillRect(0, 0, width, height);
        for (int x = 0; x < ints.length; x++) {
            for (int y = 0; y < ints[x].length; y++) {
                if (ints[x][y] == 1) {
                    bi.setRGB(x, y, 0xFF7F2E);
                }
            }
        }
        Iterator<ImageWriter> it = ImageIO.getImageWritersByFormatName("png");
        ImageWriter writer = it.next();
        File f = new File("c://" + name + ".png");
        ImageOutputStream ios = ImageIO.createImageOutputStream(f);
        writer.setOutput(ios);
        writer.write(bi);
    }

原文:https://blog.csdn.net/w18756901575/article/details/78615275

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,753评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,668评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,090评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,010评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,054评论 6 395
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,806评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,484评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,380评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,873评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,021评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,158评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,838评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,499评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,044评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,159评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,449评论 3 374
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,136评论 2 356

推荐阅读更多精彩内容