import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.Random; import com.sun.image.codec.jpeg.ImageFormatException; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; //定義一個工具人,用於產生驗證碼圖片 public class SecurityCode { public static final int WIDTH = 100; public static final int HEIGHT = 25; public static String checkstring ; //獲得驗證碼的BtyArrayInputStream流 public static ByteArrayInputStream getImageAsInputStream() { BufferedImage image = getRandomImage(); return convertImageToStream(image); } //獲得驗證碼圖片 public static BufferedImage getRandomImage() { BufferedImage randomImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics g = randomImage.getGraphics(); setBackground(g); setBorder(g); paintLine(g); checkstring = setRandomString((Graphics2D) g); g.dispose(); return randomImage; } //設置邊框 private static void setBorder(Graphics g) { g.setColor(Color.WHITE); g.drawRect(1, 1, WIDTH - 2, HEIGHT - 2); } //隨機數 private static String setRandomString(Graphics2D g) { StringBuilder sb = new StringBuilder(); g.setFont(new Font("宋體", Font.BOLD, 22)); String randomString = "abcdefghijkmnpqrstuvwsyz23456789"; for (int i = 0; i < 4; i++) { g.setColor(Color.red); int x = 20 * i + 10; String str = randomString.charAt(new Random().nextInt(randomString .length())) + ""; sb.append(str); int roll = new Random().nextInt() % 30; g.rotate(roll * Math.PI / 180, x, 20); g.drawString(str, x, 20); g.rotate(-roll * Math.PI / 180, x, 20); } return sb.toString(); } //畫干擾線 private static void paintLine(Graphics g) { g.setColor(Color.GREEN); for (int i = 0; i < 5; i++) { int x1 = new Random().nextInt(WIDTH); int y1 = new Random().nextInt(HEIGHT); int x2 = new Random().nextInt(WIDTH); int y2 = new Random().nextInt(HEIGHT); g.drawLine(x1, y1, x2, y2); } } //設置背景顏色 private static void setBackground(Graphics g) { // TODO Auto-generated method stub g.setColor(Color.WHITE); g.fillRect(0, 0, WIDTH, HEIGHT); } //將image驗證碼轉化成字節數組輸入流 private static ByteArrayInputStream convertImageToStream(BufferedImage image) { ByteArrayInputStream inputStream = null; ByteArrayOutputStream bos = new ByteArrayOutputStream(); JPEGImageEncoder jpeg = JPEGCodec.createJPEGEncoder(bos); try { jpeg.encode(image); byte[] bts = bos.toByteArray(); inputStream = new ByteArrayInputStream(bts); } catch (ImageFormatException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return inputStream; } } //寫一個Action類,用於接收請求,而且回送驗證碼圖片 public class SecurityCodeImageAction extends ActionSupport implements SessionAware { private Map<String,Object> session; private ByteArrayInputStream imageStream; @Override public String execute() throws Exception{ imageStream = SecurityCode.getImageAsInputStream(); String securityCode = SecurityCode.checkstring; session.put("checkstring", securityCode); return SUCCESS; } public ByteArrayInputStream getImageStream() { return imageStream; } public void setImageStream(ByteArrayInputStream imageStream) { this.imageStream = imageStream; } public Map<String, Object> getSession() { return session; } @Override public void setSession(Map<String, Object> session) { // TODO Auto-generated method stub this.session = session; } } //最後配置Struts.xml文件便可 <!-- 驗證碼映射 --> <action name ="SecurityCodeImageAction" class= "com.web.service.SecurityCodeImageAction"> <result name="success" type="stream"> <param name="contentType">image/jpeg</param> <param name="inputName">imageStream</param> <param name="bufferSize">2048</param> </result> </action>