generate Captcha Text - Java 2D Graphics

Java examples for 2D Graphics:Image Captcha

Description

generate Captcha Text

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        int captchaLength = 2;
        System.out.println(generateCaptchaText(captchaLength));
    }//  ww  w . j  a v a 2s  .co  m

    public static String generateCaptchaText(int captchaLength) {
        // Characters to show in captcha A-Z & 1-9. 
        String saltChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
        StringBuffer captchaStrBuffer = new StringBuffer();
        java.util.Random rnd = new java.util.Random();

        // build a random captchaLength chars slat
        while (captchaStrBuffer.length() < captchaLength) {
            int index = (int) (rnd.nextFloat() * saltChars.length());
            captchaStrBuffer.append(saltChars.substring(index, index + 1));
        }

        return captchaStrBuffer.toString();
    }
}

Related Tutorials