码迷,mamicode.com
首页 > 其他好文 > 详细

验证码程序

时间:2018-10-08 13:01:00      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:tca   pos   buffered   exti   red   dispose   setfont   add   and   

package Captcha;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Captcha {
  // 图像宽度
    private int width = 80;
    // 图像高度
    private int height = 30;
    // 验证码字符长度
    private int length = 4;
    // 随机生成验证码基础字符串
    private final String baseCharacters = "abcdefghijkmnpqrstuvwxyABCDEFGHIJKLMNPQRSTUVWXYZ23456789";
    public BufferedImage getCaptchaImage() {
        BufferedImage img = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);
        Graphics g = img.getGraphics();
        g.setColor(getRandomColor(200, 255));
        g.fillRect(0, 0, width, height);
        g.setColor(Color.BLACK);
        g.drawRect(0, 0, width - 1, height - 1);
        int len = baseCharacters.length(); 
        g.setFont(new Font("楷体", Font.HANGING_BASELINE, 24));
        Random rand = new Random();
        for (int i = 0; i < length; i++) {
            String randStr = String.valueOf(baseCharacters.charAt(rand
                    .nextInt(len)));
            int width = this.width / this.length;
            int x = width * i;
            int y = this.height / 2 + rand.nextInt(this.height / 3);
            drawString(g, x, y, randStr);
        }
        g.dispose();
        return img;
    }
    private void drawString(Graphics g, int width, int height, String str) {
        Random rand = new Random();
        int degree = rand.nextInt(60);
        if (degree > 30)
            degree = 30 - degree;
             g.setColor(getRandomColor(0, 80));
        Graphics2D g2 = (Graphics2D) g.create();
        g2.translate(width + rand.nextInt(5), height + rand.nextInt(5));
        g2.rotate(degree * Math.PI / 180);
        g2.drawString(str, 0, 0);
    }
   private Color getRandomColor(int minimum, int maximum) {
        if (minimum > maximum) {
            int tmp = minimum;
            minimum = maximum;
            maximum = tmp;
        }
        if (maximum > 255)
            maximum = 255;
        if (minimum < 0)
            minimum = 0;
        int r = minimum + (int) (Math.random() * (maximum - minimum));
        int g = minimum + (int) (Math.random() * (maximum - minimum));
        int b = minimum + (int) (Math.random() * (maximum - minimum));
        return new Color(r, g, b);
    }
    public static void main(String[] args) {
        Captcha captcha = new Captcha();
        JFrame frame = new JFrame("验证码");
        frame.setSize(300, 200);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JLabel lbl = new JLabel(new ImageIcon(captcha.getCaptchaImage()));
        frame.add(lbl);
        frame.setVisible(true);
    }
}
 

验证码程序

标签:tca   pos   buffered   exti   red   dispose   setfont   add   and   

原文地址:https://www.cnblogs.com/zjl-0217/p/9753711.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!