How to implement captcha in Android [closed]

…衆ロ難τιáo~ 提交于 2019-12-13 03:41:42

问题


I want to do captcha in my application. After my initial analysis, I found that most of them recommend to use 'Simplcaptcha'. I gave a shot at it. But my efforts are in vain. The output of 'simplecaptcha' is a AWT component. So it cannot be used in Android. Is there any way to use the same in Android by means of any conversion or is there any other good library available out there. If so can anyone guide me to a good documentation or examples. It would be a great help for me.


回答1:


A very easy to use, minimal options, on-device Captcha system for Android applications REFER




回答2:


You can use the custom view below to show captcha (tune it according to your needs like if you want to have a alphanumeric captcha or you want a skewed captcha)

public class CaptchaView extends ImageView {

private Paint mPaint;
private static String mCaptchaAsText;

public CaptchaView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

public CaptchaView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public CaptchaView(Context context) {
    super(context);
    init();
}

private void init() {
    mPaint = new Paint();
    mPaint.setColor(Color.BLACK);

    initCaptcha();
}

private static void initCaptcha() {
    mCaptchaAsText = "";
    for (int i = 0; i < 4; i++) {
        int number = (int) (Math.random() * 10);
        mCaptchaAsText += number;
    }
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    int desiredWidth = 300;
    int desiredHeight = 80;

    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);

    int width;
    int height;

    // Measure Width
    if (widthMode == MeasureSpec.EXACTLY) {
        // Must be this size
        width = widthSize;
    } else if (widthMode == MeasureSpec.AT_MOST) {
        // Can't be bigger than...
        width = Math.min(desiredWidth, widthSize);
    } else {
        // Be whatever you want
        width = desiredWidth;
    }

    // Measure Height
    if (heightMode == MeasureSpec.EXACTLY) {
        // Must be this size
        height = heightSize;
    } else if (heightMode == MeasureSpec.AT_MOST) {
        // Can't be bigger than...
        height = Math.min(desiredHeight, heightSize);
    } else {
        // Be whatever you want
        height = desiredHeight;
    }

    // MUST CALL THIS
    setMeasuredDimension(width, height);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    mPaint.setTextSize(getMeasuredHeight());
    canvas.drawText(mCaptchaAsText,
            ((canvas.getWidth() - mPaint.measureText(mCaptchaAsText)) / 2),
            getMeasuredHeight(), mPaint);
}

public static boolean match(String value) {
    if (value.equals(mCaptchaAsText)) {
        return true;
    } else {
        if (value != null && value.length() > 0)
            initCaptcha();

        return false;
    }
}
}


来源:https://stackoverflow.com/questions/17938350/how-to-implement-captcha-in-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!