Purpose:
Prevent malicious form registration
Generate verification code image
1. Define width and height
int width = 100; int height = 50;
2. Use BufferedImage to generate images in memory
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
3. Draw background and borders
Graphics g = image.getGraphics(); g.setColor(Color.WHITE); g.fillRect(0, 0, width, height); g.setColor(Color.BLACK); g.drawRect(0, 0, width - 1, height - 1);
(Free learning video tutorial sharing: java video tutorial)
4. Create a random character set and random number object
//字符集 String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefgjijklmnopqrstuvwxyz"; //随机数 Random ran = new Random();
5. Create a random color generation method
private Color getRandomColor(Random random) { //获取随机颜色 int colorIndex = random.nextInt(3); switch (colorIndex) { case 0: return Color.BLUE; case 1: return Color.GREEN; case 2: return Color.RED; case 3: return Color.YELLOW; default: return Color.MAGENTA; } }
6. Draw Verification code characters
//绘制验证码 for (int i = 0; i < 4; i++) { //获取随机字符 int index = ran.nextInt(str.length()); char ch = str.charAt(index); //获取随机色 Color randomColor = getRandomColor(ran); g.setColor(randomColor); //设置字体 Font font = new Font("宋体", Font.BOLD, height / 2); g.setFont(font); //写入验证码 g.drawString(ch + "", (i == 0) ? width / 4 * i + 2 : width / 4 * i, height - height / 4); }
7, draw interference lines
//干扰线 for (int i = 0; i < 10; i++) { int x1 = ran.nextInt(width); int x2 = ran.nextInt(width); int y1 = ran.nextInt(height); int y2 = ran.nextInt(height); Color randomColor = getRandomColor(ran); g.setColor(randomColor); g.drawLine(x1, x2, y1, y2); }
8, use ImageIO to output pictures
ImageIO.write(image, "jpg", resp.getOutputStream());
to achieve refresh effect
1. Create a new html page
2. Use the img tag to display images
<img src="/static/imghwm/default1.png" data-src="identcode" class="lazy" id="identcode" alt="Java implements dynamic picture verification code" > <a id="refesh" href="">看不清,换一张</a>
3. Use js to achieve the refresh effect
//点击图片时 var img = document.getElementById("identcode"); img.onclick = function (){ refesh(); } //点击连接时 var a = document.getElementById("refesh"); a.onclick = function (){ refesh(); //返回false防止a标签默认href行为 return false; } function refesh() { /** * 由于路径相同时浏览器会自动调用缓存中的图片 * 所以在连接后加时间戳解决此问题 */ var date = new Date().getTime(); img.src = "identcode?" + date; }
Finally Rendering:
Recommended related articles and tutorials: java introductory tutorial
The above is the detailed content of Java implements dynamic picture verification code. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于设计模式的相关问题,主要将装饰器模式的相关内容,指在不改变现有对象结构的情况下,动态地给该对象增加一些职责的模式,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

Dreamweaver CS6
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1
Powerful PHP integrated development environment
