Android学习笔记29

平常在登入帐号密码的时候,下面常常会有验证码,接着就试着做做看吧
首先先把所有可能出现的字元打进来
public class IdentifyingCode {
//随机数数组,验证码上的数字和字母
private static final char[] CHARS={
'0','1','2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm',
'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
};
接着在定义相关的数值
private static IdentifyingCode IdentifyingCode = null;

public static IdentifyingCode getInstance(){
if(IdentifyingCode==null){
IdentifyingCode = new IdentifyingCode();
}
return IdentifyingCode;
}

//验证码个数
private static final int CODE_LENGTH = 4;
//字体大小
private static final int FONT_SIZE = 50;
//线条数
private static final int LINE_NUMBER = 5;
//padding,其中base的意思是初始值,而range是变化范围。数值根据自己想要的大小来设置
private static final int BASE_PADDING_LEFT=10,RANGE_PADDING_LEFT=100,BASE_PADDING_TOP=75,RANGE_PADDING_TOP=50;
//验证码默认宽高
private static final int DEFAULT_WIDTH=400,DEFAULT_HEIGHT=150;

//画布的长宽
private int width=DEFAULT_WIDTH,height=DEFAULT_HEIGHT;

//字体的随机位置
private int base_padding_left=BASE_PADDING_LEFT,range_padding_left=RANGE_PADDING_LEFT,
base_padding_top=BASE_PADDING_TOP,range_padding_top=RANGE_PADDING_TOP;
//验证码个数,线条数,字体大小
private int codeLength=CODE_LENGTH,line_number=LINE_NUMBER,font_size=FONT_SIZE;

private String code;
private int padding_left,padding_top;
private Random random=new Random();
然後是验证码图片
//验证码图片(生成一个用位图)
public Bitmap createBitmap(){
padding_left=0;
padding_top=0;
Bitmap bp=Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
Canvas c=new Canvas(bp);

    code =createCode();
    //将画布填充爲白色
    c.drawColor(Color.WHITE);
    //新建一个画笔
    Paint paint =new Paint();
    //设置画笔抗锯齿
    paint.setAntiAlias(true);
    paint.setTextSize(font_size);
    //在画布上画上验证码

// Paint.FontMetrics fontMetrics = paint.getFontMetrics();
for(int i=0;i<code.length();i++){
randomTextStyle(paint);
randomPadding();
//这里的padding_left,padding_top是文字的基线
c.drawText(code.charAt(i)+"",padding_left,padding_top,paint);
}
//画干扰线
for(int i = 0;i<line_number;i++){
drawLine(c,paint);
}
//保存一下画布
c.save();
c.restore();
return bp;
}
最後是生成验证码与画线
//生成验证码
private String createCode(){
StringBuilder sb=new StringBuilder();
//利用random生成随机下标
for(int i=0;i<codeLength;i++){
sb.append(CHARS[random.nextInt(CHARS.length)]);
}
return sb.toString();
}

//画线
private void drawLine(Canvas canvas,Paint paint){
    int color=randomColor();
    int startX=random.nextInt(width);
    int startY=random.nextInt(height);
    int stopX=random.nextInt(width);
    int stopY=random.nextInt(height);
    paint.setStrokeWidth(1);
    paint.setColor(color);
    canvas.drawLine(startX,startY,stopX,stopY,paint);
}
//随机文字样式,颜色,文字粗细与倾斜度
private void randomTextStyle(Paint paint){
    int color=randomColor();
    paint.setColor(color);
    paint.setFakeBoldText(random.nextBoolean());//true爲粗体,false爲非粗体
    double skew =random.nextInt(11)/10;
    //随机ture或者false来生成正数或者负数,来表示文字的倾斜度,负数右倾,正数左倾
    skew=random.nextBoolean()?skew:-skew;
    //   paint.setUnderlineText(true);//下划线
    // paint.setStrikeThruText(true);//删除线
}
//生成随机颜色,利用RGB
private int randomColor(){
    return randomColor(1);
}
private int randomColor(int rate){
    int red=random.nextInt(256)/rate;
    int green=random.nextInt(256)/rate;
    int blue = random.nextInt(256)/rate;
    return Color.rgb(red,green,blue);
}
//验证码位置随机
private void randomPadding(){

    padding_left+=base_padding_left+random.nextInt(range_padding_left);
    padding_top=base_padding_top+random.nextInt(range_padding_top);
}

public String getCode(){
    return code;
}

}
下一篇会结合一般帐号密码的逻辑使用


<<:  如何在 Angular 建立 Breadcrumb (面包屑)

>>:  DAY24神经网路(续二)

DAY30:Strategy Pattern,选定不同的策略来执行

什麽是 Strategy Pattern? 设计相同介面但不同实作的物件,再由使用端以此介面去选择要...

【Day01】楔子-关於永丰金融APIs

iT邦帮忙一直以来都是我查询技术问题的好夥伴;而铁人赛为IT界名闻遐迩的年度盛事。 在友人极力鼓吹报...

从 IT 技术面细说 Search Console 的 27 组数字 KPI (16) :结构化资料(流量)

虽然结构化资料是从 2011 年就开始运作,但从 RDFa 到 Microformat 及 OWL ...

[Day14] 补充说明 – Cookie、Session和Token之Part1

哈罗大家好,我们今天来简单补充一下cookie、Session和Token,这里我会分成两天来介绍,...

成为工具人应有的工具包-27 ShellMenuView

ShellMenuView 今天一样来认识这个看 shell 外挂选单的工具(?) ShellMen...