import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DynamicImage extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
BufferedImage bi = new BufferedImage(50,30,BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bi.createGraphics();
g2d.setColor(Color.white);
g2d.fillRect(0, 0, 50, 30);
g2d.setColor(Color.black);
g2d.drawRect(5, 5, 40, 20);
StringBuilder sbl = new StringBuilder();
while(sbl.length() < 4){
sbl.append(Integer.toString((int)(Math.random() * 9)));
}
g2d.setFont(new Font("Default",Font.BOLD,13));
g2d.drawString(sbl.toString(), 10, 20);
for(int i = 0; i < 20; i++){
int x = (int)(Math.random() * 40) + 5;
int y = (int)(Math.random() * 20) + 5;
g2d.fillOval(x, y, 1, 1);
}
response.setContentType("image/jpeg");
ServletOutputStream sos = response.getOutputStream();
Iterator ite = ImageIO.getImageWritersByFormatName("jpeg");
ImageWriter iw = (ImageWriter)ite.next();
ImageOutputStream ios = ImageIO.createImageOutputStream(sos);
iw.setOutput(ios);
iw.write(bi);
ios.flush();
sos.close();
}
}
2011年10月23日 星期日
動態產生密碼圖片
DynamicImage.java
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言