用JAVA中BufferedImage画出漂亮的验证码点击变化
如果我们想用JAVA中BufferedImage画出漂亮的验证码点击变化怎么实现呢,类似这样:
点击变化,以下是实现过程,直接上代码:
首先前台:<i><img style="height:22px;" id="zcodeImg" alt="点击更换" title="点击更换" src="" /></i>放在你页面想展示的位置;接着页面加载时
1
2
3
4
5
|
$(document).ready( function () { changeCode1(); $( "#codeImg" ).bind( "click" , changeCode1); }); function changeCode1() { |
js部分写完了,现在是控制层,直接上代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
@Controller @RequestMapping( "/code" ) public class SecCodeController { @RequestMapping public void generate(HttpServletResponse response){ ByteArrayOutputStream output = new ByteArrayOutputStream(); String code = drawImg(output); Subject currentUser = SecurityUtils.getSubject(); Session session = currentUser.getSession(); session.setAttribute(Const.SESSION_SECURITY_CODE, code); try { ServletOutputStream out = response.getOutputStream(); output.writeTo(out); out.close(); } catch (IOException e) { //e.printStackTrace(); } } private String drawImg(ByteArrayOutputStream output){ String code = "" ; for (int i=0; i<4; i++){ code += randomChar(); } int width = 70; int height = 25; BufferedImage bi = new BufferedImage(width,height,BufferedImage.TYPE_3BYTE_BGR); Font font = new Font( "Times New Roman" ,Font.PLAIN,20); Graphics2D g = bi.createGraphics(); g.setFont(font); Color color = new Color(66,2,82); g.setColor(color); g.setBackground( new Color(226,226,240)); g.clearRect(0, 0, width, height); FontRenderContext context = g.getFontRenderContext(); Rectangle2D bounds = font.getStringBounds(code, context); double x = (width - bounds.getWidth()) / 2; double y = (height - bounds.getHeight()) / 2; double ascent = bounds.getY(); double baseY = y - ascent; g.drawString(code, (int)x, (int)baseY); g.dispose(); try { ImageIO.write(bi, "jpg" , output); } catch (IOException e) { //e.printStackTrace(); } return code; } private char randomChar(){ Random r = new Random(); String s = "ABCDEFGHJKLMNPRSTUVWXYZ0123456789" ; return s.charAt(r.nextInt(s.length())); } } |
用JAVA中BufferedImage画出漂亮的验证码点击变化的更多相关文章
- 使用Pangolon在同一副图中,画出两个轨迹,比较误差
使用 code/ground-truth.txt 和 code/estimate.txt 两条轨迹.请你根据上面公式,实现 RMSE的计算代码,给出最后的 RMSE 结果.作为验算,参考答案为:2.2 ...
- java中BufferedImage类的用法
1. BufferedImage是Image的一个子类,Image和BufferedImage的主要作用就是将一副图片加载到内存中. BufferedImage生成的图片在内存里有一个图像缓冲区,利用 ...
- 【转载】使用python库--Graphviz为论文画出漂亮的示意图
原文: Drawing Graphs using Dot and Graphviz 1 License Copyright (C) 2013, 2014, 2015, 2016, 2017, 2018 ...
- JAVA中BufferedImage与byte[]转换
BufferedImage转byte[] ByteArrayOutputStream out = new ByteArrayOutputStream(); ImageIO.write(imgBuff, ...
- java中如何求出2008年的第1星期星期一是几号?
题目8: 2008年的第1星期星期一是几号? import java.util.*; public class Test { public static void main(String[] ...
- java中请给出一个return this的例子。
[新手可忽略不影响继续学习]下面例子中setYear中的return this;返回了一个指向对象的指针,this.setMonth(8).setDay(20);是合法的,如果像原来的例子一样什么都不 ...
- java中请给出例子程序:找出两个数的最大公约数和最小公倍数
9.2 找出12和8的最大公约数和最小公倍数. public class Test { public static void main(String[] args) { ...
- java中请给出例子程序:找出n到m之间的质数。
9.1 找出100到200之间的质数. public class Test { public static void main(String[] args){ for (in ...
- java中请给出一个抽象类,可以继承实体类的例子
例1.7.2(抽象类可以继承实体类)- class VehMark_to_win { void steer() { System.out.println("Turn st ...
随机推荐
- java 静态代码块 代码块 构造函数 静态成员变量 成员变量的初始化顺序
没有父类的情况 1,静态代码块-->静态成员变量-->成员变量(实例属性)-->代码块-->构造函数 2, 静态代码块 和 静态成员变量 只会初始化一次 有父类的情况 1,父类 ...
- MySQL中变量的总结
本文对MySQL中局部变量.用户变量.系统变量的理解进行总结. 一.局部变量 局部变量由DECLARE语句声明: DECLARE var_name[,...] type [DEFAULT value] ...
- win10 UWP GET Post
win10 应用应该是要有访问网络,网络现在最多的是使用GET,Post,简单的使用,可以用网络的数据:获得博客的访问量. 在使用网络,我们需要设置Package.appxmanifest 网络请求使 ...
- BootStrap教程完整版
http://www.runoob.com/bootstrap/bootstrap-navbar.html
- Spring源码情操陶冶-AOP之Advice通知类解析与使用
阅读本文请先稍微浏览下上篇文章Spring源码情操陶冶-AOP之ConfigBeanDefinitionParser解析器,本文则对aop模式的通知类作简单的分析 入口 根据前文讲解,我们知道通知类的 ...
- tomcat启动报错Several ports (8080, 8009) required by Tomcat v6.0
tomcat启动报错 如下图: 问题:8080.8009端口已经被占用. 解决办法: 1.在命令提示符下,输入netstat -aon | findstr 8080 2.继续输入taskkill -F ...
- web.xml中<init-param>报错
报错信息如下图: 控制台报错:严重: Servlet /*** threw load() exceptionjava.lang.ClassNotFoundException: MyApplicatio ...
- ASP.NET Core 开源GitServer 实现自己的GitHub
ASP.NET Core 2.0 开源Git HTTP Server,实现类似 GitHub.GitLab. GitHub:https://github.com/linezero/GitServer ...
- co源码分析及其实践
本文始发于我的个人博客,如需转载请注明出处. 为了更好的阅读体验,可以直接进去我的个人博客看. 前言 知识储备 阅读本文需要对Generator和Promise有一个基本的了解. 这里我简单地介绍一下 ...
- mybatis 参数为list时,校验list是否为空
校验objStatusList 是否为空 <if test="objStatusList != null and objStatusList.size() > 0 "& ...