结合网上的图形验证码思路和方案,弄了一个比较复杂的图形验证码工具,可以比较有效防OCR

结合⽹上的图形验证码思路和⽅案,弄了⼀个⽐较复杂的图形验证码⼯具,可以⽐较有效防OCR
验证码效果如下:有静态的,动态的,中空的
结合⽹上的图形验证码技术,不依赖第三⽅包纯java加⼯了⼀个⽐较复杂的图形验证码⽅案,防OCR防机器⼈
⽹上的图形验证码⽅案都是零星的,弄了⼀个随机字体、随机彩⾊字符、随机字体⼤⼩、随机扭曲、随机旋转等技术,能有效的防OCR、描边、深浅⾊等技术识别
本意是想区分登录、领券、抽奖等⼀些场景操作的,后来⼀想,随机拼⼈品吧,于是就有了下⾯的⼯具类
st;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import AffineTransform;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Random;
import javax.imageio.ImageIO;
import org.apache.log4j.Logger;
/
**
* 验证码⼯具类:
* 随机字体、字体样式、字体⼤⼩(验证码图⽚宽度 - 8 ~ 验证码图⽚宽度 + 10)
* 彩⾊字符每个字符的颜⾊随机,⼀定会不相同
* 随机字符阿拉伯数字 + ⼩写字母 + ⼤写字母
* 3D中空⾃定义字体,需要单独使⽤,只有阿拉伯数字和⼤写字母
*
* @author cgtu
* @date 2017年5⽉9⽇下午7:27:55
*/
public class RandomVerifyImgCodeUtil
{
private static Logger logger = Logger(RandomVerifyImgCodeUtil.class);
/**
* 随机类
*/
private static Random random = new Random();
// 放到session中的key
public static final String RANDOMCODEKEY = "RANDOMVALIDATECODEKEY";
// 验证码来源范围,去掉了0,1,I,O,l,o⼏个容易混淆的字符
public static final String VERIFY_CODES = "23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz";
private static ImgFontByte imgFontByte = new ImgFontByte();
private static Font baseFont;
static
霍尔式角度传感器
{
try
{
baseFont = ateFont(Font.TRUETYPE_FONT, new ByteArrayInputStream(imgFontByte.FontByteStr())));
}
catch (FontFormatException e)
{
<("new img font font format failed. e: " + e.getMessage(), e);
}
catch (IOException e)
{
<("new img font io failed. e: " + e.getMessage(), e);
}
}
// 字体类型
private static String[] fontName =
{
"Algerian", "Arial", "Arial Black", "Agency FB", "Calibri", "Cambria", "Gadugi", "Georgia", "Consolas", "Comic Sans MS", "Courier New",
"Gill sans", "Time News Roman", "Tahoma", "Quantzite", "Verdana"
};
// 字体样式
private static int[] fontStyle =
{
Font.BOLD, Font.ITALIC, Font.ROMAN_BASELINE, Font.PLAIN, Font.BOLD + Font.ITALIC
};
// 颜⾊
private static Color[] colorRange =
{
Color.WHITE, Color.CYAN, Color.GRAY, Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE, Color.PINK, Color.YELLOW, Color.GREEN, Color.BLUE,  Color.DARK_GRAY, Color.BLACK, Color.RED
};
/**
* 使⽤系统默认字符源⽣成验证码
*
* @param verifySize
*            验证码长度
* @param uuid
*            App端传的参数,app没有session和cookie,必须⽤设备号在redis记录图形验证码的值
* @param platFormName
*            平台名称:pc\wap\app app⽐较特殊,需要单独处理
* @param request
*            请求
* @return
*/光线路终端
public static String generateVerifyCode(int verifySize)
{
return generateVerifyCode(verifySize, VERIFY_CODES);
}
/**
* 使⽤指定源⽣成验证码
*
* @param verifySize
*            验证码长度
* @param sources
*            验证码字符源
* @return
* @return
*/
private static String generateVerifyCode(int verifySize, String sources)
{
if (sources == null || sources.length() == 0)
导电碳浆
{
sources = VERIFY_CODES;
}
int codesLen = sources.length();
Random rand = new Random(System.currentTimeMillis());
StringBuilder verifyCode = new StringBuilder(verifySize);
for (int i = 0; i < verifySize; i++)
{
verifyCode.append(sources.Int(codesLen - 1)));
}
String();
}
/**
* 输出指定验证码图⽚流
*
* @param w
*            验证码图⽚的宽
* @param h
*            验证码图⽚的⾼
* @param os
*            流
* @param code
*            验证码
* @param type
*            场景类型,login:登录,
*            coupons:领券登录清晰化,领券模糊化
*            3D: 3D中空⾃定义字体
*            GIF:普通动态GIF
*            GIF3D:3D动态GIF
*            mix2: 普通字体和3D字体混合
*            mixGIF: 混合动态GIF
* @throws IOException
*/
public static void outputImage(int w, int h, OutputStream os, String code, String type) throws IOException  {
int verifySize = code.length();
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Random rand = new Random();
Graphics2D g2 = ateGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);  Color[] colors = new Color[5];
Color[] colorSpaces = colorRange;
float[] fractions = new float[colors.length];
for (int i = 0; i < colors.length; i++)
sky angel vol.96{
colors[i] = Int(colorSpaces.length)];
fractions[i] = Float();
}
Arrays.sort(fractions);
g2.setColor(Color.GRAY);// 设置边框⾊
g2.fillRect(0, 0, w, h);
Color c = getRandColor(200, 250);
g2.setColor(c);// 设置背景⾊
g2.fillRect(0, 2, w, h - 4);
char[] charts = CharArray();
for (int i = 0; i < charts.length; i++)
for (int i = 0; i < charts.length; i++)
{
g2.setColor(c);// 设置背景⾊
g2.setFont(getRandomFont(h, type));醚基汽油
g2.fillRect(0, 2, w, h - 4);
}
// 1.绘制⼲扰线
Random random = new Random();
g2.setColor(getRandColor(160, 200));// 设置线条的颜⾊
int lineNumbers = 20;
if (type.equals("login") || ains("mix") || ains("3D"))  {
lineNumbers = 20;
}
else if (type.equals("coupons"))
{
lineNumbers = getRandomDrawLine();
}
else
{
lineNumbers = getRandomDrawLine();
}
for (int i = 0; i < lineNumbers; i++)
{
int x = Int(w - 1);
int y = Int(h - 1);
int xl = Int(6) + 1;
int yl = Int(12) + 1;
g2.drawLine(x, y, x + xl + 40, y + yl + 20);
}
// 2.添加噪点
float yawpRate = 0.05f;
if (type.equals("login") || ains("mix") || ains("3D"))  {
yawpRate = 0.05f; // 噪声率
}
else if (type.equals("coupons"))
{
yawpRate = getRandomDrawPoint(); // 噪声率
}
else
{
yawpRate = getRandomDrawPoint(); // 噪声率
}
int area = (int) (yawpRate * w * h);
for (int i = 0; i < area; i++)
{
int x = Int(w);
int y = Int(h);
int rgb = getRandomIntColor();
image.setRGB(x, y, rgb);
}
// 3.使图⽚扭曲
shear(g2, w, h, c);
char[] chars = CharArray();
Double rd = Double();
Boolean rb = Boolean();
if (type.equals("login"))
{
for (int i = 0; i < verifySize; i++)
{
{
g2.setColor(getRandColor(100, 160));
g2.setFont(getRandomFont(h, type));
AffineTransform affine = new AffineTransform();
affine.setToRotation(Math.PI / 4 * rd * (rb ? 1 : -1), (w / verifySize) * i + (h - 4) / 2, h / 2);
g2.setTransform(affine);
g2.Int(w), Int(h), 5 + Int(10), 5 + Int(10));    g2.drawChars(chars, i, 1, ((w - 10) / verifySize) * i + 5, h / 2 + (h - 4) / 2 - 10);
}
g2.dispose();
ImageIO.write(image, "jpg", os);
}
五方通话系统
else if (ains("GIF") || ains("mixGIF"))
{
GifEncoder gifEncoder = new GifEncoder(); // gif编码类,这个利⽤了洋⼈写的编码类
// ⽣成字符
gifEncoder.start(os);
gifEncoder.setQuality(180);
gifEncoder.setDelay(150);
gifEncoder.setRepeat(0);
AlphaComposite ac3;
for (int i = 0; i < verifySize; i++)
{
g2.setColor(getRandColor(100, 160));
g2.setFont(getRandomFont(h, type));
for (int j = 0; j < verifySize; j++)
{
AffineTransform affine = new AffineTransform();
affine.setToRotation(Math.PI / 4 * rd * (rb ? 1 : -1), (w / verifySize) * i + (h - 4) / 2, h / 2);
g2.setTransform(affine);
g2.drawChars(chars, i, 1, ((w - 10) / verifySize) * i + 5, h / 2 + (h - 4) / 2 - 10);
ac3 = Instance(AlphaComposite.SRC_OVER, getAlpha(j, i, verifySize));
g2.setComposite(ac3);
g2.Int(w), Int(h), 5 + Int(10), 5 + Int(10));    gifEncoder.addFrame(image);
image.flush();
}
}
gifEncoder.finish();
g2.dispose();
}
else
{
for (int i = 0; i < verifySize; i++)
{
g2.setColor(getRandColor(100, 160));
g2.setFont(getRandomFont(h, type));
AffineTransform affine = new AffineTransform();
affine.setToRotation(Math.PI / 4 * rd * (rb ? 1 : -1), (w / verifySize) * i + (h - 4) / 2, h / 2);
g2.setTransform(affine);
g2.Int(w), Int(h), 5 + Int(10), 5 + Int(10));    g2.drawChars(chars, i, 1, ((w - 10) / verifySize) * i + 5, h / 2 + (h - 4) / 2 - 10);
}
g2.dispose();
ImageIO.write(image, "jpg", os);
}
}
/**
* 获取随机颜⾊

本文发布于:2024-09-23 19:16:58,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/1/219732.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:验证码   字体   图形   字符   设置
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议