Android 登陆页面 图片验证码

话不多说,先上图片:

Android 登陆页面 图片验证码_第1张图片

其中包括了验证码和账号密码验证简单的登陆界面。要想实现这个功能,博主可是花费了一番功夫,今天把经验分享出来,跟大家分享,第一次写博客,后面我也会把我近期Android的一些项目贴出来,供大家踩坑。

CodeUtils

package com.example.myapplication;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

import java.util.Random;

/**
 * Created by HuGuanzZhong.
 * 用于图片验证码的工具类
 */


public class CodeUtils {

//    定义所显示的数据类型
    private static final char[] CHARS = {
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
            'n', 'o', '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', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'


    };

//  绘图准备
    private static CodeUtils mCodeUtils;
    private int mPaddingLeft, mPaddingTop;
    private StringBuilder mBuilder = new StringBuilder();
    private Random mRandom = new Random();

    private static final int DEFAULT_CODE_LENGTH = 6;//验证码的长度  这里6位,可以简单点,简单一点

    private static final int DEFAULT_FONT_SIZE = 60;//字体大小
    private static final int DEFAULT_LINE_NUMBER = 3;//多少条干扰线
    private static final int BASE_PADDING_LEFT = 20; //左边距
    private static final int RANGE_PADDING_LEFT = 30;//左边距范围值
    private static final int BASE_PADDING_TOP = 70;//上边距
    private static final int RANGE_PADDING_TOP = 15;//上边距范围值
    private static final int DEFAULT_WIDTH = 300;//默认宽度.图片的总宽
    private static final int DEFAULT_HEIGHT = 100;//默认高度.图片的总高
    private static final int DEFAULT_COLOR = 0xDF;//默认背景颜色值


    private String code;
    public static CodeUtils getInstance() {
        if (mCodeUtils == null) {
            mCodeUtils = new CodeUtils();
        }
        return mCodeUtils;

    }


    public Bitmap createBitmap() {
        mPaddingLeft = 0;
        mPaddingTop = 0;
        Bitmap bitmap = Bitmap.createBitmap(DEFAULT_WIDTH, DEFAULT_HEIGHT, Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        code = createCode();

        canvas.drawColor(Color.rgb(DEFAULT_COLOR, DEFAULT_COLOR, DEFAULT_COLOR));

        Paint paint = new Paint();

        paint.setTextSize(DEFAULT_FONT_SIZE);

        for (int i = 0; i < code.length(); i++) {
            randomTextStyle(paint);
            randomPadding();
            canvas.drawText(code.charAt(i) + "", mPaddingLeft, mPaddingTop, paint);

        }

        for (int i = 0; i < DEFAULT_LINE_NUMBER; i++) {

            drawLine(canvas, paint);

        }

//      这段代码,由于API28版本太高不能使用,所以需要降版本,否则报错
        canvas.save(Canvas.ALL_SAVE_FLAG);
        canvas.restore();
        return bitmap;

    }


    public String getCode() {

        return code;

    }


    public String createCode() {
        mBuilder.delete(0, mBuilder.length()); 
        for (int i = 0; i < DEFAULT_CODE_LENGTH; i++) {
            mBuilder.append(CHARS[mRandom.nextInt(CHARS.length)]);
        }
        return mBuilder.toString();
    }
    private void drawLine(Canvas canvas, Paint paint) {

        int color = randomColor();

        int startX = mRandom.nextInt(DEFAULT_WIDTH);

        int startY = mRandom.nextInt(DEFAULT_HEIGHT);

        int stopX = mRandom.nextInt(DEFAULT_WIDTH);


        int stopY = mRandom.nextInt(DEFAULT_HEIGHT);


        paint.setStrokeWidth(1);
        paint.setColor(color);

        canvas.drawLine(startX, startY, stopX, stopY, paint);

    }

    private int randomColor() {
        mBuilder.delete(0, mBuilder.length());

        String haxString;
        for (int i = 0; i < 3; i++) {
            haxString = Integer.toHexString(mRandom.nextInt(0xFF));

            if (haxString.length() == 1) {
                haxString = "0" + haxString;
           }

            mBuilder.append(haxString);
        }
        return Color.parseColor("#" + mBuilder.toString());
    }


    private void randomTextStyle(Paint paint) {

        int color = randomColor();

        paint.setColor(color);

        paint.setFakeBoldText(mRandom.nextBoolean());

        float skewX = mRandom.nextInt(11) / 10;

        skewX = mRandom.nextBoolean() ? skewX : -skewX;

        paint.setTextSkewX(skewX);
    }

    private void randomPadding() {
        mPaddingLeft += BASE_PADDING_LEFT + mRandom.nextInt(RANGE_PADDING_LEFT);

        mPaddingTop = BASE_PADDING_TOP + mRandom.nextInt(RANGE_PADDING_TOP);
    }
}

这是一个工具类,可以使用我的或者使用其他人的,网上一抓一大把,工具类的使用会有问题,我在做的时候是安卓8.0,由于API是28.会报类型错误。具体错误地点,已经标注出来了。这里一定要注意降级!降级!降级!

API降级方法:

非gradle:File->Project Structure ->选择你所选择的Modules

gradle:修改SdkVersion ,后面牵扯包的引用,具体详情看其他博主详细讲了,我们hai

 
  
 
  
附上我的build代码

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion '27.0.3'
    defaultConfig {
        applicationId "example.com.sunshine"
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"


    }


    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    repositories {
        jcenter()
    }
}


dependencies {
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:support-v4:25.3.1'
    compile 'com.android.support:support-v13:25.3.1'
    compile 'com.android.support:cardview-v7:25.3.1'
    compile 'com.android.support:appcompat-v7:25.3.1'
    implementation files('libs/okhttp-3.2.0.jar')
    implementation files('libs/okio-1.12.0.jar')
}
使用生成工具类的代码块:
 img_yzm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//            调用工具类
                codeUtils = CodeUtils.getInstance();
   //        生成bitmap
                Bitmap bitmap = codeUtils.createBitmap();
//            设置进imageView
                img_yzm.setImageBitmap(bitmap);
                String code = codeUtils.getCode().toLowerCase();
                System.out.println("code------------:" + code);
            }
        });

最好把code打印出来,毕竟有时候并不一定能看得清。测试使用。

最后贴出整个项目源码:

MainAcitvity:登陆首页

package com.example.myapplication;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {
    private ImageView img_yzm;
    private EditText ed_uname, ed_pwd, ed_yzm;
    private CodeUtils codeUtils;
    private Button btn_login, btn_cz;
    private String codeStr, uname, upwd;
    private String name, pwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       

        initView();
        initData();
    }

    private void initView() {
        img_yzm = (ImageView) findViewById(R.id.img_yzm);
        ed_uname = (EditText) findViewById(R.id.ed_uname);
        ed_pwd = (EditText) findViewById(R.id.ed_pwd);
        ed_yzm = (EditText) findViewById(R.id.ed_yzm);
        btn_login = (Button) findViewById(R.id.btn_login);
        btn_cz = (Button) findViewById(R.id.btn_cz);
    }

    private void initData() {
//      设置验证码,验证码项目需要SDK降级
        codeUtils = CodeUtils.getInstance();
        Bitmap bitmap = codeUtils.createBitmap();
        img_yzm.setImageBitmap(bitmap);
        img_yzm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                codeUtils = CodeUtils.getInstance();
                Bitmap bitmap = codeUtils.createBitmap();
                img_yzm.setImageBitmap(bitmap);
                String code = codeUtils.getCode().toLowerCase();
                System.out.println("code------------:" + code);
            }
        });


//        设置登录按钮
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                将获取的验证码进行小写判断
                codeStr = ed_yzm.getText().toString().trim().toLowerCase();
                uname = ed_uname.getText().toString().trim();
                upwd = ed_pwd.getText().toString().trim();
                String code = codeUtils.getCode().toLowerCase();
//                进行验证码判断
                if (null == codeStr || TextUtils.isEmpty(codeStr)||!code.equalsIgnoreCase(codeStr)) {
                    Toast.makeText(MainActivity.this, "请输入正确验证码", Toast.LENGTH_SHORT).show();
                }
//                将验证码变成小写字母

                System.out.println("code------------:" + code);
                if (code.equalsIgnoreCase(codeStr)) {
                    if (uname.equals(name) && upwd.equals(pwd)) {
                        Toast.makeText(MainActivity.this, "成功登陆", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent();
                        intent.putExtra("uname", uname);
                        intent.setClass(MainActivity.this, FirstActivity.class);
                        startActivity(intent);
                    } else {
                        Toast.makeText(MainActivity.this, "请输入正确的账号密码", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        btn_cz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ed_pwd.setText("");
                ed_uname.setText("");
                ed_yzm.setText("");
            }
        });


    }
}

xml文件




    

    
    
        

        
    
    
        
附:由于代码涉及到服务器验证登陆,所以会有一定的文件不足,敬请见谅。





你可能感兴趣的:(Android 登陆页面 图片验证码)