產生圖片驗證碼

來源:互聯網
上載者:User

標籤:ima   string   一個   jpg   ext   dom   lin   設定   void   

 1  package util; 2   3 import javax.imageio.ImageIO; 4 import java.awt.*; 5 import java.awt.image.BufferedImage; 6 import java.io.File; 7 import java.io.IOException; 8 import java.util.Random; 9  10 public class image {11     public static int[] ran() {12         //設定圖片寬度和高度13         int width = 150;14         int height = 60;15         //幹擾線條數16         int lines = 10;17 //        驗證碼數組18         int[] random = new int[4];19         Random r = new Random();20         BufferedImage b = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);21         Graphics g = b.getGraphics();22         g.setFont(new Font("宋體", Font.BOLD, 30));23         for (int i = 0; i < 4; i++) {24             int number = r.nextInt(10);25             random[i] = number;26             int y = 10+r.nextInt(40);// 10~40範圍內的一個整數,作為y座標27             //隨機顏色,RGB模式28             Color c = new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255));29             g.setColor(c);30 //            g.drawString("" + a, 5 + i * width / 4, y);31             //寫驗證碼32             g.drawString(Integer.toString(number), 5 + i * width / 4, y);33         }34         for (int i = 0; i < lines; i++) {35             //設定幹擾線顏色36             Color c = new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255));37             g.setColor(c);38             g.drawLine(r.nextInt(width), r.nextInt(height), r.nextInt(width), r.nextInt(height));39         }40         try {41             //清空緩衝42             g.dispose();43             //向檔案中寫入44             ImageIO.write(b, "jpg", new File("E:\\IntelliJ IDEA\\mail\\web\\imagecode\\test.jpg"));45         } catch (IOException e) {46             e.printStackTrace();47         }48         return random;49     }50  51     //測試52     public static void main(String[] args) {53         ran();54     }55 } 

 

產生圖片驗證碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.