jsp简单生成图形验证码

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*
         * 1,创建图片缓冲区
         * 2,设置其宽高
         * 3,得到这个图片的绘制环境(得到画笔)
         * 4,保存起来
         */
        BufferedImage bi = new BufferedImage(180, 160, BufferedImage.TYPE_INT_RGB);
        Graphics2D g =(Graphics2D)bi.getGraphics();
        g.setColor(Color.green);//把环境设置为白色
        g.fillRect(0, 0, 180, 160);//给图片设置矩形,从x轴0,y轴0开始,填充背景色
        g.setColor(Color.red);
        g.drawString("hello", 180-50, 160-80);//向图片写入字符串,其中2,2表示X,Y轴的坐标
        
        ImageIO.write(bi, "JPEG", new FileOutputStream("D:/XXX.JPG"));
    }

猜你喜欢

转载自blog.csdn.net/weixin_41957098/article/details/88326847