/** * 随机生成验证图片 * * @author zyen * */ public class RandCodeImageServlet extends HttpServlet { private static final long serialVersionUID = -1257947018545327308L; private static final String SESSION_KEY_OF_RAND_CODE = "randCode"; // todo 要统一常量
//private static final int count = 200;//干扰线条数 private static final int width = 73;//定义图形大小 private static final int height = 21;//定义图形大小 //private Font mFont = new Font("Arial Black", Font.PLAIN, 15); //设置字体 //private static final int lineWidth = 2;//干扰线的长度=1.414*lineWidth
g.setFont(new Font("Times New Roman", Font.PLAIN, 20));
g.setColor(getRandColor(160, 200)); for (int i = 0; i < 155; i++) { int x = random.nextInt(width); int y = random.nextInt(height); int xl = random.nextInt(12); int yl = random.nextInt(12); g.drawLine(x, y, x + xl, y + yl); }
String sRand = ""; for (int i = 0; i < 4; i++) { String rand = String.valueOf(random.nextInt(10)); sRand += rand;
/** * 描述: * @param fc 描述: * @param bc 描述: * @return 描述: */ private Color getRandColor(int fc, int bc) { // 取得给定范围随机颜色 final Random random = new Random(); if (fc > 255) { fc = 255; } if (bc > 255) { bc = 255; } final int r = fc + random.nextInt(bc - fc); final int g = fc + random.nextInt(bc - fc); final int b = fc + random.nextInt(bc - fc);