package com.bjsxt.controller;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.sql.rowset.serial.SerialException;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
@WebServlet("/codeServlet")
class CodeServlet extends HttpServlet{
@override
protected void service(HttpServletRequest req,HttpServletResponse resp)throws SerialException, IOException {
int width = 110,height = 30;
BufferedImage image = new BufferedImage(width,herght,BufferedImage,TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setColor(getRandColor(fc:200,bc:250));
g.fillRect(x:0,y:0,width,height);
g.setFont(new Font(name:"Times New Roman", Font.PLAIN,size:20));
g.setColor(getRandColor(fc:160,bc:200));
for(int i=0;i<155;i++){
int x = randdom.nextInt(width);
int y = randdom.nextInt(height);
int x1 = randdom.nextInt(12);
int y1 = randdom.nextInt(12);
g.drawLine(x,y,x2:x+x1,y2:y+y1);
}
String sRand = "";
for(int i = 0;i<6;i++){
String rand = String.valueOf(randm.nextInt(10));
sRand+=rand;
g.setColor(new Color(20 + random.nextInt(100),20 + rand.nexInt(110),20 + rand.nextInt(110)));
g.drawString(rand,13 * i +6,16);
}
g.dispose();
try{
InmageIO.write(image,image,"JPEG",response.getOutputStream());
}catch(Exception e){
System.out.println("验证码图片产生出现错误:"+e.toString());
}
req.getSession().setAttribute("randStr",sRand);
}
private Color getRandColor(int fc,int bc){
Random random = new Random();
if(fc > 255)
fc = 255;
if(bc >255)
bc = 255;
int r = fc + random.nextInt(bound:bc - fc);
int g = fc + random.nextInt(bound:bc - fc);
int b = fc + random.nextInt(bound:bc - fc);
return new Color(r,g,b);
}
}