后台生成数字英文验证码,前台调用

本示例简单实现验证码的功能,验证码只有数字和英文;生成验证码是在后台Java文件。

代码

java
    <img src="getImage.do" />
    <form action="Verify.do">
        <input type="text" name="input_verify"/>
        <input type="submit" />
    </form>
java
String verify_value = "";
    @RequestMapping("/getImage")
    public void getImage(HttpServletRequest request, HttpServletResponse response) throws IOException{
        VerifyCode v = new VerifyCode();
        VerifyCode.output(v.getImage(), response.getOutputStream());
        verify_value = v.getText();
    }

    @RequestMapping("/Verify")
    public void Verify(HttpServletRequest request, HttpServletResponse response) throws IOException{
        String input_verify = request.getParameter("input_verify");
        System.out.println("input_verify:" + input_verify);
        System.out.println("verify_value:" + verify_value);
    }

如何生成验证码,请君往下看~:

java
public class VerifyCode {
    private int w=100;
    private int h=50;
    private Random r=new Random();
    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 style=r.nextInt(4);
        int size=r.nextInt(5)+45;
        return new Font(fontName,style,size);
    }

    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);  //填充矩形区域,前两个:x,y   后两个:宽,高
        return image;
    }

    private void drawLine(BufferedImage image){
        int num=5;
        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(Color.BLUE);
            g2.drawLine(x1, y1, x2, y2);
        }
    }

    public BufferedImage getImage() {
        BufferedImage image=createImage();
        Graphics2D g2=(Graphics2D)image.getGraphics();
        StringBuilder sb=new StringBuilder();
        for(int i=0;i<4;i++){
            String s=randomChar()+"";
            sb.append(s);
            float x=i*1.0f*w/4;
            g2.setFont(randomFont());
            g2.setColor(randomColor());
            g2.drawString(s,x,h-5);
        }
        this.text=sb.toString();
        drawLine(image);
        return image;
    }

    public static void output(BufferedImage image,
            ServletOutputStream outputStream) throws IOException {
        ImageIO.write(image, "JPEG", outputStream);     
    }
    public String getText() {
        return text;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
包括:times new roman,中山行书百年纪念版,calibri,Christopherhand,DejaVuSansMono,方正兰亭黑,James Fajardo,Monaco,微软雅黑,仿宋,黑体,楷体,宋体,yahei_mono,仿宋_GB2312,楷体_GB2312,迷你简行楷碑。 文件名列表: bb1550.ttf calibri.ttf calibrib.ttf calibrii.ttf calibriz.ttf comesinhandy.ttf DejaVuSansMono-Bold.ttf DejaVuSansMono-BoldOblique.ttf DejaVuSansMono-Oblique.ttf DejaVuSansMono.ttf DroidSansFallback.ttf James_Fajardo.ttf Monaco.ttf msyh.ttf msyhbd.ttf simfang.ttf simhei.ttf simkai.ttf simsun.ttc times.ttf timesbd.ttf timesbi.ttf timesi.ttf yahei_mono.ttf 仿宋_GB2312.ttf 使用方法.txt 楷体_GB2312.ttf 迷你简行楷碑.TTF 使用方法: ==================================================================================================================== ==================================================================================================================== windows: 将所有的ttf文件拷贝到 系统所在分区的windows\fonts 目录下即可 linux: Install and configure Unicode TrueType fonts in Linux Uncompress the downloaded font archive to a directory and add it to the font path, a list of directories containing fonts: 1. Uncompress the archive # tar xvzf utf8.tar.gz or # tar xvjf arial.tar.bz2 2. Create a directory for new fonts # mkdir /usr/share/fonts/truetype 3. Move the uncompressed font files to the new font directory # mv *.ttf /usr/share/fonts/truetype 4. Navigate to the font directory # cd /usr/share/fonts/truetype 5. Create fonts.scale and fonts.dir # mkfontscale && mkfontdir # fc-cache 6. Add the new font directory to the X11 font path # chkfontpath --add /usr/share/fonts/truetype 7. Restart X font server # /etc/rc.d/init.d/xfs restart You can verify the successful addition of the new path by running chkfontpath command or by listing X font server's /etc/X11/XF86Config file. If you do not have root access, copy the *.ttf to ~/.fonts directory instead. Make X11 fonts available to Java Perform one of the following: 1. Open /etc/profile and add a new environment variable JAVA_FONTS=/usr/share/fonts/truetype export JAVA_FONTS 2. Open font.properties file under jre/lib directory, uncommnent and set to the appropriate font directory appendedfontpath=/usr/share/fonts/truetype
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值