• 验证码


     原创链接:http://www.cnblogs.com/yanqin/p/5345037.html (允许转载,但请注明原创链接) 

    package com.me.util;
    
    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics2D;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.util.Random;
    
    import javax.imageio.ImageIO;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    
    public class VerifyCodeServlet extends HttpServlet {
    
    	
    	private static final long serialVersionUID = 1L;
    
    	@Override
    	public void doGet(HttpServletRequest req, HttpServletResponse resp)
    			throws ServletException, IOException {
    		//画图
    		BufferedImage img = getImage();
    		//输出
    		output(img, resp.getOutputStream());
    		//将验证码存到session
    		req.getSession().setAttribute("vCode",text);
    	}
    	
    	
    	private int w = 120;
    	private int h = 40;
     	private Random r = new Random();
     	// {"宋体", "华文楷体", "黑体", "华文新魏", "华文隶书", "微软雅黑", "楷体_GB2312"}
    	private String[] fontNames  = {"宋体", "华文楷体", "黑体", "微软雅黑", "楷体_GB2312"};
    	private String codes  = "23456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ";
    	//背景色
    	private Color bgColor  = new Color(255, 255, 255);
    	private String text ;
    	
    	//字体颜色
    	private Color randomColor () {
    		int red = r.nextInt(150);
    		int green = r.nextInt(150);
    		int blue = r.nextInt(150);
    		return new Color(red, green, blue);
    	}
    	
    	//字体
    	private Font randomFont () {
    		int index = r.nextInt(fontNames.length);
    		String fontName = fontNames[index];
    		int size = r.nextInt(4) + 32; 
    		return new Font(fontName, Font.PLAIN, size);
    	}
    	
    	//画线
    	private void drawLine (BufferedImage image) {
    		int num  = 3;
    		Graphics2D g2 = (Graphics2D)image.getGraphics();
    		for(int i = 0; i < num; i++) {
    			int x1 = r.nextInt(w);
    			int y1 = r.nextInt(h);
    			int x2 = r.nextInt(w);
    			int y2 = r.nextInt(h); 
    			g2.setStroke(new BasicStroke(1.5F)); 
    			g2.setColor(new Color(200,200,200)); 
    			g2.drawLine(x1, y1, x2, y2);
    		}
    	}
    	
    	//随机字
    	private char randomChar () {
    		int index = r.nextInt(codes.length());
    		return codes.charAt(index);
    	}
    	
    	//画背景
    	private BufferedImage createImage () {
    		BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); 
    		Graphics2D g2 = (Graphics2D)image.getGraphics(); 
    		g2.setColor(this.bgColor);
    		g2.fillRect(0, 0, w, h);
     		return image;
    	}
    	
    	//绘画
    	private BufferedImage getImage () {
    		BufferedImage image = createImage(); 
    		Graphics2D g2 = (Graphics2D)image.getGraphics();
    		StringBuilder sb = new StringBuilder();
    		// 向图片中画4个字符
    		for(int i = 0; i < 4; i++)  {
    			String s = randomChar() + ""; 
    			sb.append(s); 
    			float x = i * w / 5 + 12; 
    			g2.setColor(randomColor());
    			g2.setFont(randomFont());
    			g2.drawString(s, x, h-5);
    		}
    		this.text = sb.toString();
    		drawLine(image);
    		return image;
    	}
    	
    	private static void output (BufferedImage image, OutputStream out) 
    				throws IOException {
    		ImageIO.write(image, "JPEG", out);
    	}
    	
    }
    

      

  • 相关阅读:
    linux内核中如何访问寄存器?
    uboot加载itb文件后提示"ERROR: new format image overwritten"如何处理?
    如何单独编译Linux内核源码中的驱动为可加载模块?
    openwrt如何打开linux内核的CONFIG_DEVMEM选项?
    openwrt的shell下如何访问寄存器的内容?
    linux系统错误码大全
    第 3 章 文本元素
    第 2 章 基本格式
    第 1 章 HTML5 概述
    第 20 章 项目实战--案例和关于[7]
  • 原文地址:https://www.cnblogs.com/yanqin/p/5345037.html
Copyright © 2020-2023  润新知