java工具類——驗證碼(VerifyCode)

來源:互聯網
上載者:User
import java.awt.BasicStroke;import java.awt.Color;import java.awt.Font;import java.awt.Graphics2D;import java.awt.image.BufferedImage;import java.io.FileNotFoundException;import java.io.IOException;import java.io.OutputStream;import java.util.Random;import javax.imageio.ImageIO;public class VerifyCode {
private int w = 70;private int h = 35;private Random r = new Random();// 定義有那些字型private String[] fontNames = { "宋體", "華文楷體", "黑體", "微軟雅黑", "楷體_GB2312" };// 定義有那些驗證碼的隨機字元private String codes = "23456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ";// 產生背景色private Color bgColor = new Color(250, 250, 250);// 用於gettext 方法 獲得產生的驗證碼文本private String text;// 產生隨機顏色private Color randomColor() {int red = r.nextInt(150);int green = r.nextInt(150);int blue = r.nextInt(150);return new Color(red, green, blue);}// 產生隨機字型private Font randomFont() {int index = r.nextInt(fontNames.length);String fontName = fontNames[index];int style = r.nextInt(4);int size = r.nextInt(5) + 24;return new Font(fontName, style, size);}// 畫幹擾線private void drawLine(BufferedImage image) {int num = 3;Graphics2D g2 = (Graphics2D) image.getGraphics();for (int i = 0; i < num; i++) {int x1 = r.nextInt(w);int y1 = r.nextInt(h);int x2 = r.nextInt(w);int y2 = r.nextInt(h);g2.setStroke(new BasicStroke(1.5F));// 不知道g2.setColor(Color.blue);g2.drawLine(x1, y1, x2, y2);}}// 得到codes的長度內的隨機數 並使用charAt 取得隨機數位置上的codes中的字元private char randomChar() {int index = r.nextInt(codes.length());return codes.charAt(index);}// 建立一張驗證碼的圖片public BufferedImage createImage() {BufferedImage image = new BufferedImage(w, h,BufferedImage.TYPE_INT_RGB);Graphics2D g2 = (Graphics2D) image.getGraphics();StringBuilder sb = new StringBuilder();// 向圖中畫四個字元for (int i = 0; i < 4; i++) {String s = randomChar() + "";sb.append(s);float x = i * 1.0F * w / 4;g2.setFont(randomFont());g2.setColor(randomColor());g2.drawString(s, x, h - 5);}this.text = sb.toString();drawLine(image);// 返回圖片return image;}// 得到驗證碼的文本 後面是用來和使用者輸入的驗證碼 檢測用public String getText() {return text;}// 定義輸出的對象和輸出的方向public static void output(BufferedImage bi, OutputStream fos)throws FileNotFoundException, IOException {ImageIO.write(bi, "JPEG", fos);}}

以上,就產生了一個驗證碼。

 寫一個test,產生指定的驗證碼映像jpg

public class test {public static void main(String[] args) throws IOException {VerifyCode code = new VerifyCode();BufferedImage image = code.createImage();ImageIO.write(image,"jpg",new File("F:/image.jpg"));}}


當然,也可以在servlet裡產生。

@WebServlet(urlPatterns="/VerifyCodeServlet.do")public class VerifyCodeServlet extends HttpServlet {@Overrideprotected void service(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {VerifyCode code = new VerifyCode();BufferedImage image = code.createImage();ImageIO.write(image,"jpg",response.getOutputStream());}}


然後展示在.html或.jsp裡

<img src="VerifyCodeServlet.do">



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.