一篇Java图片验证码生成的代码
package projectUtil; /**
* @author tian
* @date 2019/4/1015:58
*/ import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.GeneralPath;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random; /**
* 验证码生成器
*
*/
public class PicUtil {
// 图片的宽度。
private int width = 120;
// 图片的高度。
private int height = 40;
// 验证码字符个数
private int codeCount = 6;
// 验证码干扰线数
private int lineCount = 3;
// 验证码
private String code = null;
// 验证码图片Buffer
private BufferedImage buffImg = null; private char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'M', 'N', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '2', '3', '4', '5', '6', '7', '8', '9' };
// 生成随机数
private Random random = new Random(); public PicUtil() {
this.createCode();
} /**
*
* @param width 图片宽
* @param height 图片高
*/
public PicUtil(int width, int height) {
this.width = width;
this.height = height;
this.createCode();
} /**
*
* @param width 图片宽
* @param height 图片高
* @param codeCount 字符个数
* @param lineCount 干扰线条数
*/
public PicUtil(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 codeX = 0;
int fontHeight = 0;
fontHeight = height - 5;// 字体的高度
codeX = width / (codeCount + 3);// 每个字符的宽度 // 图像buffer
buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffImg.createGraphics(); // 将图像填充为白色
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height); // 创建字体
// ImgFontByte imgFont = new ImgFontByte();
Font font=new Font("宋体",Font.PLAIN,16);
g.setFont(font); StringBuffer randomCode = new StringBuffer();
// 随机产生验证码字符
for (int i = 0; i < codeCount; i++) {
String strRand = String.valueOf(codeSequence[random.nextInt(codeSequence.length)]);
// 设置字体颜色
g.setColor(getRandomColor());
// 设置字体位置
g.drawString(strRand, (i + 1) * codeX, getRandomNumber(height / 2) + 20);
randomCode.append(strRand);
}
// 利用GeneralPath类来画曲线
GeneralPath gp = new GeneralPath();
gp.moveTo(0,0); for (int i = 0; i < lineCount; i++) {
// 绘制一个圆弧(弧线)
// void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)
// 填充一个圆弧(扇形)
// void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)
// drawTanx(gp,g);
// drawCosx(gp,g);
drawSinx(gp,g);
drawSinxDX(gp,g);
// 设置字体颜色
g.setColor(getRandomColor());
// 绘制一个圆弧(弧线)
// g.drawArc(lineCount*getRandomNumber(10), lineCount*getRandomNumber(10), width, height,getRandomNumber(100),lineCount*getRandomNumber(100)%2==1?-lineCount*getRandomNumber(360):lineCount*getRandomNumber(1872));
}
code = randomCode.toString();
} /** 获取随机颜色 */
private Color getRandomColor() {
int r = getRandomNumber(255);
int g = getRandomNumber(255);
int b = getRandomNumber(255);
return new Color(r, g, b);
} /** 获取随机数 */
private int getRandomNumber(int number) {
return random.nextInt(number);
} 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.flush();
sos.close();
} public BufferedImage getBuffImg() {
return buffImg;
} public String getCode() {
return code;
} public static void main(String[] args) throws IOException {
for (int i = 0; i <5 ; i++) {
new PicUtil(){{
this.write("D:/yy/"+super.code+".png");
}};
} } private void drawTanx(GeneralPath gp, Graphics2D g2d) {
for (double i = 0.000001; i <= 8*Math.PI; i+=0.0001*Math.PI) {
gp.lineTo(20*i, 100*-Math.tan(i));
}
g2d.draw(gp); // 将当前画笔以原点为中心,旋转180度,画奇函数(关于原点对称)
g2d.rotate(Math.PI);
g2d.draw(gp);
} private void drawCosx(GeneralPath gp, Graphics2D g2d) {
for (double i = 0.000001; i <= 8*Math.PI; i+=0.0001*Math.PI) {
gp.lineTo(20*i, 100*-Math.cos(i));
}
g2d.draw(gp); // 将当前画笔以Y中为对称轴,画偶函数(关于Y轴对称)
g2d.scale(-1, 1);
g2d.draw(gp);
}
private void drawSinx(GeneralPath gp, Graphics2D g2d) {
for (double i = 0.000001; i <= 8*Math.PI; i+=0.0001*Math.PI) {
gp.lineTo(15*i, 100*-Math.sin(i));
}
g2d.draw(gp);
g2d.rotate(Math.PI*0.01*getRandomNumber(100));
g2d.draw(gp);
} private void drawSinxDX(GeneralPath gp, Graphics2D g) {
for (double i = 0.1; i <= 8*Math.PI; i+=0.0001*Math.PI) {
gp.lineTo(20*i, -100*-Math.sin(i)/i);
}
g.draw(gp);
g.scale(-1, 1);
g.draw(gp);
} } 网上有类似的代码,但独独只有生成的文字没有干扰线。所以花了点时间完善了一下,发上来,如果有需要请拿走。
一篇Java图片验证码生成的代码的更多相关文章
- Java图片验证码生成工具
直接把以下代码拷贝使用: import javax.imageio.ImageIO;import java.awt.*;import java.awt.image.BufferedImage;impo ...
- 开发工具类API调用的代码示例合集:六位图片验证码生成、四位图片验证码生成、简单验证码识别等
以下示例代码适用于 www.apishop.net 网站下的API,使用本文提及的接口调用代码示例前,您需要先申请相应的API服务. 六位图片验证码生成:包括纯数字.小写字母.大写字母.大小写混合.数 ...
- php 图片验证码生成 前后台验证
自己从前一段时间做了个php小项目,关于生成图片验证码生成和后台的验证,把自己用到的东西总结一下,希望大家在用到相关问题的时候可以有一定的参考性. 首先,php验证码生成. 代码如下: 1.生成图像代 ...
- Atitit 图片 验证码生成attilax总结
Atitit 图片 验证码生成attilax总结 1.1. 图片验证码总结1 1.2. 镂空文字 打散 干扰线 文字扭曲 粘连2 1.1. 图片验证码总结 因此,CAPTCHA在图片验证码这一应用点 ...
- [转]php 图片验证码生成 前后台验证
本文转自:https://www.cnblogs.com/xiaoyezi/p/3541195.html 自己从前一段时间做了个php小项目,关于生成图片验证码生成和后台的验证,把自己用到的东西总结一 ...
- java图片操作--生成与原图对称的图片
java图片操作--生成与原图对称的图片 package com.pay.common.util; import java.awt.image.BufferedImage; import java.i ...
- Flask实战第40天:图片验证码生成技术
图片验证码生成 安装pillow pip install pillow 在utils下新建python package命名为captcha 把需要需要用到的字体放在captcha下 编辑captcha ...
- Web后端 JAVA实现验证码生成与验证功能
首先,写一个验证码生成帮助类,用来绘制随机字母: <span style="font-size:14px;">import java.awt.Color; impor ...
- JavaWeb开发之普通图片验证码生成技术与算术表达式验证码生成技术
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6134649.html 另:算术验证码生成的JSP.Servlet实现均已移植github:https:/ ...
随机推荐
- [GO]gomaxprocs的使用
package main import ( "runtime" "fmt" ) func main() { n := runtime.GOMAXPROCS()/ ...
- javascript总结24:Array常用的队列操作和排序方法
1 数组-引用类型 JavaScript中的内置对象 复习数组的使用 两种创建数组的方式 Array对象的属性 length 获取数组的长度(元素个数) 2 常用方法 : 检测数组 instanceo ...
- iOS应用开发之Persistence持久化[转]
持久化(Persistence) 持久化(Persistence)意思就是当你退出app的时候它还会存在.NSUserDefaults就是一个非常简单的持久化方案,不过这有限制,它只能是很小的东西,通 ...
- 编写高质量代码改善C#程序的157个建议——建议149:使用表驱动法避免过长的if和switch分支
建议149:使用表驱动法避免过长的if和switch分支 随着代码变得复杂,我们很容易被过长的if和switch分支困扰. 一个类枚举类型Week如下: enum Week { Monday, Tue ...
- Linux CentOS 7 安装confluence 5.8.10
一..需要下载的安装包如下 1.atlassian-confluence-5.8.10-x64.bin #confluence安装包 2.confluence5.x-crack.zip #conflu ...
- airpods2代连接macbook失败
更新至最新系统(10.14.4),更新完毕,重启电脑再次连接即可. 参考连接: http://dq.tieba.com/p/6082366443
- opencv—读取一张图片并滤波
#include <opencv2\opencv.hpp> #include <iostream> #include <string> using namespac ...
- 加载log文件
procedure Jslog(szLogText:string);var pMyFile : TextFile;begin Assignfile(pMyFile, 'log.txt'); if Fi ...
- java学习(一)数据类型
一.java的安装及环境变量的配置 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.htm ...
- Android-显示意图和隐式意图
显示意图,一般情况下是用于,APP应用自身:组件(Activity,Service,...) 与 组件(Activity,Service,...) 的激活调用: 显示意图,是可以看得到,明确激活哪一个 ...