资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Android栗子の图片验证码生成实例代码

废话不多说了,下面一段代码给大家分享android 生成栗子图片验证码功能,具体代码如下所示;

成都创新互联专注于企业网络营销推广、网站重做改版、滨海网站定制设计、自适应品牌网站建设、H5场景定制成都做商城网站、集团公司官网建设、成都外贸网站制作、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为滨海等各大城市提供网站开发制作服务。

import java.util.Random;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
public class Autjcode {
  private static Autjcode bmpCode;
  private int width = 100, height = 60;
  private int base_padding_left = 15, range_padding_left = 5,
      base_padding_top = 25, range_padding_top = 30;
  private int codeLength = 4, line_number = 2, font_size = 25;
  private String code;
  private int padding_left, padding_top;
  private static final char[] CHARS = { '2', '3', '4', '5', '6', '7', '8',
      '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'l', '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' };
  /**
   * 获得实例
   * 
   * @return
   */
  public static Autjcode getInstance() {
    if (bmpCode == null)
      bmpCode = new Autjcode();
    return bmpCode;
  }
  private Random random = new Random();
  /**
   * 创建位图
   * 
   * @return
   */
  public Bitmap createBitmap() {
    padding_left = 0;
    // ARGB_8888表示为32位的ARGB位图
    Bitmap bp = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas c = new Canvas(bp);
    code = createCode();
    c.drawColor(Color.WHITE);
    Paint paint = new Paint();
    paint.setTextSize(font_size);
    paint.setFakeBoldText(true);
    for (int i = 0; i < code.length(); i++) {
      randomTextStyle(paint);
      randomPadding();
      c.drawText(code.charAt(i) + "", padding_left, padding_top, paint);
    }
    for (int i = 0; i <= line_number; i++) {
      drawLine(c, paint);
    }
    c.save(Canvas.ALL_SAVE_FLAG);// 保存
    c.restore();
    return bp;
  }
  public String getCode() {
    return code;
  }
  // 验证码
  private String createCode() {
    StringBuilder buffer = new StringBuilder();
    for (int i = 0; i < codeLength; i++) {
      buffer.append(CHARS[random.nextInt(CHARS.length)]);
    }
    return buffer.toString();
  }
  /**
   * 划线
   * 
   * @param canvas
   * @param paint
   */
  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 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 randomTextStyle(Paint paint) {
    int color = randomColor();
    paint.setColor(color);
    paint.setFakeBoldText(random.nextBoolean());
    float skewX = random.nextInt(11) / 10;
    skewX = random.nextBoolean() ? skewX : -skewX;
    paint.setTextSkewX(skewX);
  }
  private void randomPadding() {
    padding_left += base_padding_left + random.nextInt(range_padding_left);
    padding_top = base_padding_top + random.nextInt(range_padding_top);
  }
}

使用方法:

初始化:

registerAuthimg = (ImageView) findViewById(R.id.registerAuthimg);
registerAuthimg.setImageBitmap(Autjcode.getInstance().createBitmap());

验证码刷新:

registerAuthimg.setImageBitmap(Autjcode.getInstance().createBitmap());

获取当前显示的文字验证码:

String Autecodeimg = Autjcode.getInstance().getCode().toUpperCase();

总结

以上所述是小编给大家介绍的Android栗子の图片验证码生成实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对创新互联网站的支持!


新闻名称:Android栗子の图片验证码生成实例代码
文章转载:http://www.cdkjz.cn/article/ipjcee.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220