How To Implement Captcha In Android
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
Solution 1:
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
)
publicclassCaptchaViewextendsImageView {
private Paint mPaint;
privatestatic String mCaptchaAsText;
publicCaptchaView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
publicCaptchaView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
publicCaptchaView(Context context) {
super(context);
init();
}
privatevoidinit() {
mPaint = newPaint();
mPaint.setColor(Color.BLACK);
initCaptcha();
}
privatestaticvoidinitCaptcha() {
mCaptchaAsText = "";
for (inti=0; i < 4; i++) {
intnumber= (int) (Math.random() * 10);
mCaptchaAsText += number;
}
}
@OverrideprotectedvoidonMeasure(int widthMeasureSpec, int heightMeasureSpec) {
intdesiredWidth=300;
intdesiredHeight=80;
intwidthMode= MeasureSpec.getMode(widthMeasureSpec);
intwidthSize= MeasureSpec.getSize(widthMeasureSpec);
intheightMode= MeasureSpec.getMode(heightMeasureSpec);
intheightSize= MeasureSpec.getSize(heightMeasureSpec);
int width;
int height;
// Measure Widthif (widthMode == MeasureSpec.EXACTLY) {
// Must be this size
width = widthSize;
} elseif (widthMode == MeasureSpec.AT_MOST) {
// Can't be bigger than...
width = Math.min(desiredWidth, widthSize);
} else {
// Be whatever you want
width = desiredWidth;
}
// Measure Heightif (heightMode == MeasureSpec.EXACTLY) {
// Must be this size
height = heightSize;
} elseif (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);
}
@OverrideprotectedvoidonDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setTextSize(getMeasuredHeight());
canvas.drawText(mCaptchaAsText,
((canvas.getWidth() - mPaint.measureText(mCaptchaAsText)) / 2),
getMeasuredHeight(), mPaint);
}
publicstaticbooleanmatch(String value) {
if (value.equals(mCaptchaAsText)) {
returntrue;
} else {
if (value != null && value.length() > 0)
initCaptcha();
returnfalse;
}
}
}
Solution 2:
A very easy to use, minimal options, on-device Captcha system for Android applications REFER
Post a Comment for "How To Implement Captcha In Android"