开发工具:
文件大小: 3kb
下载次数: 0
上传时间: 2012-01-06
详细说明: package Cls; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.Random; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; public class MyVerifyCode extends HttpServlet { private sta tic MyVerifyCode instance; private final String ATTRIBUTE_NAME = "verifycode"; // ��֤���ַ������� private final int CODE_LENGTH =4; // ��֤��߶����� private static final int Height =30; // ������֤���Ƿ���Ҫ��תЧ�� private boolean ROTATE_FLAG = true; //���������Ƿ���Ҫ��� private boolean FONT_FLAG=false; //�����������Ĭ������ private String font=""; //��������Ĵ�С,Ĭ��Ϊ15 private int fontsize=15; private final String RAND_RANGE = "abcdefhgkmnpqrstuvwxyz" + "ABCDEFGHGKLMNPQRSTUVWXYZ" + "2345678"; // private Random rand=new Random(); public static Random rand = new Random(); private final char[] CHARS = RAND_RANGE.toCharArray(); // ���캯�� public MyVerifyCode() { } public static MyVerifyCode getInstance() { if (instance == null) instance = new MyVerifyCode(); return instance; } // �������ַ� private String getRandString() { StringBuffer vcode = new StringBuffer(); for (int i = 0; i < CODE_LENGTH; i++) vcode.append(CHARS[rand.nextInt(CHARS.length)]); return vcode.toString(); } // ��������� public static float getRandomJiao() { float jiao = 0.0f; if (rand.nextFloat()<=0.5) { jiao = rand.nextFloat()-0.3f; } else { jiao = -rand.nextFloat()+0.3f; } return jiao; } // ��������ɫ private Color getRandColor(int ll, int ul) { if (ll > 255) ll = 255; if (ll < 1) ll = 1; if (ul > 255) ul = 255; if (ul < 1) ul = 1; if (ul == ll) ul = ll + 1; int r = rand.nextInt(ul - ll) + ll; int g = rand.nextInt(ul - ll) + ll; int b = rand.nextInt(ul - ll) + ll; Color color = new Color(r, g, b); return color; } private BufferedImage getImage(HttpServletRequest request) { //根据设置的高度计算事宜的fontsize int FontSize=Height*75/96; //根据设置的字体及字的个数计算出适宜的宽度 int width = CODE_LENGTH * FontSize*4/5 + 10; //根据字体大小设置X轴的位移量 int X_=FontSize/2; BufferedImage image = new BufferedImage(width,Height, BufferedImage.TYPE_INT_RGB); // ��ȡͼ�������� Graphics graphics = image.getGraphics(); Graphics2D g2 = (Graphics2D) graphics; g2.setColor(getRandColor(100, 255)); g2.setColor(Color.WHITE); g2.fillRect(0, 0, width, Height); g2.translate(0, Height*2/3); double oldrot = 0; String vcode=this.getRandString(); // 把验证码存入session request.getSession(true).setAttribute("codes", vcode); for (int i = 0; i < CODE_LENGTH; i++) { g2.setFont(new Font("Times New Roman", Font.HANGING_BASELINE, FontSize)); double rot = getRandomJiao(); // 旋转画笔 if (ROTATE_FLAG) { g2.rotate(-oldrot); g2.translate(X_+rand.nextInt(8), 0); oldrot = rot; g2.rotate(rot); } String temp = vcode.substring(i, i + 1); g2.setColor(getRandColor(1, 100)); g2.drawString(temp, 0, 0); } // ͼ����Ч g2.dispose(); return image; } public void printImage(HttpServletRequest request, HttpServletResponse response) { // ��ContentType��Ϊ"image/jpeg"���������ʶ��ͼ���ʽ�� response.setContentType("image/jpeg"); // ����ҳ�治���� response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 2000); // ��������֤�� String verifyCode = this.getRandString(); // �����֤���ͼ����� BufferedImage bi = this.getImage(request); try { // ���Servlet����� ServletOutputStream outStream = response.getOutputStream(); // ������������ͼ����ݱ���ΪJPEG������ı����� JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outStream); // ��ͼ����ݽ��б��� encoder.encode(bi); // ǿ�н���������������뵽ҳ�� outStream.flush(); // �ر������ outStream.close(); } catch (IOException ex) { ex.printStackTrace(); } } public void doGet(HttpServletRequest request, HttpServletResponse response) { printImage(request, response); } } ...展开收缩
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.