Java生成图片验证码
来源:互联网 发布:军绿色外套 淘宝 编辑:程序博客网 时间:2024/06/16 13:07
在日常我们在登录或者注册的时候,网页上会出现验证码让我们填写,其实利用jdk提供给我们的工具类完全可以模拟出来一个生成验证码图片的功能。
package util;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 image { public static int[] ran() { //设置图片宽度和高度 int width = 150; int height = 60; //干扰线条数 int lines = 10;// 验证码数组 int[] random = new int[4]; Random r = new Random(); BufferedImage b = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = b.getGraphics(); g.setFont(new Font("宋体", Font.BOLD, 30)); for (int i = 0; i < 4; i++) { int number = r.nextInt(10); random[i] = number; int y = 10+r.nextInt(40);// 10~40范围内的一个整数,作为y坐标 //随机颜色,RGB模式 Color c = new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)); g.setColor(c);// g.drawString("" + a, 5 + i * width / 4, y); //写验证码 g.drawString(Integer.toString(number), 5 + i * width / 4, y); } for (int i = 0; i < lines; i++) { //设置干扰线颜色 Color c = new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)); g.setColor(c); g.drawLine(r.nextInt(width), r.nextInt(height), r.nextInt(width), r.nextInt(height)); } try { //清空缓冲 g.dispose(); //向文件中写入 ImageIO.write(b, "jpg", new File("E:\\IntelliJ IDEA\\mail\\web\\imagecode\\test.jpg")); } catch (IOException e) { e.printStackTrace(); } return random; } //测试 public static void main(String[] args) { ran(); }}
运行之后生成的图片:
这样我们在方法中用一个数组存了四位的验证码,在后台判断用户输入的和数组中的元素是否一致,就可以实现验证了。
阅读全文
0 0
- Java生成图片验证码
- Java生成验证码图片
- Java生成图片验证码
- Java生成图片验证码
- JAVA 生成验证码图片
- Java生成图片验证码
- Java生成验证码图片
- java生成验证码图片
- java生成验证码图片
- java 生成图片验证码
- java生成图片验证码
- java验证码图片生成
- JAVA生成图片验证码
- JAVA生成图片验证码
- java--生成图片验证码
- java生成图片验证码
- Java生成图片验证码
- java生成图片验证码
- iOS经典讲解之适配iOS 11那些事-持续更新...
- DISPATCH_SOURCE_TYPE_TIMER那些容易坑的点
- java经典问题——生兔子
- 搭建ssm框架遇到的问题:The request sent by the client was syntactically incorrect
- Re-ID: Learning Deep Feature Representation with Domain Guided Dropout for Person Re-identifiation
- Java生成图片验证码
- JAVA | 11
- WIN7下安装Tensorflow
- React 组件通信
- 【HDU】 1213
- Java练习题14 教师研究生工资
- 音频常用的音频格式
- 用Condtruct2 制作游戏2
- HashMap、LinkedHashMap、TreeMap、HashTable、ConcurrentHashMap原理解析