使用java產生字母驗證碼_java

來源:互聯網
上載者:User

本文執行個體為大家分享了java產生字母驗證碼的具體代碼,供大家參考,具體內容如下

import java.awt.BasicStroke;import java.awt.Color;import java.awt.Font;import java.awt.Graphics2D;import java.awt.image.BufferedImage;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="234567890qwertyuipasdfghjkzxcvbnmQWERTYUIPASDFGHJKZXCVBNM";  private Color bgColor=new Color(255,255,255);  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);//0 無 1 粗體 2 斜體 3 粗+斜    int size=r.nextInt(5)+24; //產生隨機字型大小 24~28    return new Font(fontName, style, size);  }  //幹擾線  private void drowLine(BufferedImage image){    //產生4條幹擾線    int num=4;    Graphics2D bi=(Graphics2D) image.getGraphics();    for (int i = 0; i < num; i++) {      int x1=r.nextInt(w);      int x2=r.nextInt(w);      int y1=r.nextInt(h);      int y2=r.nextInt(h);      bi.setStroke(new BasicStroke(1.5F));      bi.setColor(Color.BLUE);//幹擾線色      bi.drawLine(x1,y1,x2,y2);    }  }  //隨機產生一個字元  private char randomChar(){    int index=r.nextInt(codes.length());    return codes.charAt(index);  }  //圖片緩衝區  private BufferedImage createImage(){    BufferedImage image=new BufferedImage(w, h, BufferedImage.TYPE_INT_BGR);    Graphics2D bi=(Graphics2D) image.getGraphics();    bi.setColor(this.bgColor);    bi.fillRect(0, 0, w, h);    return image;  }  //產生圖片  public BufferedImage getImage(){    BufferedImage image=createImage();    Graphics2D bi=(Graphics2D) image.getGraphics();    StringBuilder sb=new StringBuilder();    for (int i = 0; i <4; i++) {      String string=randomChar()+"";      sb.append(string);      //每個字元占圖片1/4寬      float x=i*1.0F*w/4;      //隨機字型格式      bi.setFont(randomFont());      bi.setColor(randomColor());      //把字寫在圖片適當處(h-6指圖片距底部6個高度)      bi.drawString(string, x, h-6);    }    this.text=sb.toString();    drowLine(image);    return image;  }  //返回得到的字型  public String getText(){    return text;  }  //把圖片寫入指定位置  public static void output(BufferedImage image,OutputStream out){    try {      ImageIO.write(image, "JPG", out);    } catch (IOException e) {      e.printStackTrace();    }  }}

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

聯繫我們

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