| ¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.sandu.ximon.admin.security.authcode; |
| | | |
| | | import java.awt.Color; |
| | | import java.awt.Font; |
| | | import java.awt.Graphics2D; |
| | | import java.awt.image.BufferedImage; |
| | | import java.io.FileNotFoundException; |
| | | import java.io.FileOutputStream; |
| | | import java.io.IOException; |
| | | import java.io.OutputStream; |
| | | import java.util.Random; |
| | | |
| | | import javax.imageio.ImageIO; |
| | | |
| | | |
| | | public class ImageVerificationCode { |
| | | |
| | | private int weight = 100; //éªè¯ç å¾ççé¿å宽 |
| | | private int height = 40; |
| | | private String text; //ç¨æ¥ä¿åéªè¯ç çææ¬å
容 |
| | | private Random r = new Random(); //è·åéæºæ°å¯¹è±¡ |
| | | //private String[] fontNames = {"å®ä½", "åææ¥·ä½", "é»ä½", "微软é
é»", "楷ä½_GB2312"}; //å使°ç» |
| | | //å使°ç» |
| | | private String[] fontNames = {"Georgia"}; |
| | | //éªè¯ç æ°ç» |
| | | private String codes = "123456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ"; |
| | | |
| | | /** |
| | | * è·åéæºçé¢è² |
| | | * |
| | | * @return |
| | | */ |
| | | private Color randomColor() { |
| | | int r = this.r.nextInt(225); //è¿é为ä»ä¹æ¯225ï¼å 为å½rï¼gï¼bé½ä¸º255æ¶ï¼å³ä¸ºç½è²ï¼ä¸ºäºå¥½è¾¨è®¤ï¼éè¦é¢è²æ·±ä¸ç¹ã |
| | | int g = this.r.nextInt(225); |
| | | int b = this.r.nextInt(225); |
| | | return new Color(r, g, b); //è¿åä¸ä¸ªéæºé¢è² |
| | | } |
| | | |
| | | /** |
| | | * è·åéæºåä½ |
| | | * |
| | | * @return |
| | | */ |
| | | private Font randomFont() { |
| | | int index = r.nextInt(fontNames.length); //è·åéæºçåä½ |
| | | String fontName = fontNames[index]; |
| | | int style = r.nextInt(4); //éæºè·ååä½çæ ·å¼ï¼0æ¯æ æ ·å¼ï¼1æ¯å ç²ï¼2æ¯æä½ï¼3æ¯å ç²å æä½ |
| | | int size = r.nextInt(10) + 24; //éæºè·ååä½çå¤§å° |
| | | return new Font(fontName, style, size); //è¿åä¸ä¸ªéæºçåä½ |
| | | } |
| | | |
| | | /** |
| | | * è·åéæºå符 |
| | | * |
| | | * @return |
| | | */ |
| | | private char randomChar() { |
| | | int index = r.nextInt(codes.length()); |
| | | return codes.charAt(index); |
| | | } |
| | | |
| | | /** |
| | | * ç»å¹²æ°çº¿ï¼éªè¯ç å¹²æ°çº¿ç¨æ¥é²æ¢è®¡ç®æºè§£æå¾ç |
| | | * |
| | | * @param image |
| | | */ |
| | | private void drawLine(BufferedImage image) { |
| | | int num = r.nextInt(10); //å®ä¹å¹²æ°çº¿çæ°é |
| | | Graphics2D g = (Graphics2D) image.getGraphics(); |
| | | for (int i = 0; i < num; i++) { |
| | | int x1 = r.nextInt(weight); |
| | | int y1 = r.nextInt(height); |
| | | int x2 = r.nextInt(weight); |
| | | int y2 = r.nextInt(height); |
| | | g.setColor(randomColor()); |
| | | g.drawLine(x1, y1, x2, y2); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å建å¾ççæ¹æ³ |
| | | * |
| | | * @return |
| | | */ |
| | | private BufferedImage createImage() { |
| | | //å建å¾çç¼å²åº |
| | | BufferedImage image = new BufferedImage(weight, height, BufferedImage.TYPE_INT_RGB); |
| | | //è·åç»ç¬ |
| | | Graphics2D g = (Graphics2D) image.getGraphics(); |
| | | //è®¾ç½®èæ¯è²éæº |
| | | g.setColor(new Color(255, 255, r.nextInt(245) + 10)); |
| | | g.fillRect(0, 0, weight, height); |
| | | //è¿åä¸ä¸ªå¾ç |
| | | return image; |
| | | } |
| | | |
| | | /** |
| | | * è·åéªè¯ç å¾ççæ¹æ³ |
| | | * |
| | | * @return |
| | | */ |
| | | public BufferedImage getImage() { |
| | | BufferedImage image = createImage(); |
| | | Graphics2D g = (Graphics2D) image.getGraphics(); //è·åç»ç¬ |
| | | StringBuilder sb = new StringBuilder(); |
| | | for (int i = 0; i < 4; i++) //ç»å个å符å³å¯ |
| | | { |
| | | String s = randomChar() + ""; //éæºçæå符ï¼å ä¸ºåªæç»åç¬¦ä¸²çæ¹æ³ï¼æ²¡æç»åç¬¦çæ¹æ³ï¼æä»¥éè¦å°å符åæå符串åç» |
| | | sb.append(s); //æ·»å å°StringBuilderéé¢ |
| | | float x = i * 1.0F * weight / 4; //å®ä¹å符çxåæ |
| | | g.setFont(randomFont()); //设置åä½ï¼éæº |
| | | g.setColor(randomColor()); //设置é¢è²ï¼éæº |
| | | g.drawString(s, x, height - 5); |
| | | } |
| | | this.text = sb.toString(); |
| | | drawLine(image); |
| | | return image; |
| | | } |
| | | |
| | | /** |
| | | * è·åéªè¯ç ææ¬çæ¹æ³ |
| | | * |
| | | * @return |
| | | */ |
| | | public String getText() { |
| | | return text; |
| | | } |
| | | |
| | | public static void output(BufferedImage image, OutputStream out) throws IOException //å°éªè¯ç å¾çååºçæ¹æ³ |
| | | { |
| | | ImageIO.write(image, "JPEG", out); |
| | | } |
| | | } |