我们来设计一个简单的验证码生成程序:验证码一个由4位的数字、字母随机组合而成图像,为了避免被光学字元识别(OCR,Optical Character Recognition)之类的程序识别出图片中的数字而失去效果,我们给图像中添加上几条干扰线。

 package password;
/**
* 使用Java设计验证码生成程序
* @author hellokitty燕
*/
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 Verification { /*验证码的框*/
// 图像长度
private int width = 100;
// 图像宽度
private int height = 40;
// 验证码的长度
private int number = 4;
// 验证码随机生成的
private String password = "abcdefghijkmnpqrstuvwxyABCDEFGHIJKLMNPQRSTUVWXYZ23456789"; /**
* 获取验证码图像
*
* @return 验证码图像
*/
public BufferedImage getImage() {
/*BufferedImage 子类描述具有可访问图像数据缓冲区的 Image。
* BufferedImage(int width, int height, int imageType)构造一个类型为预定义图像类型之一的 BufferedImage。
*
* */
// 创建图像缓冲区
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 获取画笔
/*public Graphics getGraphics()*/
Graphics g = image.getGraphics(); // 设置图像背景色,填充背景矩形
/*public abstract void setColor(Color c)*/ g.setColor(getRandomColor(200, 255));//???
/*public abstract void fillRect(int x,int y,int width,int height)*/
g.fillRect(0, 0, width, height); // 画边框
g.setColor(Color.blue);
g.drawRect(0, 0, width - 1, height - 1); /* 生成随机验证码 */
int len = password.length();
// 设置验证码字体 Font(String name, int style, int size)
// HANGING_BASELINE 布置文本时,在 Devanigiri 和类似脚本中使用的基线。
g.setFont(new Font("楷体", Font.HANGING_BASELINE, 20)); // 循环生成验证码各字符????
Random random = new Random();
for (int i = 0; i < number; i++) {
// 随机生成验证码中单个字符/*public int nextInt(int n)返回一个伪随机数,它是取自此随机数生成器序列的、在 0(包括)和指定值(不包括)之间均匀分布的 int 值*/
String randStr = String.valueOf(password.charAt(random.nextInt(len)));
// 单个字符绘制宽度
int width = this.width / this.number;
// 当前字符绘制原点 ????
int x = width * i;
int y = this.height / 2 + random.nextInt(this.height / 3);
/* 将该字符画到图像中 */// ???
drawString(g, x, y, randStr); } // 画干扰线
drawLine(g, 10); // 释放画笔
g.dispose();
return image; } /**
* 画验证码字符串单个字符
*
* @param g
* 图像上下文
* @param x
* 字符 所占宽度
* @param y
* 字符所占高度
* @param randStr
* 待绘制字符串
*
*/
private void drawString(Graphics g,int width,int height,String randStr){
//private void drawString(Graphics g, int x, int y, String randStr) {
Random rand = new Random();
// 随机生成字符旋转(-30-30度)/*/*public int nextInt(int n)返回一个伪随机数,它是取自此随机数生成器序列的、在 0(包括)和指定值(不包括)之间均匀分布的 int 值*/*/
int degree = rand.nextInt(60);
if (degree > 30) {
degree = 30 - degree;
}
// 设置字体颜色
g.setColor(getRandomColor(0, 50));
// 转换Graphics2D
Graphics2D g2 = (Graphics2D) g.create();
// 平移原点到图形环境的中心,这个方法的作用实际上就是将字符串移到某一位置/*public abstract void translate(int x,int y)将 Graphics2D 上下文的原点平移到当前坐标系中的点 (x, y)。*/
g2.translate(width + rand.nextInt(5), height + rand.nextInt(5));
// 旋转文本 ( 单位是弧度)
g2.rotate(degree * Math.PI / 180);
// 画文本,特别需要注意的是,这里的笔画已经具有上次指定的一个位置,所以这里指定的位置是一个相对的位置
g2.drawString(randStr, 0, 0);
} /**
*
* 画 随机干扰线
*
* @param g
* 图形上下文(画笔)
*
* @param count
* 干扰线条数
*/
private void drawLine(Graphics g,int count){ Random random = new Random();
// 循环绘制每条干扰线
for (int j = 0; j < count; j++) {
// 设置线条随机颜色
g.setColor(getRandomColor(180, 200)); // 生成随机线条起点终点,坐标点
int x1 = random.nextInt(this.width);
int y1 = random.nextInt(this.height);
int x2 = random.nextInt(this.width);
int y2 = random.nextInt(this.height);
// 画线条
g.drawLine(x1, y1, x2, y2);
}
} /**
* 获取随机颜色
*
* @param i
* 颜色下限值
* @param j
* 颜色上限值
* @return 随机颜色对象
*/
private Color getRandomColor(int i, int j) {
if (i > j) {
int tmp = i;
i = j;
j = tmp;
}
if (j > 225) {
j = 225;
}
if (i < 0) {
i = 0;
}
int r = i + (int) (Math.random() * (j - i));
int g = i + (int) (Math.random() * (j - i));
int b = i + (int) (Math.random() * (j - i)); return new Color(r, g, b); // values in the range (0 - 255). red green blue
} public static void main(String[] args) { JFrame frame = new JFrame("验证码");
frame.setSize(200, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
Verification cation = new Verification(); JLabel lbl = new JLabel(new ImageIcon(cation.getImage()));
frame.add(lbl);
frame.setVisible(true); } }

             

使用Java设计验证码生成程序的更多相关文章

  1. java简单验证码生成程序

    下面的函数,返回的字符串就是所需验证码 public String id(){ Random ra =new Random(); st=""; String [] w= {&quo ...

  2. 工作笔记5.JAVA图片验证码

    本文主要内容为:利用JAVA图片制作验证码. 设计思路: 1.拷贝AuthImageServlet.class图片验证码 2.配置web.xml 3.JSP中,调用封装好的AuthImageServl ...

  3. java识别验证码

    所需资源下载链接(资源免费,重在分享) Tesseract:http://download.csdn.net/detail/chenyangqi/9190667 jai_imageio-1.1-alp ...

  4. 【开发技术】Java生成验证码

    Java生成验证码 为了防止用户恶意,或者使用软件外挂提交一些内容,就得用验证码来阻止,虽然这个会影响用户体验,但为了避免一些问题很多网站都使用了验证码;今天下午参考文档弄了一个验证码,这里分享一下; ...

  5. java制作验证码(java验证码小程序)

    手动制作java的验证码 Web应用验证码的组成: (1)输入框 (2)显示验证码的图片 验证码的制作流程: 生成验证码的容器使用 j2ee的servlet 生成图片需要的类: (1) Buffere ...

  6. Swing 是一个为Java设计的GUI工具包

    Swing 是一个为Java设计的GUI工具包. Swing是JAVA基础类的一部分. Swing包括了图形用户界面(GUI)器件如:文本框,按钮,分隔窗格和表. Swing提供许多比AWT更好的屏幕 ...

  7. java图形验证码

    用java实现验证码的生成,以下代码是一个controller,可以直接使用 package org.jxnd.tongxuelu.controller; import java.awt.Color; ...

  8. java实现验证码功能

    java实现验证码功能 通过java代码实现验证码功能的一般思路: 一.通过java代码生成一张验证码的图片,将验证码的图片保存到项目中的指定文件中去,代码如下: package com.util; ...

  9. java生成图片验证码(转)--封装生成图片验证码的工具类

    博客部分内容转载自 LonlySnow的博客:后台java 实现验证码生成 1.controller方法 @RequestMapping(value = "/verifycode/img&q ...

随机推荐

  1. 评论:一套Developer Express控件包 For Delphi7

    http://www.2ccc.com/idea.asp?articleid=1675 (也可以查看盒子上这个帖子的内容) Developer Express Inc 系列控件组 for Delphi ...

  2. 如何使用linq读取DataTable集合?AsQueryable() 和 AsEnumerable()区别?

    一.准备工作 引入linq和data 相关的using命名空间 DataTable dt=new DataTable();//dt的来源可以是多个地方,比如:数据库,Excel等等.我这里使用Exce ...

  3. 使用Commons Email发送邮件

    Commons Email是apache commons库中的一个组件,对java mail做了一些个封装,提供能为简化的API供开发者使用.它依赖于javax.mail . 首先下载commons- ...

  4. Java基础学习篇---------封装

    一.类和对象分配内存 二.Java中的内部类  : 可以直接去访问外部类的所有属性(包括私有成员) 1.Java中成员内部类 (1).内部类的方法可以直接访问外部类的类中的所有成员变量 (2).外部类 ...

  5. leetcode 78. 子集 JAVA

    题目: 给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: nums = [1,2,3] 输出: [ [3],   [1], ...

  6. Organising the Organisation(uva10766)(生成树计数)

    Input Output Sample Input 5 5 2 3 1 3 4 4 5 1 4 5 3 4 1 1 1 4 3 0 2 Sample Output 3 8 3 题意: 有一张图上有\( ...

  7. poj1269---直线位置关系

    题目大意:给你8个点,也就是两条直线,让你判断他们的位置关系 代码如下: #include <iostream> #include<cstdio> #include<cm ...

  8. elasticsearch索引目录设置

    path.data and path.logs If you are using the .zip or .tar.gz archives, the data and logs directories ...

  9. js面试题——找到数组中的重复元素并判断重复次数且输出重复元素

    var countArr = [1,2,3,4,5,6,3,4,3,3,7,8,9,32,1,11,2,3,3,3]; var res = {}; var maxnum=0; var max; fun ...

  10. [SqlServer] Error: 15023

    Use DataBaseName go sp_change_users_login 'update_one', 'UserName', 'UserName' 恢复数据库后,添加用户,报错号15023 ...