生成验证图片


private void createImage(OutputStream out) {
int width = 100;
int height = 60;
BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bimage.createGraphics();
//设置背景色
g.setBackground(Color.WHITE);
g.clearRect(0, 0, width, height);

g.setColor(Color.GRAY);
g.drawRect(0, 0, width - 1, height - 1);

//绘制背景线条
Random random = new Random();
g.setColor(new Color(random.nextInt(40) + 160, random.nextInt(40) + 160, random.nextInt(40) + 160));
for (int i = 0; i < 120; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x, y, x + xl, y + yl);
}

//绘制随机数值
String str = "";
int[] ns = {10, 26, 26, 48, 65, 97};
for (int i = 0; i < 6; i++) {
int n = (int) (random.nextInt(3));
String s = String.valueOf((char) (random.nextInt(ns[n]) + ns[n + 3]));
str += s;
g.setFont(new Font("Arial", random.nextInt(3), 15 + random.nextInt(10)));
g.setColor(new Color((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255)));
g.drawString(s, (5 + i * 15), random.nextInt(10) + 30);
}
System.out.println(str);

g.dispose();
bimage.flush();

//编码生成jpeg图片
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
param.setQuality(1.0f, false);
encoder.setJPEGEncodeParam(param);
try {
encoder.encode(bimage);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值