Giter Site home page Giter Site logo

captcha's Introduction

Captcha

Android滑块拼图验证码控件

Feature

1.简单,实用,只需一两句代码即可使用
2.采用策略模式为使用者开放自定义拼图样式策略,对拼图样式(拼图形状、视觉效果)进行定制
3.自选模式,无滑动条模式(手触移动),有滑动条模式
4.通过监听器回调用户可获得验证通过时间和验证失败的次数以对这些情况进行进一步处理(如对帐号进行封锁,禁止部分操作)提高安全性
5.支持加载网络图片

Method

方法名 描述 版本限制
setMode(int mode) 设置验证模式(默认为MODE_BAR) 1.0.8开始
setMaxFailedCount(int count) 设置最大验证失败次数(默认为5次) 1.0.8开始
setBitmap(Bitmap bitmap) 设置图片 1.0.8开始
setBitmap(int drawableId) 设置图片 1.1.1开始
setBitmap(String imgUrl) 设置图片 1.1.2开始
setBlockSize(int blockSize) 设置滑块图片大小,单位px(默认50dp) 1.0.8开始
setCaptchaStrategy(CaptchaStrategy strategy) 设置验证策略 1.0.5开始
setSeekBarStyle(int progressDrawable, int thumbDrawable) 设置滑动条样式 1.0.5开始
setCaptchaListener(CaptchaListener listener) 设置验证回调 1.0.0开始

Attributes(captcha布局文件中调用)

属性名 类型 描述
mode enum mode_bar:带滑动条 mode_nonbar:不带滑动条
src reference 图片
progressDrawable reference 滑动条progress样式
thumbDrawable reference 滑动条触动点样式
max_fail_count integer 最大验证失败次数
blockSize dimension 缺块大小

Version Update

版本号 描述
v1.0.5 可用基本功能
v1.0.8 添加可定制属性,查看Method
v1.0.9 对回调CaptchaListener返回参数修改,以让使用者自定义图片底部阴影文本
v1.1.0 修复重写CaptchaStrategy类编译时报错
v1.1.1 1.优化体验 2.添加刷新按钮,取消原来验证失败重置的设定,改为用刷新按钮重置
v1.1.2 添加支持网络图片

Usage

1.在app的build.gradle添加依赖

compile 'com.luozm.captcha:captcha:1.1.2'

2.将Captcha添加至布局

<com.luozm.captcha.Captcha
        android:id="@+id/captCha"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:src="@mipmap/cat"/>

3.添加Java代码

   captcha = (Captcha) findViewById(R.id.captCha);
   captcha.setBitmap(url);
   captcha.setCaptchaListener(new Captcha.CaptchaListener() {
            @Override
            public String onAccess(long time) {
                Toast.makeText(MainActivity.this,"验证成功",Toast.LENGTH_SHORT).show();
                return "验证通过,耗时"+time+"毫秒";
            }

            @Override
            public String onFailed(int failedCount) {
                Toast.makeText(MainActivity.this,"验证失败",Toast.LENGTH_SHORT).show();
                return "验证失败,已失败"+failedCount+"次";
            }
            
            @Override
            public String onMaxFailed() {
                Toast.makeText(MainActivity.this,"验证超过次数,你的帐号被封锁",Toast.LENGTH_SHORT).show();
                  return "验证失败,帐号已封锁";
            }
        });

4.(可选)自定义拼图样式

1.编写策略类,继承CaptchaStrategy类,重写策略方法,具体可参考DefaultCaptchaStrategy类

public abstract class CaptchaStrategy {

  protected Context mContext;

  public CaptchaStrategy(Context ctx) {
      this.mContext = ctx;
  }

  protected Context getContext() {
      return mContext;
  }

  /**
   * 定义拼图缺块的形状
   *
   * @param blockSize 单位dp,注意转化为px,缺块的大小,注意Path的边界不要超出此大小
   * @return path of the shape
   */
  public abstract Path getBlockShape(int blockSize);

  /**
   * 根据整张拼图的宽高和拼图缺块大小定义拼图缺块的位置
   *
   * @param width  picture width
   * @param height picture height
   * @return position info of the block
   */
  public abstract PositionInfo getBlockPostionInfo(int width, int height, int blockSize);

  /**
    * 定义滑块图片的位置信息(只有设置为无滑动条模式有用并建议重写)
    *
   * @param width  picture width
   * @param height picture height
   * @return position info of the block
   */
  public PositionInfo getPositionInfoForSwipeBlock(int width, int height, int blockSize){
      return getBlockPostionInfo(width,height,blockSize);
  }

  /**
   * 定义拼图缺失部分阴影的Paint
   */
  public abstract Paint getBlockShadowPaint();

  /**
   * 获得拼图缺块图片的Paint
   */
  public abstract Paint getBlockBitmapPaint();

  /**
   * 装饰滑块图片,在绘制图片后执行,即绘制滑块前景
   * @params canvas
   * @params shape   缺块的形状
   */
  public void decoreateSwipeBlockBitmap(Canvas canvas, Path shape) {

  }
}

2.添加Java代码

captCha.setCaptchaStrategy(new XXXCaptchaStrategy(context));

5.(可选)自定义滑块条

  与Seekbar自定义样式一样

<com.luozm.captcha.Captcha
        android:id="@+id/captCha"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:progressDrawable="@drawable/progress"
        app:thumbDrawable="@drawable/thumb"
        app:src="@mipmap/cat"/>
captcha.setSeekBarStyle(R.drawable.po_seekbar,R.drawable.thumb);

博文地址

http://blog.csdn.net/sdfsdfdfa/article/details/79120665

使用者须知

若使用过程中发现bug或想提出修改建议,请Issues留言或者自行修复并提交Pull request。我会在最快时间内处理。万分感谢!

captcha's People

Contributors

luozhanming avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

captcha's Issues

控件大小可否自适应.

控件大小设置固定的话会显示不全.下面的拖动按钮设置了图片也是无法调整大小的.希望大神有空可以优化一下.

关于reset方法

关于reset方法现在只是把滑动条的位置和图片的缺口复原了,然后上面的文字和图片并没有隐藏,而是还在显示上次的操作结果 能不能全部重置

对接口没有任何保护作用

只是实现了界面,但实际上验证的功能没有,用了等于白用,机器不会操作你的界面的,想滥用你的接口直接拦截请求去模拟就行了,前端界面直接绕过就行,写的这个东西不和后台配合是验证不了是人是机,用了这个非但没保护接口,反而给用户体验上带来阻碍.

控件写的不错,但没有实际作用

适配问题

再有的机型上图片的绘制 有问题
是大小的问题 图片块绘制出来比背景填充块大 导致比对不到

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.