Java代码生成图片验证码实现

   介绍一个使用Java代码生成随机图片验证码的示例

  可以直接配置成Servlet,在页面直接调用

  Java代码随机生成图片验证码

  package com.rchm.util.images;

  import java.awt.Color;

  import java.awt.Font;

  import java.awt.Graphics2D;

  import java.awt.image.BufferedImage;

  import java.io.FileOutputStream;

  import java.io.IOException;

  import java.io.OutputStream;

  import java.util.Random;

  import javax.imageio.ImageIO;

  /**

  * 验证码生成器

  */

  public class ValidateCode {

  // 图片的宽度。

  private int width = 160;

  // 图片的高度。

  private int height = 40;

  // 验证码字符个数

  private int codeCount = 5;

  // 验证码干扰线数

  private int lineCount = 150;

  // 验证码

  private static String code = null;

  // 验证码图片Buffer

  private BufferedImage buffImg=null;

  private char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J','K', 'L',

  'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W','X', 'Y',

  'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

  public ValidateCode() {

  this.createCode();

  }

  /**

  *

  * @param width 图片宽

  * @param height 图片高

  */

  public ValidateCode(int width,int height) {

  this.width=width;

  this.height=height;

  this.createCode();

  }

  /**

  *

  * @param width 图片宽

  * @param height 图片高

  * @param codeCount 字符个数

  * @param lineCount 干扰线条数

  */

  public ValidateCode(int width,int height,int codeCount,int lineCount) {

  this.width=width;

  this.height=height;

  this.codeCount=codeCount;

  this.lineCount=lineCount;

  this.createCode();

  }

  public void createCode() {

  int x = 0,fontHeight=0,codeY=0;

  int red = 0, green = 0, blue = 0;

  x = width / (codeCount +2);//每个字符的宽度

  fontHeight = height - 2;//字体的高度

  codeY = height - 4;

  // 图像buffer

  buffImg = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);

  Graphics2D g = buffImg.createGraphics();

  // 生成随机数

  Random random = new Random();

  // 将图像填充为白色

  g.setColor(Color.WHITE);

  g.fillRect(0, 0, width, height);

  // 创建字体

  ImgFontByte imgFont=new ImgFontByte();

  Font font =imgFont.getFont(fontHeight);

  g.setFont(font);

  for (int i = 0; i

  int xs = random.nextInt(width);

  int ys = random.nextInt(height);

  int xe = xs+random.nextInt(width/8);

  int ye = ys+random.nextInt(height/8);

  red = random.nextInt(255);

  green = random.nextInt(255);

  blue = random.nextInt(255);

  g.setColor(new Color(red, green, blue));

  g.drawLine(xs, ys, xe, ye);

  }

  // randomCode记录随机产生的验证码

  StringBuffer randomCode = new StringBuffer();

  // 随机产生codeCount个字符的验证码。

  for (int i = 0; i

  String strRand = String.valueOf(codeSequence[random.nextInt(codeSequence.length)]);

  // 产生随机的颜色值,让输出的每个字符的颜色值都将不同。

  red = random.nextInt(255);

  green = random.nextInt(255);

  blue = random.nextInt(255);

  g.setColor(new Color(red, green, blue));

  g.drawString(strRand, (i + 1) * x, codeY);

  // 将产生的四个随机数组合在一起。

  randomCode.append(strRand);

  }

  // 将四位数字的验证码保存到Session中。

  code = randomCode.toString();

  }

  public void write(String path) throws IOException {

  OutputStream sos = new FileOutputStream(path);

  this.write(sos);

  }

  public void write(OutputStream sos) throws IOException {

  ImageIO.write(buffImg, "png", sos);

  sos.close();

  }

  public BufferedImage getBuffImg() {

  return buffImg;

  }

  public static String getCode() {

  return code;

  }

  }

  在 servlet 中使用该类:

  package com.rchm.util.images;

  import java.io.IOException;

  import javax.servlet.ServletException;

  import javax.servlet.http.HttpServlet;

  import javax.servlet.http.HttpServletRequest;

  import javax.servlet.http.HttpServletResponse;

  import javax.servlet.http.HttpSession;

  public class ValidateCodeServlet extends HttpServlet {

  private static final long serialVersionUID = 1L;

  protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

  response.setContentType("image/jpeg");

  response.setHeader("Pragma", "no-cache");

  response.setHeader("Cache-Control", "no-cache");

  response.setDateHeader("Expires", 0);

  ValidateCode vCode = new ValidateCode(100,30,4,100);

  HttpSession session = request.getSession();

  session.removeAttribute("validateCode");

  vCode.write(response.getOutputStream());

  session.setAttribute("validateCode", vCode.getCode());

  vCode.write(response.getOutputStream());

  }

  }

  在 web.xml配置Servlet访问路径:

  validateCodeServlet

  class>com.rchm.util.images.ValidateCodeServletclass>

  validateCodeServlet

时间: 2024-08-22 14:44:00

Java代码生成图片验证码实现的相关文章

Java实现图片验证码具体代码_java

本文实例为大家分享了Java图片验证码代码,供大家参考,具体内容如下 网页显示效果: index.jsp 使用两种方式强制图片更新: 1.设置图片输出时不缓存. 2.用js改变请求的地址. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>第一个web项目的主页</

java生成图片验证码实例代码_java

关于java图片验证码的文章最近更新了不少,帮助大家掌握java验证码的生成技术,下文为大家分享了java生成图片验证码最简单的方法,供大家参考. 现在各行业在定制系统时都会考虑到机器注册,现在最有效的方式就是输入验证.现在的验证方式有很多种: 一.问题验证,其实也是图片验证,在图片上生成问题,然后输入框输入答案. 二.图片验证,输入图片上展示的文字信息. 三.短信验证,比较繁杂,用户也不怎么喜欢. 四.还有就是百度最新的验证方式.图片上生成文字,然后出现一个文字点击框,选择你在验证图片上看到的

new- 发送验证码到手机的java代码 有谁知道这是用的哪个服务商的服务?

问题描述 发送验证码到手机的java代码 有谁知道这是用的哪个服务商的服务? com.shovesoft.SMS.sendMSM(IConstants.SEND_PHONE_ACCOUNT IConstants.SEND_PHONE_PASSWORD content userPhone);下面的配置参数send_phone_account=J01370send_phone_password=635210/** * 发送验证码到手机 * @param userCode *@param userPh

使用java生成字母验证码_java

本文实例为大家分享了java生成字母验证码的具体代码,供大家参考,具体内容如下 import java.awt.BasicStroke; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.OutputStream; import java.ut

Android自定义View绘制随机生成图片验证码_Android

本篇文章讲的是Android自定义View之随机生成图片验证码,开发中我们会经常需要随机生成图片验证码,但是这个是其次,主要还是想总结一些自定义View的开发过程以及一些需要注意的地方. 按照惯例先看看效果图: 一.先总结下自定义View的步骤: 1.自定义View的属性 2.在View的构造方法中获得我们自定义的属性 3.重写onMesure 4.重写onDraw 其中onMesure方法不一定要重写,但大部分情况下还是需要重写的 二.View 的几个构造函数 1.public CustomV

asp 生成图片验证码

下面一款代码是一个用asp 生成图片验证码的程序哦,关于验证代码写程序的大多都会知道哦,下面来看看这款图片验证码的生成方式吧. <% Option Explicit Response.buffer=true NumCode Function NumCode()  Response.Expires = -1  Response.AddHeader "Pragma","no-cache"  Response.AddHeader "cache-ctrol&

phantomjs的使用+Java代码+依赖js(兼容Linux和windows版本)

1.  在使用phantomjs的时候需要下载phantomjs,网上有window版本和Linux版本.将phantomjs放在Linux上的指定位置之后(如下面的/home/tpl/phantomjs-1.9.7-linux-x86_64/处), 2.  按照类似如下命令进行测试,是否可以生成图片(执行前检查phantomjs的权限,要有执行权限): /home/tpl/phantomjs-1.9.7-linux-x86_64/bin/phantomjs /home/tpl/phantomj

echarts-ECharts可以不走JS直接在JAVA端生成图片吗?

问题描述 ECharts可以不走JS直接在JAVA端生成图片吗? 直接通过JAVA代码.不走前端JS,.直接生成一张图片可以吗. 解决方案 这个应该不可以,你试一下就知道了 解决方案二: 用jfreechart代替ECharts 解决方案三: 这个不行就换个试试,不用非得用它,实现了同样的效果就可以了 解决方案四: 不行.echarts是个js框架,怎么可能不走js呢

图片处理-麻烦各位帮忙看看这个java代码,急急急急啊,有一部分不会

问题描述 麻烦各位帮忙看看这个java代码,急急急急啊,有一部分不会 public class CheckCoderTool extends HttpServlet {public void doGet(HttpServletRequest request HttpServletResponse response)throws ServletException IOException { response.setContentType(""image/jpeg"")