验证码

原创 2006年05月18日 17:06:00

*
* Created on 2005-8-12
*
*
*/
package hp.alex.code;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
* @author Wan,bin
*

*/
public class RandomCode extends Panel {

  private static Random random = null;
  //content of the Code
  private String code = null;
  //height of the code
  public final static int CODE_HEIGHT = 20;
  //code width split
  public final static int CODE_WIDTH_SPLIT = 15;
  //code width single
  public final static int SINGLE_CODE_WIDTH = 6;
  //random line number
  public final static int RANDOM_LINE_NUM = 155;
  //code font
  public final static Font font = new Font("Arial", Font.PLAIN, 18);
  //code number
  private int codeNum = 0;
  //all length
  private int width = 0;
  //height
  private int height = 0;

  static {
    //initialize the random
    random = new Random();
  }

  /**
   *
   * @param start
   *            start of the color
   * @param finish
   *            end of the color
   * @return a new Color with the random
   *
   * return a new random color over the given range
   */
  private Color getColorOverRange(int start, int finish) {
    final int range = finish - start;
    if (range >= 0) {
      if (start > 255)
        start = 255;
      if (finish > 255)
        finish = 255;
      int r = start + random.nextInt(range);
      int g = start + random.nextInt(range);
      int b = start + random.nextInt(range);
      return new Color(r, g, b);
    } else {
      return Color.BLACK;
    }
  }

  /**
   *
   * @param length
   * @return image
   *
   * return the image with the code
   */

  private BufferedImage drawImage(int length) {
    width = (length + 1) * CODE_WIDTH_SPLIT;
    height = CODE_HEIGHT;
    BufferedImage image = new BufferedImage(width, height,
        BufferedImage.TYPE_INT_RGB);

    Graphics g = image.getGraphics();

    //set the background color
    g.setColor(getColorOverRange(200, 250));
    g.fillRect(0, 0, width, height);

    //set font of the code
    g.setFont(font);

    //draw the border
    g.setColor(new Color(255, 255, 255));
    g.drawRect(0, 0, width - 1, height - 1);

    //random generate some line in order not to get the code by other
    // program easily.
    g.setColor(getColorOverRange(160, 200));
    for (int i = 0; i < RANDOM_LINE_NUM; i++) {
      int x = random.nextInt(width);
      int y = random.nextInt(height);
      int xl = random.nextInt(12);
      int yl = random.nextInt(12);
      g.drawLine(x, y, x + xl, y + yl);
    }

    //generate the random code
    StringBuffer buffer = new StringBuffer();
    for (int i = 0; i < length; i++) {
      String rand = String.valueOf(random.nextInt(10));
      buffer.append(rand);
      //draw the string to the image
      g.setColor(new Color(20 + random.nextInt(110), 20 + random
          .nextInt(110), 20 + random.nextInt(110)));
      g.drawString(rand, CODE_WIDTH_SPLIT * i + SINGLE_CODE_WIDTH,
          CODE_HEIGHT - 4);
    }
    code = buffer.toString();
    g.dispose();
    return image;
  }

  /*
   * (non-Javadoc)
   *
   * @see java.awt.Component#paint(java.awt.Graphics)
   */
  public void paint(Graphics g) {
    if (codeNum <= 0)
      codeNum = 4;
    g.drawImage(drawImage(codeNum), 0, 0, width, height, null);
  }
  public RandomCode(int codeNum) {
    this.codeNum = codeNum;
  }
  /**
   * @return Returns the code.
   */
  public String getCode() {
    return code;
  }
}

 


 

/*
* Created on 2005-8-12
*
*/
package hp.alex.code;

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Panel;

/**
* @author Wan,bin

*/
public class App extends Applet {
  /*
   * (non-Javadoc)
   *
   * @see java.applet.Applet#init()
   */
  public void init() {
    // TODO Auto-generated method stub
    Panel panel = new RandomCode(5);
    setLayout(new BorderLayout());
    add(panel, BorderLayout.CENTER);
    setVisible(true);
    setSize(100,20);
  }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

数字英文验证码jar包

  • 2017-09-14 18:00
  • 30KB
  • 下载

发送邮箱验证码(带html格式)

场景:用于用户注册、密码重置等。 1 . 邮件html模板 emailTemplet.html </

腾讯验证码标注样本-4

  • 2017-09-17 11:07
  • 50MB
  • 下载

python使用pytesseract识别验证码的方法

1、系统环境 OS X  10.7MacBook Pro(13英寸,2012年初期)Python 2.7 2、需要软件包     a、需要安装PIL以及pytesseract库     ...

腾讯验证码标注样本-3

  • 2017-09-17 11:03
  • 50MB
  • 下载

android 验证码

  • 2016-06-28 10:42
  • 5.82MB
  • 下载

随机生成图片验证码

生成随机验证码直接贴代码,注释的很清楚了。VerifyCode.javapackage cn.testone.image;import java.awt.BasicStroke; import jav...

java 验证码

  • 2016-01-27 09:48
  • 55KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)