版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、1.图片验证码的实现主要的技术点是如何生成一个图片。生成图片可以使用java.awt包下的类来实现。我们先写一个简单的生成图片的程序HelloImage.java。以下是代码部分。(是我在做留言板项目时的部分代码)HelloImage .javaview plaincopy to clipboardprint?package liuyanban;import java.awt.Color;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOExcept
2、ion;import javax.imageio.ImageIO;/* 生成图片*/public class HelloImage public static void main(String args)BufferedImage image = new BufferedImage(80, 50,BufferedImage.TYPE_INT_RGB);/建立BufferedImage对象。指定图片的长度宽度和色彩。Graphics g = image.getGraphics();/取得Graphics对象,用来绘制图片。g.setColor(new Color(255,255,255);/绘制
3、图片背景和文字。g.fillRect(0, 0, 80, 25);g.setColor(new Color(0,0,0);g.drawString(HelloImage,6,16);g.dispose();/释放Graphics对象所占用的资源。tryImageIO.write(image, jpeg, new File(C:helloImage.jpeg);/通过ImageIO对象的write静态方法将图片输出。catch(IOException e)e.printStackTrace();package liuyanban;import java.awt.Color;import java
4、.awt.Graphics;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import javax.imageio.ImageIO;/* 生成图片*/public class HelloImage public static void main(String args)BufferedImage image = new BufferedImage(80, 50,BufferedImage.TYPE_INT_RGB);/建立BufferedImage对象。指定图片的长度宽度和色
5、彩。Graphics g = image.getGraphics();/取得Graphics对象,用来绘制图片。g.setColor(new Color(255,255,255);/绘制图片背景和文字。g.fillRect(0, 0, 80, 25);g.setColor(new Color(0,0,0);g.drawString(HelloImage,6,16);g.dispose();/释放Graphics对象所占用的资源。tryImageIO.write(image, jpeg, new File(C:helloImage.jpeg);/通过ImageIO对象的write静态方法将图片输
6、出。catch(IOException e)e.printStackTrace();view plaincopy to clipboardprint?*viewclipboardprint?2.生成验证码图片的类2.生成验证码图片的类view plaincopy to clipboardprint?package liuyanban;plaincopy toimport java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.I
7、OException;import java.util.Random;import javax.imageio.ImageIO;import javax.servlet.http.HttpServletResponse;/* 生成验证码图片的类*/public class RandImgCreater private static final String CODE_LIST ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890;private HttpServletResponse response = null;pri
8、vate static final int HEIGHT = 20;private static final int FONT_NUM = 4;private int width = 0;private int iNum = 0;private String codeList = ;private boolean drawBgFlag = false;private int rBg = 0; =private int gBg = 0;private int bBg = 0;public RandImgCreater(HttpServletResponse response) this.resp
9、onse = response;this.width = 13 * FONT_NUM + 12;this.iNum = FONT_NUM;this.codeList = CODE_LIST;public RandImgCreater(HttpServletResponse response,int iNum,String codeList) this.response = response;this.width = 13 * iNum + 12;this.iNum = iNum;this.codeList = codeList;public String createRandImage()Bu
10、fferedImage image = new BufferedImage(width, HEIGHT,BufferedImage.TYPE_INT_RGB);Graphics g = image.getGraphics();Random random = new Random();if ( drawBgFlag )g.setColor(new Color(rBg,gBg,bBg);g.fillRect(0, 0, width, HEIGHT);elseg.setColor(getRandColor(200, 250);g.fillRect(0, 0, width, HEIGHT);for (
11、int i = 0; i 155; i+) g.setColor(getRandColor(140, 200);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);g.setFont(new Font(Times New Roman, Font.PLAIN, 18);String sRand=;for (int i=0;i 255)fc = 255;
12、if (bc 255)bc = 255;int r = fc + random.nextInt(bc - fc);int g = fc + random.nextInt(bc - fc);int b = fc + random.nextInt(bc - fc);return new Color(r, g, b);package liuyanban;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.IOExce
13、ption;import java.util.Random;import javax.imageio.ImageIO;import javax.servlet.http.HttpServletResponse;/* 生成验证码图片的类*/public class RandImgCreater private static final String CODE_LIST ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890;private HttpServletResponse response = null;private
14、static final int HEIGHT = 20;private static final int FONT_NUM = 4;private int width = 0;private int iNum = 0; =private String codeList = ;private boolean drawBgFlag = false;private int rBg = 0;private int gBg = 0;private int bBg = 0;public RandImgCreater(HttpServletResponse response) this.response
15、= response;this.width = 13 * FONT_NUM + 12;this.iNum = FONT_NUM;this.codeList = CODE_LIST;public RandImgCreater(HttpServletResponse response,int iNum,String codeList) this.response = response;this.width = 13 * iNum + 12;this.iNum = iNum;this.codeList = codeList;public String createRandImage()Buffere
16、dImage image = new BufferedImage(width, HEIGHT,BufferedImage.TYPE_INT_RGB);Graphics g = image.getGraphics();Random random = new Random();if ( drawBgFlag )g.setColor(new Color(rBg,gBg,bBg);g.fillRect(0, 0, width, HEIGHT);elseg.setColor(getRandColor(200, 250);g.fillRect(0, 0, width, HEIGHT);for (int i
17、 = 0; i 155; i+) g.setColor(getRandColor(140, 200);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);g.setFont(new Font(Times New Roman, Font.PLAIN, 18);String sRand=;for (int i=0;i 255)fc = 255;if (b
18、c 255)bc = 255;int r = fc + random.nextInt(bc - fc);int g = fc + random.nextInt(bc - fc);int b = fc + random.nextInt(bc - fc);return new Color(r, g, b);view plaincopy to clipboardprint?*viewclipboardprint?3.调用生成验证码图片:img.jsp3.调用生成验证码图片:img.jspview plaincopy to clipboardprint?* 4.测试,验证,刷新: name=code% page plaincopy toview plaincopy to clipboardprint?response.setHeader(Pragma,No-cache); name=code% page * 4.测试,验证,刷新: view plaincopy to clipboardprint? view plaincopy to clipboardprint?* *view plaincopy to clipboardprint?view plaincopy to clipboardprint?4.测试,验证,刷新: 4.测试,验证,刷新:view plainc
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026紫米灰家用电器市场分析及家电下乡政策与品牌差异发展研究
- 2026智慧医疗系统建设标准与投资效益评估报告
- 婴儿脐部渗水护理
- 心肌梗死CI术后护理
- ISO 18788 2015 中文版 安保运营服务管理体系要求
- 2026中国自动驾驶仿真测试平台数据迭代效率评估报告
- 2026全国执业兽医资格考试(水生动物类-临床科目)历年参考题库含答案详解
- 2026信号工-电子电气设备维修(官方)-中级工参考试题库历年考点答案详解
- 2026住院医师规培-黑龙江-黑龙江住院医师规培(全科医学)历年参考题库含答案详解
- 2026住院医师规培-贵州-贵州住院医师规培(骨科)历年参考题库含答案详解
- 2025-2026学年湖南师大附中七年级(上)期中数学试卷
- (2025)义务教育英语课程标准【2025年版】测试题(含参考答案)
- 药事管理学考试真题及答案
- 浙江省精诚联盟2025-2026学年高一上学期10月月考化学试题(含答案)
- LNG消防安全培训内容课件
- 养殖蝎子知识培训课件
- 急性重症有机磷农药中毒护理疑难病例讨论
- 水下测绘培训课件下载
- 2024-2025学年度自考专业(学前教育)真题(考试直接用)附答案
- 人工智能通识 课件 第七章 智能之躯-具身智能
- T/CHES 54-2021取水权交易可行性报告编制导则
评论
0/150
提交评论