2013-07-30 62 views
0

我想在我的應用程序中執行驗證碼。在我最初的分析後,我發現他們大多數人推薦使用'Simplcaptcha'。我給了它一個鏡頭。但我的努力是徒勞的。 'simplecaptcha'的輸出是一個AWT組件。所以它不能用於Android。有什麼方法可以通過任何轉換的方式在Android中使用它,或者是否有任何其他可用的良好庫?如果有的話,任何人都可以指導我一個很好的文檔或例子。這對我來說是一個很大的幫助。如何在Android中實現驗證碼

回答

0

一個非常容易使用,最少的選項,設備上的驗證碼Android應用程序系統REFER

1

您可以使用下面的自定義視圖顯示了根據您的需求captcha(調一樣,如果你想有alphanumeric captcha或你想要一個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; 
    } 
} 
} 
+0

我怎樣才能重新加載此驗證碼?我怎樣才能使用它作爲字母數字captcha? –

+0

檢查此http://stackoverflow.com/a/41218856/6295668 –