| 日历 
			
				| 
	|  |  | 日 | 一 | 二 | 三 | 四 | 五 | 六 | 
|---|
 | 26 | 27 | 28 | 29 | 30 | 31 | 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 | 1 | 2 | 3 | 4 | 5 | 6 |  |  统计 
		随笔 - 78
		文章 - 1
		评论 - 29
		引用 - 0
	 导航常用链接留言簿(1)随笔分类随笔档案(42)文章档案(37)相册搜索积分与排名最新随笔最新评论
	阅读排行榜评论排行榜  | 
	
	
 
 有时候在我们的网络应用中,防止程序自动登录搞破坏,我们一般都会加上验证码,这些验证码一般来说都是由人来识别的,当然,如果验证码很有规律,或者说很清楚,漂亮,那么也是可能被程序识别的,我以前就识别过某网站的验证码,因为比较有规律,所以被识别了,并且识别率达到99%左右,其实我们可以制作很复杂一点的验证码,添加一些干扰的线条或者字体变形,使程序识别的难度加大,这样,我们的目的也就达到了.
 下面是生成的图片:
 
  
 packagecom.hadeslee; 
 import java.awt.BasicStroke;
 import java.awt.Color;
 import java.awt.Font;
 import java.awt.GradientPaint;
 import java.awt.Graphics2D;
 import java.awt.GraphicsEnvironment;
 import java.awt.Paint;
 import java.awt.Point;
 import java.awt.Stroke;
 import java.awt.image.BufferedImage;
 import java.io.*;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Locale;
 import java.util.Random;
 import javax.imageio.ImageIO;
 
 import javax.servlet.*;
 import javax.servlet.http.*;
 
 /**
 *
 * @author lbf
 * @version
 */
 public class Code extends HttpServlet {
 
 /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
 * @param request servlet request
 * @param response servlet response
 */
 private List<String> fonts=new ArrayList<String>();
 public Code(){
 initFonts();
 }
 private void initFonts(){
 GraphicsEnvironment.getLocalGraphicsEnvironment().preferLocaleFonts();
 String[] names=GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(Locale.CHINA);
 for(String s:names){
 char c=s.charAt(0);
 if(Character.isLowerCase(c)||Character.isUpperCase(c)){
 
 }else{
 fonts.add(s);
 }
 }
 }
 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
 response.setContentType("image/jpeg;charset=UTF-8");
 OutputStream out=response.getOutputStream();
 BufferedImage bi=new BufferedImage(200,50,BufferedImage.TYPE_INT_RGB);
 Graphics2D g=bi.createGraphics();
 char[] cs={'0','1','2','3','4','5','6','7','8','9'};
 char[] use=new char[4];
 g.setColor(new Color(240,240,240));
 g.fillRect(0,0,200,50);
 for(int i=0;i<4;i++){
 Point p=getPoint(i);
 int size=getSize();
 use[i]=cs[(int)(Math.random()*cs.length)];
 // g.setColor(new Color((int)(Math.random()*256),0,(int)(Math.random()*256)));
 g.setPaint(getPaint(p,size));
 g.setFont(new Font(fonts.get((int)(Math.random()*fonts.size())),getFace(),size));
 g.drawString(""+use[i],p.x,p.y);
 }
 g.setStroke(new BasicStroke(1.0f));
 g.setPaint(null);
 for(int i=0;i<4;i++){
 g.setColor(new Color((int)(Math.random()*0x00FFFFFFF)));
 g.drawLine((int)(Math.random()*200),(int)(Math.random()*50),(int)(Math.random()*200),(int)(Math.random()*50));
 }
 Random random = new Random();
 for (int i=0;i<88;i++) {
 int x = random.nextInt(200);
 int y = random.nextInt(50);
 g.setColor(new Color((int)(Math.random()*0x00FFFFFFF)));
 g.setStroke(getStroke());
 g.drawLine(x,y,x,y);
 }
 ImageIO.write(bi,"JPEG",out);
 out.close();
 g.dispose();
 }
 private Stroke getStroke(){
 BasicStroke bs=new BasicStroke((float)(Math.random()*3));
 return bs;
 }
 private Point getPoint(int index){
 return new Point(5+(index*((int)(Math.random()*10)+40)),40);
 }
 private Paint getPaint(Point p,int size){
 GradientPaint gp=new GradientPaint(p.x,p.y,new Color((int)(Math.random()*256),0,(int)(Math.random()*256)),
 p.x,p.y-size,new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256)));
 return gp;
 }
 private int getFace(){
 if(Math.random()*10>5){
 return Font.BOLD;
 }else{
 return Font.ITALIC;
 }
 }
 private int getSize(){
 int[] sizes=new int[20];
 for(int i=0;i<20;i++){
 sizes[i]=30+i;
 }
 return sizes[(int)(Math.random()*sizes.length)];
 }
 
 // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
 /** Handles the HTTP <code>GET</code> method.
 * @param request servlet request
 * @param response servlet response
 */
 protected void doGet(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
 processRequest(request, response);
 }
 
 /** Handles the HTTP <code>POST</code> method.
 * @param request servlet request
 * @param response servlet response
 */
 protected void doPost(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
 processRequest(request, response);
 }
 
 /** Returns a short description of the servlet.
 */
 public String getServletInfo() {
 return "Short description";
 }
 // </editor-fold>
 }
 
     
	    
    
   |