<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ page import="java.awt.image.*,java.awt.*,javax.imageio.*,java.util.Random" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<%
	final int width = 120;
	final int height = 25;
	BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
	Graphics g = image.getGraphics();
	g.setColor(new Color(0,0,0));
	g.drawRect(0, 0, width, height);
	g.setColor(Color.WHITE);
	g.fillRect(1, 1, width-2, height-2);
	g.setColor(Color.PINK);
	g.setFont(new Font("宋体",Font.BOLD,20));
	Random random=new Random();
	int x=20;
	String s="qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM12345678900123456987";
	for(int i =0;i<4;i++){
		int red=random.nextInt(255);
		int green=random.nextInt(255);
		int blue=random.nextInt(255);
		g.setColor(new Color(red,green,blue));
		int str=random.nextInt(s.length());
		g.drawString(s.charAt(str)+"", x, 20);
		x+=20;
		
		
		
	}
	for (int i=0;i<20;i++)
	{
	 int p = random.nextInt(width);
	 int q = random.nextInt(height);
	        int pl = random.nextInt(12);
	        int ql = random.nextInt(12);
	 g.drawLine(p,q,p+pl,q+ql);
	}
	//添加四个随机字母和数字的组合生成,并添加干扰线
    //干扰线绘制使用g.drawLine(开始的x坐标,开始的y坐标,结束的x坐标,结束的y坐标);
    //字符串绘制使用g.drawString(字符串,x坐标,y坐标);
	ServletOutputStream output = response.getOutputStream();
	response.reset();
	ImageIO.write(image,"jpeg",output);

%>
</form>
</body>
</html>


版权声明:本文为qq_34913677原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_34913677/article/details/115394027