JAVA验证码实现源码,java简易验证码实现图片验证码简单实现 javaWEB

JAVAweb验证码实现

int width = 100;
int height = 50;
	
	//创建图片对象
	BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
	
	
	//美化图片
	//1. 获取画笔对象
	Graphics g = image.getGraphics();
	//2. 设置画笔颜色
	g.setColor(Color.pink);
	//3. 填充背景色
	g.fillRect(0, 0, width, height);
	//3.1画出边框
	g.setColor(Color.blue);
	g.drawRect(0, 0, width-1, height-1);
	
	
	//3.2改回画笔颜色
	g.setColor(Color.black);
	
	//4. 画出随机4个字符
	String str = "abcdefghijklmnopqrstuv0wxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
	Random r = new Random();
	int x = 10;
	for(int i=0;i<4;i++) {
		char charAt = str.charAt(r.nextInt(str.length())+1);
		
		g.drawString(charAt+"", x+=15, height/2);
	}
	g.setColor(Color.green);
	
	//5. 画出干扰线
	for(int i=0;i<10;i++) {
		int x1 = r.nextInt(width);
		int x2 = r.nextInt(width);
		
		int y1 = r.nextInt(height);
		int y2 = r.nextInt(height);
		g.drawLine(x1, y1, x2, y2);
	}
	
	
	//输出到页面
	ImageIO.write(image, "jpg", response.getOutputStream());

猜你喜欢

转载自blog.csdn.net/Janyi_/article/details/108591563