⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 defaulttextcreator.java

📁 基于Java平台的、功能强大的验证码图片生成工具
💻 JAVA
字号:
/*
 * Created on Sep 10, 2005
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package nl.captcha.text.imp;

import java.util.Properties;
import java.util.Random;
import java.util.StringTokenizer;

import nl.captcha.servlet.Constants;
import nl.captcha.text.TextProducer;


/**
 * @author Administrator
 * Default TextProducer implementation. Takes character array and
 * produces a random String with the getText() method.
 */
public class DefaultTextCreator implements TextProducer {

	public Random generator = new Random();
	private Properties properties;
	private int capLength = 5;
	private char[] captchars =
		new char[] {
			'a',
			'b',
			'c',
			'd',
			'e',
			'2',
			'3',
			'4',
			'5',
			'6',
			'7',
			'8',
			'g',
			'f',
			'y',
			'n',
			'm',
			'n',
			'p',
			'w',
			'x' };
	
	
	public DefaultTextCreator() {
			
	}
	
	public DefaultTextCreator(Properties properties) {
		super();
		this.setProperties(properties);
	}
	
	public void setCharArray(char[] chars) {
		this.captchars = chars;
	}
	 
	public void setProperties(Properties props) {
		this.properties = props;
		if (props != null ) {
			String charString = props.getProperty(Constants.SIMPLE_CAPTCHA_TEXTPRODUCER_CHARR);
			if (charString != null && !charString.equals("")) {
				
				StringTokenizer token = new StringTokenizer(charString, ",");
				this.captchars = new char[token.countTokens()];
				int cnt = 0;
				while (token.hasMoreTokens()) {
					captchars[cnt] = ((String)token.nextElement()).toCharArray()[0];
					cnt++;
				}
				
				
			}
			
			String l = props.getProperty(Constants.SIMPLE_CAPTCHA_TEXTPRODUCER_CHARRL);
			if (l != null && !l.equals("")){
				try {
					capLength = Integer.parseInt(l);
				}catch (Exception e) {
					// TODO: handle exception
				}
				if (capLength < 2 ) capLength = 5;
			}
		}
		
	}
	public String getText() {
		int car = captchars.length - 1;

		String capText = "";
		for (int i = 0; i < capLength; i++) {
			capText += captchars[generator.nextInt(car) + 1];
		}

		return capText;

	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -