我想在我的应用程序中做验证码。经过初步分析,我发现他们中的大多数人都推荐使用“Simplcaptcha”。我试了一下。但我的努力是徒劳的。'simplecapcha' 的输出是一个 AWT 组件。所以它不能在Android中使用。有没有办法通过任何转换在 Android 中使用相同的方法,或者那里有任何其他好的库可用。如果是这样,任何人都可以指导我找到一个好的文档或示例。这对我会有很大的帮助。
问问题
8454 次
2 回答
1
您可以使用下面的自定义视图来显示captcha
(根据您的需要进行调整,例如您想要 aalphanumeric captcha
还是想要 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;
}
}
}
于 2013-07-30T04:46:24.577 回答
0
一个非常易于使用、选项最少、适用于 Android 应用程序的设备上验证码系统REFER
于 2013-07-30T04:37:52.047 回答