java实现随机验证码的图片
链接地址:http://blog.sina.com.cn/s/blog_407a68fc010006qo.html
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* 随机取得一个字体
* @param Random random 随机数
* @return Font 返回一个新字体
*/
private Font getsFont(Random random){
return new Font("Fixedsys",Font.CENTER_BASELINE,18);
}
/**
* 返回一个随机颜色
* @param int fc 随机数
* @param int bc 随机数
* @param Random random 随机数
* @return Color 返回一个新颜色
*/
private Color getRandColor(int fc,int bc,Random random){
if(fc>255) fc=255;
if(bc>255) bc=255;
int r=fc+random.nextInt(bc-fc-6);
int g=fc+random.nextInt(bc-fc-4);
int b=fc+random.nextInt(bc-fc-8);
return new Color(r,g,b);
}
/**
* 生成随机数图片
*/
public void getRandcode(HttpServletRequest request,HttpServletResponse response)throws Exception{
System.setProperty("java.awt.headless","true");
HttpSession session = request.getSession();
int width=80, height=22;//设置图片大小
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Random random = new Random();
g.fillRect(1, 1, width, height);//设定边框
g.setFont(new Font("Times New Roman",Font.ROMAN_BASELINE,18));
g.setColor(getRandColor(111,133,random));
//产生随机线
for (int i=0;i<11;i++){
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(13);
int yl = random.nextInt(15);
g.drawLine(x,y,x+xl,y+yl);
}
//产生随机点
g.setColor(getRandColor(130,150,random));
//产生5个随机数
String sRand="";
for (int i=0;i<5;i++){
g.setFont(getsFont(random));
g.setColor(new Color(random.nextInt(101),random.nextInt(111),random.nextInt(121)));
String rand=String.valueOf(getRandomString(random.nextInt(36)));
sRand+=rand;
g.translate(random.nextInt(3),random.nextInt(3));
g.drawString(rand,13*i,16);
}
session.removeAttribute("Rand");
session.setAttribute("Rand",sRand);
g.dispose();
ImageIO.write(image, "JPEG", response.getOutputStream());
}
private String getRandomString(int num){
String randstring = "0123456788ABCDEFGHIJKLMNOPQRSTUVWXYZ";
return String.valueOf(randstring.charAt(num));
}
}
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("image/jpeg");
resp.setHeader("Pragma","No-cache");
resp.setHeader("Cache-Control","no-cache");
resp.setDateHeader("Expires", 0);
RandomCode rc = new RandomCode();
try{
rc.getRandcode(req,resp);
}catch(Exception e){
System.err.println(e);
}
}
throws ServletException, IOException {
doGet(request, response);
}
转载于:https://www.cnblogs.com/wvqusrtg/p/5105362.html
java实现随机验证码的图片相关推荐
- java 验证码_如何使用Java生成随机验证码
我们想想,如何用Java绘制图片呢? 第一步 寻找☞ 纸画画要纸的 第二步 寻找笔 画画当然也要笔 在开始之前,我们需要用到几个主要的类 BufferedImage 用来缓寸你画的东西 Random ...
- Java代码生成随机验证码、验证数
/*** 业务编码生成规则*/ public class CodeGeneratorUtil {static final IdGen IDG = IdGen.get();/*** 生成业务编码* @p ...
- java五位随机验证码的实现。要求前四位是随机大小写的字母,最后一位是数字的组合。例如qWrY4
package Test; import java.util.Random; public class Test1 { public static void main(String[] arg ...
- 使用原生 Java 玩转验证码【含 DATA-URIS 介绍】
开篇前的废话: 验证码是日常简单开发中经常会用到的东西,作为防止暴力破解密码的一道屏障.所以开发者必须掌握如何使用.废话不多说,直接上代码. 工具类:VerifyCodeUtil import sun ...
- java登录的 验证码_java登录验证码
一.创建web项目 二.生成验证码 创建VcodeObject.java,存储验证码与图片 package com.demo.vcode; import java.io.InputStream; pu ...
- 使用Java生成图形验证码(后端)
生成图形验证码工具类: VerificationCodeUtil package com.merce.woven.utils;import java.awt.*; import java.awt.im ...
- 用ajax写验证码随机数,JavaScript随机验证码
利用canvas制作一个随机验证码: 1.clearRect:context.clearRect(x,y,width,height);清空给定矩形内的指定像素 2.fillStyle:设置画笔的颜色 ...
- python圆形噪点_python模块PIL-获取带噪点噪线的随机验证码
from PIL import Image from PIL import ImageDraw from PIL import ImageFont from io import BytesIO imp ...
- Java生成随机图片验证码工具类
一.第一种生成随机图片验证码 public class VerifyCodeUtil {//生成随机数据的数据源 public static final String VERIFY_CODES = & ...
最新文章
- iOS 的本地化使用和创建过程
- Jquery zTree实例
- AR2220 通过cpu-defend policy处理大量大量arp广播的小技巧
- 网易有毛病,我的账号被锁了,去解锁时候又几把提示不成功,浪费劳资感情啊
- 逆向最大匹配分词算法
- 重整谋定电商经信研究新格局-李玉庭:人工智能精细化运营
- Android7.0 PowerManagerService(2) WakeLock的使用及流程
- 提取pfx证书公钥和私钥
- winform php 交互,WinForm开发,窗体显示和窗体传值
- spring注解-声明式事务
- 【英语学习】【WOTD】inexorable 释义/词源/示例
- 频谱感知1:未知确定信号的能量检测
- “搏一搏,单车变摩托
- java怎么用unicode写程序_简单的讲解Java是使用Unicode字符集
- java 用redis如何处理电商平台,秒杀、抢购超卖
- 计算机图形图像设计构图的基本形式,设计构图的八大基本表现手法!你造吗?...
- 阿里云服务器配置DNS域名解析
- python英文日期转数字年月日格式
- js+html+css+jQuery实现简单购物车
- iphone连电脑服务器未响应,今天要闻iphone8无限转圈黑屏强制关机没反应(苹果电脑开不了机黑屏)...