检查验证码

时间:2017-08-16 06:34:56

标签: android captcha

我正在尝试检查我的注册表单中的验证码,该表单是从java文件中在项目中静态生成的。但是我无法跟踪检查验证码的情况,因为它一直显示错误。无论验证码是否匹配,它总是显示无效的验证码。下面是代码。

mainactivity.java

                      Submit_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //isValidData();
            if (isValidData()){
                if (textCaptcha.checkAnswer(Usercapt.getText().toString().trim())) {
                    /*AlertDialog.Builder alt = new AlertDialog.Builder(MainActivity.this);
                    alt.setMessage("Invalid Captcha");
                    alt.setCancelable(true);
                    alt.setPositiveButton("ok", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
                    AlertDialog alertDialog = alt.create();
                    alertDialog.show();*/
                    Toast.makeText(MainActivity.this, "captch match", Toast.LENGTH_SHORT).show();

                } else{
                   /* Toast.makeText(MainActivity.this, "Successful Registration", Toast.LENGTH_SHORT).show();
                    Intent i = new Intent(MainActivity.this, Login_Activity.class);
                    startActivity(i);*/
                    Toast.makeText(MainActivity.this, "captcha not match", Toast.LENGTH_SHORT).show();

                }
            } else {

                /*here the code to save the data in data base will be written*/
                Toast.makeText(MainActivity.this, "Registration Failed, Register again", Toast.LENGTH_SHORT).show();

            }

        }
    });

Captcha.java

       package com.mws.tms_application;

   import android.graphics.Bitmap;
 import android.graphics.Color;

 import java.util.List;
 import java.util.Random;



public abstract class Captcha {
protected Bitmap image;
protected String answer = "";
private int width;
protected int height;
protected int x = 0;
protected int y = 0;
protected static List usedColors ;

protected abstract Bitmap image();

public static int color(){
    Random r = new Random();
    int number;
    do{
        number = r.nextInt(9);
    }while(usedColors.contains(number));
    usedColors.add(number);
    switch(number){
        case 0: return Color.BLACK;
        case 1: return Color.BLUE;
        case 2: return Color.CYAN;
        case 3: return Color.DKGRAY;
        case 4: return Color.GRAY;
        case 5: return Color.GREEN;
        case 6: return Color.MAGENTA;
        case 7: return Color.RED;
        case 8: return Color.YELLOW;
        case 9: return Color.WHITE;
        default: return Color.WHITE;
    }
}

public int getWidth(){
    return this.width;
}

public void setWidth(int width){
    if(width > 0 && width < 10000){
        this.width = width;
    }else{
        this.width = 300;
    }
}

public int getHeight(){
    return this.height;
}

public void setHeight(int height){
    if(height > 0 && height < 10000){
        this.height = height;
    }else{
        this.height = 100;
    }
}

public Bitmap getImage() {
    return this.image;
}

public boolean checkAnswer(String ans) {
    /*System.out.println("ans received here"+ans);
    System.out.println("captcha"+answer);*/
    return (ans.equals(this.answer));


}
}

textcaptcha.java

         package com.mws.tms_application;

 import android.graphics.Bitmap;
 import android.graphics.Canvas;
 import android.graphics.LinearGradient;
 import android.graphics.Paint;
 import android.graphics.Shader;

 import java.io.CharArrayWriter;
 import java.util.ArrayList;
 import java.util.Random;



 public class TextCaptcha extends Captcha {

protected TextOptions options;
private int wordLength;
private char mCh;

public enum TextOptions {
    UPPERCASE_ONLY,
    LOWERCASE_ONLY,
    NUMBERS_ONLY,
    LETTERS_ONLY,
    NUMBERS_AND_LETTERS
}

public TextCaptcha(int wordLength, TextOptions opt) {
    new TextCaptcha(0, 0, wordLength, opt);
}

public TextCaptcha(int width, int height, int wordLength, TextOptions opt) {
    setHeight(height);
    setWidth(width);
    this.options = opt;
    usedColors = new ArrayList<>();
    this.wordLength = wordLength;
    this.image = image();
}

@Override
protected Bitmap image() {
    LinearGradient gradient = new LinearGradient(0, 0, getWidth() / this.wordLength, getHeight() / 2, color(), color(), Shader.TileMode.CLAMP);
    Paint p = new Paint();
    p.setDither(true);
    p.setShader(gradient);
    Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(bitmap);
    c.drawRect(0, 0, getWidth(), getHeight(), p);
    Paint tp = new Paint();
    tp.setDither(true);
    tp.setTextSize(getWidth() / getHeight() * 20);

    Random r = new Random(System.currentTimeMillis());
    CharArrayWriter cab = new CharArrayWriter();
    this.answer = " ";
    for (int i = 0; i < this.wordLength; i++) {
        char ch = ' ';
        switch (options) {
            case UPPERCASE_ONLY:
                ch = (char) (r.nextInt(91 - 65) + (65));
                break;
            case LOWERCASE_ONLY:
                ch = (char) (r.nextInt(123 - 97) + (97));
                break;
            case NUMBERS_ONLY:
                ch = (char) (r.nextInt(58 - 49) + (49));
                break;
            case LETTERS_ONLY:
                ch = getLetters(r);
                break;
            case NUMBERS_AND_LETTERS:
                ch = getLettersNumbers(r);
                break;
            default:
                ch = getLettersNumbers(r);
                break;
        }
        cab.append(ch);
        this.answer += ch;
    }

    char[] data = cab.toCharArray();
    for (int i = 0; i < data.length; i++) {
        this.x += (30 - (3 * this.wordLength)) + (Math.abs(r.nextInt()) % (65 - (1.2 * this.wordLength)));
        this.y = 50 + Math.abs(r.nextInt()) % 50;
        Canvas cc = new Canvas(bitmap);
        tp.setTextSkewX(r.nextFloat() - r.nextFloat());
        tp.setColor(color());
        cc.drawText(data, i, 1, this.x, this.y, tp);
        tp.setTextSkewX(0);
    }
    return bitmap;
}

private char getLetters(Random r) {
    int rint = (r.nextInt(123 - 65) + (65));
    if (((rint > 90) && (rint < 97)))
        getLetters(r);
    else
        mCh = (char) rint;
    return mCh;
}

private char getLettersNumbers(Random r) {
    int rint = (r.nextInt(123 - 49) + (49));

    if (((rint > 90) && (rint < 97)))
        getLettersNumbers(r);
    else if (((rint > 57) && (rint < 65)))
        getLettersNumbers(r);
    else
        mCh = (char) rint;
    return mCh;
}
}

1 个答案:

答案 0 :(得分:0)

就是这样

用空格初始化answer,而不是空字符串:

this.answer = " ";

答案将填写如下“1234567” 注意到领先的空间

用空字符串初始化:

this.answer = "";

或在trim()上使用answer进行比较时

public boolean checkAnswer(String ans) {
    /*System.out.println("ans received here"+ans);
    System.out.println("captcha"+answer);*/
    return (ans.equals(this.answer.trim()));
}

推荐的方法是使用空字符串初始化answer