| | |
| | | |
| | | public class ImageVerificationCode { |
| | | |
| | | private int weight = 100; //验证码图片的长和宽 |
| | | private int height = 40; |
| | | private int weight = 400; //验证码图片的长和宽 |
| | | private int height = 160; |
| | | private String text; //用来保存验证码的文本内容 |
| | | private Random r = new Random(); //获取随机数对象 |
| | | //private String[] fontNames = {"宋体", "华文楷体", "黑体", "微软雅黑", "楷体_GB2312"}; //字体数组 |
| | |
| | | int index = r.nextInt(fontNames.length); //获取随机的字体 |
| | | String fontName = fontNames[index]; |
| | | int style = r.nextInt(4); //随机获取字体的样式,0是无样式,1是加粗,2是斜体,3是加粗加斜体 |
| | | int size = r.nextInt(10) + 24; //随机获取字体的大小 |
| | | int size = r.nextInt(130) + 36; //随机获取字体的大小 |
| | | return new Font(fontName, style, size); //返回一个随机的字体 |
| | | } |
| | | |
| | |
| | | * @param image |
| | | */ |
| | | private void drawLine(BufferedImage image) { |
| | | int num = r.nextInt(10); //定义干扰线的数量 |
| | | int num = r.nextInt(15); //定义干扰线的数量 |
| | | Graphics2D g = (Graphics2D) image.getGraphics(); |
| | | for (int i = 0; i < num; i++) { |
| | | int x1 = r.nextInt(weight); |
| | |
| | | float x = i * 1.0F * weight / 4; //定义字符的x坐标 |
| | | g.setFont(randomFont()); //设置字体,随机 |
| | | g.setColor(randomColor()); //设置颜色,随机 |
| | | g.drawString(s, x, height - 5); |
| | | g.drawString(s, x, height - 10); |
| | | } |
| | | this.text = sb.toString(); |
| | | drawLine(image); |