我们来设计一个简单的验证码生成程序:验证码一个由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. 动态的把固定格式的json数据以菜单形式插入

    var root=$("#side-menu"); $(menuData).each(function(i,n){ var top1Li=$("<li>< ...

  2. maven下@override标签失效

    经常遇见此问题,现记录如下,以备下次查阅. 在pom文件添加配置: <plugin> <groupId>org.apache.maven.plugins</groupId ...

  3. Android-WebViewUtils-工具类

    WebViewUtils-工具类是专门处理,Android API 中的WebView使用,公共方法抽取定义: package common.library.utils; import android ...

  4. 【WinRT】让控件飞,WinRT 中实现 web 中的 dragable 效果

    由于在 xaml 体系中,控件没有传统 WebForm 中的 Left.Top.Right.Bottom 这些属性,取而代之的是按比例(像 Grid)等等的响应布局.但是,传统的这些设置 Left.T ...

  5. Windows Phone 放开政策 - 应用内支付(IAP)可加入三方支付

    Windows Phone 应用商店在 今年(2013)11月04号 修改了商店政策 允许公司账户的应用使用三方支付SDK. 通过 App certification requirements cha ...

  6. log.debug(e.getMessage());

    private static final Log log = LogFactory.getLog(AbcAction.class); @ManagedProperty(name = "abc ...

  7. TFS:需要包管理许可证才能进一步操作You need a Package Management license to go further

    问题: 为什么团队成员没有查看包管理服务的权限?如下图: 答案: TFS系统的访问级别设置,决定在默认配置中用户是否有包管理的访问权限.默认配置中,只有"VS Enterprise" ...

  8. Tempdb--关于表变量的一点疑问和测试

    在思考表变量与临时表之间区别时,表变量不会受事务回滚的影响,那么是否意味着表变量无需写入日志呢? 测试方式: 分别对tempdb上的用户表/临时表/表变量 进行10000次插入,查看日志写入次数,使用 ...

  9. SSE sqrt还是比C math库的sqrtf快了不少

    #include <stdio.h> #include <xmmintrin.h> #define NOMINMAX #include <windows.h> #i ...

  10. Redis持久化————AOF与RDB模式

      1.        官方说明:  By default Redis asynchronously dumps the dataset on disk. This mode is good enou ...