您好,登錄后才能下訂單哦!
-- 用于生成驗證碼
- public class RandomNumUtil {
- private ByteArrayInputStream p_w_picpath;// 圖像
- private String str;// 驗證碼
- public RandomNumUtil() {
- init();
- }
- private void init() {
- // 在內存中創(chuàng)建圖象
- int width = 85, height = 20;
- BufferedImage p_w_picpath = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
- // 獲取圖形上下文
- Graphics g = p_w_picpath.getGraphics();
- // 生成隨機類
- Random random = new Random();
- // 設定背景色
- g.setColor(getRandColor(200, 250));
- g.fillRect(0, 0, width, height);
- // 設定字體
- g.setFont(new Font("Times New Roman", Font.PLAIN, 12));
- // 隨機產(chǎn)生155條干擾線,使圖象中的認證碼不易被其它程序探測到
- g.setColor(getRandColor(160, 200));
- for (int i = 0; i < 155; i++) {
- int x = random.nextInt(width);
- int y = random.nextInt(height);
- int xl = random.nextInt(12);
- int yl = random.nextInt(12);
- g.drawLine(x, y, x + xl, y + yl);
- }
- // 取隨機產(chǎn)生的認證碼(6位數(shù)字)
- String sRand = "";
- for (int i = 0; i < 6; i++) {
- String rand = getVerify(random.nextInt(3));
- sRand += rand;
- // 將認證碼顯示到圖象中
- g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
- // 調用函數(shù)出來的顏色相同,可能是因為種子太接近,所以只能直接生成
- g.drawString(rand, 13 * i + 6, random.nextInt(10) + 10);
- }
- // 賦值驗證碼
- this.str = sRand;
- // 圖象生效
- g.dispose();
- ByteArrayInputStream input = null;
- ByteArrayOutputStream output = new ByteArrayOutputStream();
- try {
- ImageOutputStream p_w_picpathOut = ImageIO.createImageOutputStream(output);
- ImageIO.write(p_w_picpath, "JPEG", p_w_picpathOut);
- p_w_picpathOut.close();
- input = new ByteArrayInputStream(output.toByteArray());
- } catch (Exception e) {
- System.out.println("驗證碼圖片產(chǎn)生出現(xiàn)錯誤:" + e.toString());
- }
- this.p_w_picpath = input;/* 賦值圖像 */
- }
- /* 隨機產(chǎn)生顏色 */
- private Color getRandColor(int fc, int bc) {
- Random random = new Random();
- if (fc > 255)
- fc = 255;
- if (bc > 255)
- bc = 255;
- int r = fc + random.nextInt(bc - fc);
- int g = fc + random.nextInt(bc - fc);
- int b = fc + random.nextInt(bc - fc);
- return new Color(r, g, b);
- }
- /**
- * 生成驗證碼
- * @param i 0為小寫字母
- * 1 為大寫字母
- * 2為普通數(shù)字
- * @return
- */
- private String getVerify(int i){
- String str = "";
- if(i == 0){
- char c = 'a';
- c=(char)(c+(int)(Math.random()*26));
- str = c + "";
- }else if(i == 1){
- char c='A';
- c=(char)(c+(int)(Math.random()*26));
- str = c + "";
- }else if(i == 2){
- str = new Random().nextInt(10) + "";
- }
- return str;
- }
- public ByteArrayInputStream getImage() {
- return p_w_picpath;
- }
- public void setImage(ByteArrayInputStream p_w_picpath) {
- this.p_w_picpath = p_w_picpath;
- }
- public String getStr() {
- return str;
- }
- public void setStr(String str) {
- this.str = str;
- }
-- Struts2
- public class UtilAction extends ActionSupport {
- private ByteArrayInputStream inputStream;
- @Override
- public String execute() throws Exception {
- // TODO Auto-generated method stub
- RandomNumUtil rdnu = new RandomNumUtil();
- this.setInputStream(rdnu.getImage());//取得帶有隨機字符串的圖片
- ActionContext.getContext().getSession().put("validateCode", rdnu.getStr());//取得隨機字符串放入HttpSession
- return SUCCESS;
- }
- public ByteArrayInputStream getInputStream() {
- return inputStream;
- }
- public void setInputStream(ByteArrayInputStream inputStream) {
- this.inputStream = inputStream;
- }
- }
-- JQuery
- $(function() {
- $("#randomCode").attr("src","verify");
- $("#refresh").click(
- function() {
- $("#randomCode").attr("src","verify");
- });
- })
-- Struts2 xml配置
- <action name="verify" class="verify.UtilAction">
- <result type="stream">
- <param name="contentType">p_w_picpath/jpeg</param>
- <param name="inputName">inputStream</param>
- </result>
- </action>
-- jsp頁面
- <body>
- <img src="" width=150 height="50" alt="驗證碼圖片" id="randomCode" />
- <span id="refresh" style="cursor: pointer;">換張圖片</span>
- </body>
--效果如圖
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。