java 使用 swing生成验证码_Java生成验证码DemoCode

该博客介绍了一个使用Java Swing生成验证码的示例代码。通过随机旋转角度、颜色、字体和干扰元素,创建了具有防伪功能的验证码图片。代码包括生成随机背景色、字体颜色、字体样式和干扰线、噪点等步骤。
摘要由CSDN通过智能技术生成

package com.yueyi.bos.utils;

import java.awt.BasicStroke;

import java.awt.Color;

import java.awt.Font;

import java.awt.FontMetrics;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Paint;

import java.awt.PaintContext;

import java.awt.Rectangle;

import java.awt.RenderingHints;

import java.awt.Shape;

import java.awt.Stroke;

import java.awt.font.FontRenderContext;

import java.awt.geom.AffineTransform;

import java.awt.geom.Rectangle2D;

import java.awt.image.BufferedImage;

import java.awt.image.ColorModel;

import java.util.Date;

import java.util.Random;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JLabel;

public class VerifiCodeUtil {

private static String[] fontNames = {"宋体","TimesRoman","Courier","Arial","楷体","黑体"};

public static BufferedImage getBufferdImage(String str,int width,int height) {

Random random = new Random(new Date().getTime());

//设置背景颜色

Color bgcolor = getRandomColor(random);

int startx = 0;

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

Graphics2D charg = (Graphics2D) charImage.getGraphics();

charg.setColor(bgcolor);;

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

for (int i = 0; i < str.length(); i++) {

/* 随机旋转角度 */

int degree = random.nextInt(360);

/* 随机字体颜色 */

Color frontcolor = getRandomColor(random);

/* 随机字体 */

String fontname = fontNames[random.nextInt(fontNames.length)];

/* 是否加粗 */

int isbold = random.nextInt(2);

/* 随机字体大小 */

int fontsize = 35 + random.nextInt(10);

Font font = new Font(fontname,isbold ,fontsize);

FontMetrics fm = sun.font.FontDesignMetrics.getMetrics(font);

int charWidth = fm.charWidth(str.toCharArray()[i]);

int charHeight = fm.getAscent();

if(charWidth>charHeight) {

startx += charWidth /2 ;

}else {

startx += charHeight /2 ;

}

int starty = (height - charHeight)/2;

int centerx = startx + charWidth/2;

int centery = starty + charHeight/2;

charg.translate(centerx, centery);

charg.rotate(degree*Math.PI/180);

charg.translate(-centerx, -centery);

charg.setColor(frontcolor);

charg.setFont(font);

charg.drawString(""+str.toCharArray()[i], startx, starty+charHeight);

charg.translate(centerx, centery);

charg.rotate(-degree*Math.PI/180);

charg.translate(-centerx, -centery);

startx += charWidth;

}

/*干扰线*/

for (int j = 0; j <5 + random.nextInt(10); j++) {

int linex1 = random.nextInt(width);

int liney1 = random.nextInt(height);

int linex2 = random.nextInt(width);

int liney2 = random.nextInt(height);

int linewidth = random.nextInt(4) ;

int arcx = random.nextInt(width);

int arcy = random.nextInt(height);

int arcw = random.nextInt(width);

int arch = random.nextInt(height);

int degree1 = random.nextInt(360);

int degree2= random.nextInt(360);

charg.setColor(getRandomColor(random));

charg.setStroke( new BasicStroke( linewidth ) );

charg.drawLine(linex1, liney1, linex2, liney2);

charg.drawArc(arcx, arcy, arcw, arch, degree1, degree2);

}

/* 噪点 */

for (int j = 0; j < 50+random.nextInt(10); j++) {

int dotx = random.nextInt(width-1);

int doty = random.nextInt(height-1);

Color randomColor = getRandomColor(random);

int dotsize = random.nextInt(4) ;

charg.setColor(randomColor);

charg.setStroke(new BasicStroke(dotsize));

charg.drawLine(dotx, doty, dotx, doty);

}

charg.dispose();

return charImage;

}

private static Color getRandomColor(Random random) {

char r = (char) random.nextInt(256);

char g = (char) random.nextInt(256);

char b = (char) random.nextInt(256);

return new Color(r,g,b);

}

public static void main(String[] args) {

JFrame jFrame=new JFrame();

jFrame.setBounds(400, 400, 250, 250);

ImageIcon img = new ImageIcon(VerifiCodeUtil.getBufferdImage("ASdf23",300,100));

JLabel background = new JLabel(img);

jFrame.add(background);

jFrame.setVisible(true);

jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

效果:

5eb0c07864df881a90421ba4b3361ee4.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值