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();
}
}
生成验证图片
最新推荐文章于 2024-05-11 23:51:20 发布
4817

被折叠的 条评论
为什么被折叠?



