欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 获取随机验证码功能示例

程序员文章站 2023-11-13 16:34:10
验证码功能在各大网站都能用到,下面小编通过实例代码给大家分享android 获取随机验证码功能,具体代码如下所示: package cn.hk.image;...

验证码功能在各大网站都能用到,下面小编通过实例代码给大家分享android 获取随机验证码功能,具体代码如下所示:

 package cn.hk.image; 
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(); 
  // {"宋体", "华文楷体", "黑体", "华文新魏", "华文隶书", "微软雅黑", "楷体_gb2312"} 
  private string[] fontnames = {"宋体", "华文楷体", "黑体", "微软雅黑", "楷体_gb2312"}; 
  // 可选字符 
  private string codes = "23456789abcdefghjkmnopqrstuvwxyzabcdefghjkmnpqrstuvwxyz"; 
  // 背景色 
  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 drawline (bufferedimage image) { 
    int num = 3;//一共画3条 
    graphics2d g2 = (graphics2d)image.getgraphics(); 
    for(int i = 0; i < num; i++) {//生成两个点的坐标,即4个值 
      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);//画线 
    } 
  } 
  // 随机生成一个字符 
  private char randomchar () { 
    int index = r.nextint(codes.length()); 
    return codes.charat(index); 
  } 
  // 创建bufferedimage,图片缓存区 
  private bufferedimage createimage () { 
    bufferedimage image = new bufferedimage(w, h, bufferedimage.type_int_rgb);  
    graphics2d g2 = (graphics2d)image.getgraphics();  
    g2.setcolor(this.bgcolor); 
    g2.fillrect(0, 0, w, h); 
    return image; 
  } 
  // 调用这个方法得到验证码 
  public bufferedimage getimage () { 
    bufferedimage image = createimage();//创建图片缓冲区  
    graphics2d g2 = (graphics2d)image.getgraphics();//得到绘制环境 
    stringbuilder sb = new stringbuilder();//用来装载生成的验证码文本 
    // 向图片中画4个字符 
    for(int i = 0; i < 4; i++) {//循环四次,每次生成一个字符 
      string s = randomchar() + "";//随机生成一个字母  
      sb.append(s); //把字母添加到sb中 
      float x = i * 1.0f * w / 4; //设置当前字符的x轴坐标 
      g2.setfont(randomfont()); //设置随机字体 
      g2.setcolor(randomcolor()); //设置随机颜色 
      g2.drawstring(s, x, h-5); //画图 
    } 
    this.text = sb.tostring(); //把生成的字符串赋给了this.text 
    drawline(image); //添加干扰线 
    return image;     
  } 
  // 返回验证码图片上的文本 
  public string gettext () { 
    return text; 
  } 
  // 保存图片到指定的输出流 
  public static void output (bufferedimage image, outputstream out)  
        throws ioexception { 
    imageio.write(image, "jpeg", out); 
  } 
} 

以上所述是小编给大家介绍的android 获取随机验证码功能示例,希望对大家有所帮助