首页 / JAVA / java生成图片验证码
java生成图片验证码
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java生成图片验证码,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3950字,纯文字阅读大概需要6分钟。
内容图文
![java生成图片验证码](/upload/InfoBanner/zyjiaocheng/829/b2de410f80b14ccbbe04e9c596948755.jpg)
package com.yt.dfn.util;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.util.Random;
import org.apache.commons.lang3.StringUtils;
/**
* 图片验证码类
*
* @author tyg
* @date 2019年3月14日下午3:02:50
*/
public class Captcha {
private int width = 100;
private int height = 30;
private Random random = new Random();
private BufferedImage image;
/** 验证码图片上显示的字符 */
private String code;
/** 波形的幅度倍数,越大扭曲的程序越高,一般为3 */
private int twistLevel = 2;
/** 干扰线数量 */
private int noiseLineNumber = 5;
/** 背景色 */
private Color backgroundColor = Color.WHITE;
/** 字体颜色 */
// private Color foregroundColor = Color.BLACK;
private Color[] colors = { Color.BLUE, Color.RED, Color.GREEN, Color.BLACK, Color.CYAN, Color.MAGENTA };
private Color getRandomColor() {
return colors[random.nextInt(colors.length)];
}
public Captcha() {
}
/**
* 生成图形验证码(响应图片流)
*
* @param width 验证码图片宽度
* @param height 验证码图片高度
* @param randomStr 随机字符串
* @return
* @return Captcha
* @author tyg
* @date 2019年3月14日下午3:03:40
*/
public Captcha generate(String randomStr) {
this.code = randomStr;
if (StringUtils.isBlank(code)) {
throw new RuntimeException("randomStr can not be empty.");
}
int xWidth = width / (code.length() + 2);
int yIndex = height - 4;
Graphics2D graphics = graphicsInit();
for (int i = 0; i < code.length(); i++) {
graphics.setColor(getRandomColor());
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics.drawString(code.charAt(i) + "", (i + 1) * xWidth, yIndex);
}
setBuffImg(disturb());
return this;
}
private Graphics2D graphicsInit() {
Graphics2D graphics = buffImgInit().createGraphics();
graphics.setColor(backgroundColor);
graphics.fillRect(0, 0, width, height);
graphics.setFont(new Font("Fixedsys", Font.ITALIC, height - 2));
graphics.drawRect(0, 0, width - 1, height - 1);
return graphics;
}
private BufferedImage buffImgInit() {
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
return image;
}
private BufferedImage disturb() {
drawNoiseLine(image.createGraphics());
return twistImage();
}
private void drawNoiseLine(Graphics2D graphics) {
int x = 0;
int y = 0;
int xl = 0;
int yl = 0;
for (int i = 0; i < noiseLineNumber; i++) {
x = random.nextInt(width * 2 / 3);
y = random.nextInt(height * 2 / 3);
xl = random.nextInt(width / 2);
yl = random.nextInt(height / 2);
// graphics.setColor(foregroundColor);
graphics.setColor(getRandomColor());
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics.drawLine(x, y, x + xl, y + yl);
}
}
private BufferedImage twistImage() {
double dMultValue = random.nextInt(9) + twistLevel;
double dPhase = random.nextInt(6);// 波形的起始相位,取值区间(0-2*PI)
BufferedImage destBi = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = destBi.createGraphics();
graphics.setColor(backgroundColor);
graphics.fillRect(0, 0, width, height);
for (int i = 0; i < destBi.getWidth(); i++) {
for (int j = 0; j < destBi.getHeight(); j++) {
int nOldX = getXPosition4Twist(dPhase, dMultValue, destBi.getHeight(), i, j);
int nOldY = j;
if (nOldX >= 0 && nOldX < destBi.getWidth() && nOldY >= 0 && nOldY < destBi.getHeight()) {
destBi.setRGB(nOldX, nOldY, image.getRGB(i, j));
}
}
}
return destBi;
}
private int getXPosition4Twist(double dPhase, double dMultValue, int height, int xPosition, int yPosition) {
double PI = Math.PI; // 此值越大,扭曲程度越大
double dx = (double) (PI * yPosition) / height + dPhase;
double dy = Math.sin(dx);
return xPosition + (int) (dy * dMultValue);
}
public BufferedImage getImage() {
return image;
}
public void setBuffImg(BufferedImage buffImg) {
this.image = buffImg;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
}
内容总结
以上是互联网集市为您收集整理的java生成图片验证码全部内容,希望文章能够帮你解决java生成图片验证码所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。