SoFunction
Updated on 2025-04-10

Android chestnut image verification code generation example code

I won’t say much nonsense. The following code will share with you the function of generating chestnut image verification code for Android. The specific code is as follows;

import ;
import ;
import ;
import ;
import ;
import ;
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' };
  /**
    * Obtain an example
    *
    * @return
    */
  public static Autjcode getInstance() {
    if (bmpCode == null)
      bmpCode = new Autjcode();
    return bmpCode;
  }
  private Random random = new Random();
  /**
    * Create a bitmap
    *
    * @return
    */
  public Bitmap createBitmap() {
    padding_left = 0;
    // ARGB_8888 is represented as 32-bit ARGB bitmap    Bitmap bp = (width, height, Config.ARGB_8888);
    Canvas c = new Canvas(bp);
    code = createCode();
    ();
    Paint paint = new Paint();
    (font_size);
    (true);
    for (int i = 0; i < (); i++) {
      randomTextStyle(paint);
      randomPadding();
      ((i) + "", padding_left, padding_top, paint);
    }
    for (int i = 0; i <= line_number; i++) {
      drawLine(c, paint);
    }
    (Canvas.ALL_SAVE_FLAG);// Save    ();
    return bp;
  }
  public String getCode() {
    return code;
  }
  // Verification code  private String createCode() {
    StringBuilder buffer = new StringBuilder();
    for (int i = 0; i < codeLength; i++) {
      (CHARS[()]);
    }
    return ();
  }
  /**
    * Scribing
    *
    * @param canvas
    * @param paint
    */
  private void drawLine(Canvas canvas, Paint paint) {
    int color = randomColor();
    int startX = (width);
    int startY = (height);
    int stopX = (width);
    int stopY = (height);
    (1);
    (color);
    (startX, startY, stopX, stopY, paint);
  }
  private int randomColor() {
    return randomColor(1);
  }
  private int randomColor(int rate) {
    int red = (256) / rate;
    int green = (256) / rate;
    int blue = (256) / rate;
    return (red, green, blue);
  }
  private void randomTextStyle(Paint paint) {
    int color = randomColor();
    (color);
    (());
    float skewX = (11) / 10;
    skewX = () ? skewX : -skewX;
    (skewX);
  }
  private void randomPadding() {
    padding_left += base_padding_left + (range_padding_left);
    padding_top = base_padding_top + (range_padding_top);
  }
}

How to use:

initialization:

registerAuthimg = (ImageView) findViewById();
(().createBitmap());

Verification code refresh:

(().createBitmap());

Get the currently displayed text verification code:

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

Summarize

The above is the example code for the Android chestnut image verification code generation introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!