用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

登录

2016-12-15 作者: 云代码会员举报

[java]代码库

package a;
 
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
 
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 VerifyCode extends HttpServlet {
     
    private final int width = 120;
    private final int height = 30;
 
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
 
        BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
         
        Graphics2D g = (Graphics2D) img.getGraphics();
         
        g.setColor(Color.white);
        g.fillRect(0, 0, width, height);
         
        g.setColor(Color.blue);
        g.drawRect(0, 0, width-1, height-1);
         
        String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
         
        Random ran = new Random();
         
        g.setFont(new Font("宋体",Font.BOLD,20));
         
        int x = 0, y = 20;
        String value="";
        for(int i = 1; i<=4; i++){
             
            int index = ran.nextInt(base.length());
            char ch = base.charAt(index);
             
            int degree = ran.nextInt(60) -30;
            double theta = degree*Math.PI;
             
            g.rotate(theta, x+20*i, 15);
            g.drawString(ch+"", x+20*i, y);
            g.rotate(-theta, x+20*i,15);
             
            value+=ch;
             
        }
        request.getSession().setAttribute("value",value);
         
        g.setColor(Color.yellow);
        int x1,y1,x2,y2;
        for(int i = 1; i<=5; i++){
             
            x1 = ran.nextInt(width);
            y1 = ran.nextInt(height);
            x2 = ran.nextInt(width);
            y2 = ran.nextInt(height);
            g.drawLine(x1, y1, x2, y2);
             
        }
         
         
        g.dispose();
         
        ImageIO.write(img, "jpg", response.getOutputStream());
         
    }
 
 
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
 
        doGet(request, response);
         
    }
 
}

[代码运行效果截图]


登录

[源代码打包下载]




网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...