博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java画出验证码图片
阅读量:7255 次
发布时间:2019-06-29

本文共 2184 字,大约阅读时间需要 7 分钟。

package com;import javax.imageio.ImageIO;import java.awt.*;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import java.util.Random;//验证码public class Imageyan {    public static void main(String[] args) throws IOException {        int id = 4;        int w = id*50;        int h = 80;        BufferedImage buf = new BufferedImage(w,h,1);        Graphics g = buf.getGraphics();        g.setColor(Color.WHITE);        g.fillRect(0,0,w,h);        Random ran = new Random();        //设置字        Font font = new Font("宋体",Font.PLAIN,40);        g.setFont(font);        for(int i = 0;i < id;i++) {            Color color = new Color(ran.nextInt(255),ran.nextInt(255),ran.nextInt(255),255);            g.setColor(color);            String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";            String str = "";            str = chars.charAt(ran.nextInt(chars.length()))+"";            int x = 20 + i * 45;            int y = 50;            g.drawString(str, x, y);        }        //添加干扰文字        for(int i = 0;i < id;i++) {            Color color = new Color(ran.nextInt(255),ran.nextInt(255),ran.nextInt(255),100);            g.setColor(color);            String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";            String str = "";            str = chars.charAt(ran.nextInt(chars.length()))+"";            g.drawString(str, ran.nextInt(w),ran.nextInt(h));        }        //添加线条        for (int a = 0; a < 30; a++) {            Color color = new Color(ran.nextInt(255),ran.nextInt(255),ran.nextInt(255),ran.nextInt(255));            g.setColor(color);            g.drawLine(ran.nextInt(w),ran.nextInt(w),ran.nextInt(w),ran.nextInt(w));        }        //添加弧线        for (int a = 0; a < 30; a++) {            Color color = new Color(ran.nextInt(255),ran.nextInt(255),ran.nextInt(255),ran.nextInt(255));            g.setColor(color);            g.drawArc(ran.nextInt(w),ran.nextInt(w),ran.nextInt(w),ran.nextInt(w),ran.nextInt(360),ran.nextInt(360));        }        g.dispose();        ImageIO.write(buf,"jpg",new File("d:/img/five.jpg"));    }}

 

转载于:https://www.cnblogs.com/zxwen/p/9526446.html

你可能感兴趣的文章
Mybatis学习(2)原始dao开发和使用mapper接口代理开发
查看>>
ggplot2 theme相关设置—矩形设置(rect)
查看>>
笔记--语音信号的预加重
查看>>
C++学习之路: 函数适配器
查看>>
六、与json交互
查看>>
python学习笔记二
查看>>
一个初学者C#编写帐号密码保存软件的思考过程
查看>>
磁盘IO性能监控(Linux 和 Windows)
查看>>
zw版【转发·台湾nvp系列Delphi例程】HALCON InpaintingCt1
查看>>
Bzoj2657 [Zjoi2012]旅游(journey)
查看>>
Java导包后在测试类中执行正确但在Servlet中执行错误报ClassNotFoundException或者ClassDefNotFoundException解决办法...
查看>>
C#判断日期是否合法
查看>>
ruby在线安装整理
查看>>
【软件解决】解决VS环境中出现虚线问题
查看>>
laravel 实现增 与查
查看>>
一种排序
查看>>
Linux实战教学笔记44:NoSQL数据库开篇之应用指南
查看>>
springmvc(2)处理器设配器和映射器
查看>>
PAT 1003
查看>>
switch gnome-terminal tabs
查看>>