java 验证码图片生成_java 生成验证码图片

package Demo;

import javax.imageio.ImageIO;

import java.awt.*;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.Random;

public class yzm {

public static void main(String[] args) {

Random rand = new Random();

String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789";

StringBuffer a = new StringBuffer(4);

for (int i = 0; i < 4; i++) {

char c = str.charAt(rand.nextInt(str.length()));

a.append(c);

}

int w = 160;

int h = 60;

BufferedImage s = new BufferedImage(w, h, 1);

Graphics2D g = s.createGraphics();

g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

g.setColor(Color.WHITE);

g.fillRect(0, 0, w, h);

try {

Font f = Font.createFont(Font.TRUETYPE_FONT, new File("C:\\Users\\86176\\Desktop\\ADKS____.TTF"));

for (int n = 0; n < a.length(); n++) {

Font ff = f.deriveFont(Font.BOLD, rand.nextInt(20) + 35);

g.setFont(ff);

int x = n * 30 + rand.nextInt(20) + 10;

int y = rand.nextInt(10) + 30;

String text = String.valueOf(a.toString().charAt(n));

g.setColor(new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256), rand.nextInt(120) + 100));

g.drawString(text, x, y);

int x1 = 0;//x轴起点

double y1 = 30;//y轴起点

int yy = 30;//y轴的位置

int hh = 30;//上下幅度

int aa = 100;//波动频率

for (x1 = 10;x1 <= 360;x1++){

y1 = (yy + hh * Math.sin(x1*Math.PI / aa));

g.drawLine(x1,(int)y1,x1,(int)y1);

}

System.out.println(text);

}

} catch (FontFormatException | IOException e) {

e.printStackTrace();

}

g.dispose();

try {

ImageIO.write(s, "jpg", new FileOutputStream("C:\\Users\\86176\\Desktop\\ABC.jpg"));

} catch (IOException e) {

e.printStackTrace();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值