code.java:
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 Code {
private static final char[] CHARS = {'1','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'
};
private static final int DEFAULT_WIDTH=150,DEFAULT_HEIGHT=50;//默认宽高
private static final int DEFAULT_CODE_LENGTH = 5;
private static final int DEFAULT_FONT_SIZE = 25;
private static final int DEFAULT_LINE_NUMBER = 2;
private String code;
private Random random=new Random();
private int codeLength = DEFAULT_CODE_LENGTH, line_number = DEFAULT_LINE_NUMBER, font_size = DEFAULT_FONT_SIZE;
private int padding_left,padding_top;
private int width=DEFAULT_WIDTH,height=DEFAULT_HEIGHT;
private int base_padding_left = 5,
range_padding_left = 15,
base_padding_top = 15,
range_padding_top = 20;
private static Code bmpCode;
public static Code getInstance(){
if(bmpCode==null){
bmpCode=new Code();
}
return bmpCode;
}
public Bitmap createBitmap(){
Bitmap bitmap=Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas=new Canvas(bitmap);
canvas.drawColor(Color.WHITE);
code=createCode();
System.out.println(code);
Paint paint=new Paint();
paint.setTextSize(font_size);
//画验证码
for(int i=0;i<code.length();i++){
randomTextStyle(paint);
randomPadding();
System.out.println("padding_left, padding_top->"+padding_left+","+padding_top);
canvas.drawText(code.charAt(i)+"", padding_left,padding_top, paint);
}
padding_left=0;
padding_top=0;
/*for(int i=0;i<line_number;i++){
drawLine(canvas, paint);
}*/
return bitmap;
}
public String getCode(){
return code;
}
/*//划线
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 String createCode(){
StringBuffer sb=new StringBuffer();
for(int i=0;i<codeLength;i++){
sb.append(CHARS[random.nextInt(CHARS.length)]);
}
return sb.toString();
}
//随机生成字体风格
private void randomTextStyle(Paint paint){
int color=randomColor();
paint.setColor(color);
paint.setFakeBoldText(random.nextBoolean()); //true为粗体,false为非粗体
float skewX = random.nextInt(11) / 10;
skewX=random.nextBoolean()?skewX:-skewX;
paint.setTextSkewX(skewX); //float类型参数,负数表示右斜,整数左斜
//paint.setUnderlineText(true); //true为下划线,false为非下划线
//paint.setStrikeThruText(true);//true为删除线,false为非删除线
}
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);
}
//随机生成paddingleft和paddingtop的值
private void randomPadding(){
padding_left+=base_padding_left+random.nextInt(range_padding_left);
padding_top=base_padding_top+random.nextInt(range_padding_top);
}
}
identifyActivity.java:
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; import com.example.identifycode.R; public class IdentifyCode extends Activity { Button bt_change,bt_login; private EditText et_identifycode; private ImageView iv_identify; private Code code; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); bt_change=(Button) findViewById(R.id.change); bt_login =(Button) findViewById(R.id.login); iv_identify = (ImageView) findViewById(R.id.iv_identifycode); et_identifycode = (EditText) findViewById(R.id.et_identifycode); code = Code.getInstance(); iv_identify.setImageBitmap(code.createBitmap()); et_identifycode.setText(code.getCode()); System.out.println("验证码:"+code.getCode()); } public void doClick(View v){ switch (v.getId()) { case R.id.change: iv_identify.setImageBitmap(code.createBitmap()); et_identifycode.setText(code.getCode()); System.out.println("验证码:"+code.getCode()); break; case R.id.login: String yanzhengma=et_identifycode.getText().toString().trim(); System.out.println(yanzhengma+",code="+code.getCode()); if(code.getCode().equals(yanzhengma)){ Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(this, "验证码不正确,登录不成功", Toast.LENGTH_SHORT).show(); } break; } } }