1、Jsp图片验证码及刷新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
2、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对象。指定图片的长度宽度和色彩。 Graphics g = image.getGraphics();/取得Graphics对象,用来绘制图片。 g.setColor(
3、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对象所占用的资源。 try ImageIO.write(image, jpeg, new File(C:helloImage.jpeg);/通过ImageIO对象的write静态方法将图片输出。 catch(IOException e) e.printStackTrace(); package liuyanba
4、n;import java.awt.Color;import java.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_IN
5、T_RGB);/建立BufferedImage对象。指定图片的长度宽度和色彩。 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对象所占用的资源。 try ImageIO.write(image, jpeg, new File(
6、C:helloImage.jpeg);/通过ImageIO对象的write静态方法将图片输出。 catch(IOException e) e.printStackTrace(); view plaincopy to clipboardprint?* *view plaincopy to clipboardprint?2.生成验证码图片的类 2.生成验证码图片的类view plaincopy to clipboardprint?package liuyanban; import java.awt.Color; import java.awt.Font; import java.awt.Graph
7、ics; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.Random; import javax.imageio.ImageIO; import javax.servlet.http.HttpServletResponse; /* * 生成验证码图片的类 */ public class RandImgCreater private static final String CODE_LIST = ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnop
8、qrstuvwxyz1234567890; private HttpServletResponse response = null; private 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; priva
9、te int bBg = 0; public RandImgCreater(HttpServletResponse response) this.response = 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 * iNu
10、m + 12; this.iNum = iNum; this.codeList = codeList; public String createRandImage() BufferedImage 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
11、, width, HEIGHT); else g.setColor(getRandColor(200, 250); g.fillRect(0, 0, width, HEIGHT); for (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
12、 + yl); g.setFont(new Font(Times New Roman, Font.PLAIN, 18); String sRand=; for (int i=0;i 255) fc = 255; 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.a
13、wt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.IOException;import java.util.Random;import javax.imageio.ImageIO;import javax.servlet.http.HttpServletResponse;/* 生成验证码图片的类*/public class RandImgCreater private static final String CODE_LIST = A
14、BCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890; private HttpServletResponse response = null; private 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; privat
15、e int rBg = 0; private int gBg = 0; private int bBg = 0; public RandImgCreater(HttpServletResponse response) this.response = response; this.width = 13 * FONT_NUM + 12; this.iNum = FONT_NUM; this.codeList = CODE_LIST; public RandImgCreater(HttpServletResponse response,int iNum,String codeList) this.r
16、esponse = response; this.width = 13 * iNum + 12; this.iNum = iNum; this.codeList = codeList; public String createRandImage() BufferedImage image = new BufferedImage(width, HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics(); Random random = new Random(); if ( drawBgFlag ) g.setColo
17、r(new Color(rBg,gBg,bBg); g.fillRect(0, 0, width, HEIGHT); else g.setColor(getRandColor(200, 250); g.fillRect(0, 0, width, HEIGHT); for (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 = rando
18、m.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 (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(
19、r, g, b); view plaincopy to clipboardprint?* *view plaincopy to clipboardprint?3.调用生成验证码图片:img.jsp 3.调用生成验证码图片:img.jspview plaincopy to clipboardprint? * 4.测试,验证,刷新: view plaincopy to clipboardprint? * 4.测试,验证,刷新: view plaincopy to clipboardprint? view plaincopy to clipboardprint?* *view plaincopy to clipboardprint? view plaincopy to clipboardprint?4.测试
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1