美文网首页
滑动验证码

滑动验证码

作者: 邓立_全栈UncleLi | 来源:发表于2019-07-18 17:40 被阅读0次

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

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

相关文章

  • 网易滑动验证码破解

    1.滑动验证码 前面介绍了利用 tesserocr 来识别简单的图形验证码,和利用openCV识别滑动验证码的缺口...

  • python爬虫高级技术之验证码篇-滑动验证码识别技术!

    滑动验证码介绍 本篇博客涉及到的验证码为滑动验证码,不同于极验证,本验证码难度略低,需要的将滑块拖动到矩形区域右侧...

  • B站登陆(滑动验证码)

    B站登陆(滑动验证码)

  • 反滑块验证

    简介 滑动验证码: 滑动验证码也可以叫做行为验证,其中最出名的就是极验。 作者:Simon0903 链接:http...

  • python爬虫之模拟移动

    爬虫的一大难点就是破解验证码。验证码大致上分为文字识别、滑动、文字点击、图像识别等,本文讲的是其中的滑动验证码。滑...

  • python爬虫之滑动验证码[完整版]

    爬虫的一大难点就是破解验证码。验证码大致上分为文字识别、滑动、文字点击、图像识别等,本文讲的是其中的滑动验证码。滑...

  • python爬虫之图像对比

    爬虫的一大难点就是破解验证码。验证码大致上分为文字识别、滑动、文字点击、图像识别等,本文讲的是其中的滑动验证码。滑...

  • python爬虫之轨迹算法

    爬虫的一大难点就是破解验证码。验证码大致上分为文字识别、滑动、文字点击、图像识别等,本文讲的是其中的滑动验证码。滑...

  • Python爬虫教程:验证码识别

    常见反爬虫手段:验证码1.简单图片,扭曲数字验证码2.中文顺序点击3.动态验证码4.滑动验证:滑动小方块到缺口5....

  • Python实现自动登录,强行突破图形验证码!

    验证码有图形验证码、极验滑动验证码、点触验证码、宫格验证码。这回重点讲讲图形验证码的识别。 虽说图形验证码最简单,...

网友评论

      本文标题:滑动验证码

      本文链接:https://www.haomeiwen.com/subject/jeqrlctx.html