Android 开发人脸识别之自动识别验证码功能讲解及实现(超详细 附源码)

简介: Android 开发人脸识别之自动识别验证码功能讲解及实现(超详细 附源码)

需要源码和图片集请点赞关注收藏后评论区留下QQ或者私信~~~

一、自动识别验证码

验证码图片中最简单的是数字验证码,一张再普通不过的验证码拿到之后要进行以下步骤的处理

1:首先对图片适当裁剪,先去掉外部的空白区域,再把每个数字所处的区域单独抠出来

2:对每个数字方块做切割 一般按照九宫格切为九块

一般情况下 图片中的数字颜色较深,其他区域颜色较浅,通过判断每个方格上的像素点颜色深浅就能得知该方格是否有线条经过,获取像素点的颜色深浅主要有以下几个步骤

1:调用bitmap对象的getPixel 获得指定x y坐标像素点的颜色对象

2:调用Integet类的toHexString方法 把颜色对象转换为字符串对象

3:第二步得到一个长度为8的字符串,其中前两位表示透明度,第三四位表示红色浓度,第五六位表示绿色浓度,第七八位表示蓝色浓度,另外,需要把十六进制的颜色浓度值转换位十进制的浓度值

接下来从外部掉用getNumber方法,即可从验证码位图识别出验证码数字,这个验证码图片识别出验证码数字,这个验证码图片可能来自本地也可能来自网络,

运行App后结果如下 什么都不加时识别正确率较高,当加了干扰线和字母时识别率将会大大降低,因为算法目前不支持字母的识别

加了干扰之后准确率直线下滑

通过上述图例也可以发现防止验证码被破解至少包含两个手段 一是扩大干扰力度,二是增加字符种类

代码如下

Java类

package com.example.face;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import com.example.face.util.CodeAnalyzer;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
@SuppressLint({"DefaultLocale", "SetTextI18n"})
public class VerifyCodeActivity extends AppCompatActivity {
    private final static String TAG = "VerifyCodeActivity";
    private final static String mCodeUrl = "http://192.168.1.5:8080/HttpServer/generateCode?char_type=%d&disturber_type=%d";
    private CheckBox ck_source; // 声明一个复选框对象
    private LinearLayout ll_local; // 声明一个线性视图对象
    private LinearLayout ll_network; // 声明一个线性视图对象
    private ImageView iv_code; // 声明一个图像视图对象
    private TextView tv_code; // 声明一个文本视图对象
    private boolean isGetting = false; // 是否正在获取验证码
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_verify_code);
        ck_source = findViewById(R.id.ck_source);
        ll_local = findViewById(R.id.ll_local);
        ll_network = findViewById(R.id.ll_network);
        iv_code = findViewById(R.id.iv_code);
        iv_code.setOnClickListener(v -> getImageCode(mCharType, mDisturberType));
        tv_code = findViewById(R.id.tv_code);
        ck_source.setOnCheckedChangeListener((buttonView, isChecked) -> {
            ll_local.setVisibility(isChecked ? View.GONE : View.VISIBLE);
            ll_network.setVisibility(isChecked ? View.VISIBLE : View.GONE);
        });
        initCodeSpinner(); // 初始化验证码图片下拉框
        initCharSpinner(); // 初始化字符类型下拉框
        initDisturbSpinner(); // 初始化干扰类型下拉框
    }
    // 初始化验证码图片下拉框
    private void initCodeSpinner() {
        ArrayAdapter<String> codeAdapter = new ArrayAdapter<>(this,
                R.layout.item_select, codeDescArray);
        Spinner sp_code = findViewById(R.id.sp_code);
        sp_code.setPrompt("请选择验证码图片");
        sp_code.setAdapter(codeAdapter);
        sp_code.setOnItemSelectedListener(new CodeSelectedListener());
        sp_code.setSelection(0);
    }
    private String[] codeDescArray={"第一张验证码", "第二张验证码", "第三张验证码", "第四张验证码", "第五张验证码"};
    private int[] codeResArray={R.drawable.code1, R.drawable.code2, R.drawable.code3, R.drawable.code4, R.drawable.code5 };
    class CodeSelectedListener implements AdapterView.OnItemSelectedListener {
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), codeResArray[arg2]);
            showVerifyCode(bitmap); // 识别并显示验证码数字
        }
        public void onNothingSelected(AdapterView<?> arg0) {
        }
    }
    // 初始化字符类型下拉框
    private void initCharSpinner() {
        ArrayAdapter<String> charAdapter = new ArrayAdapter<>(this,
                R.layout.item_select, charDescArray);
        Spinner sp_char = findViewById(R.id.sp_char);
        sp_char.setPrompt("请选择字符类型");
        sp_char.setAdapter(charAdapter);
        sp_char.setOnItemSelectedListener(new CharSelectedListener());
        sp_char.setSelection(0);
    }
    private int mCharType = 0; // 字符类型
    private String[] charDescArray={"纯数字", "字母加数字"};
    class CharSelectedListener implements AdapterView.OnItemSelectedListener {
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            mCharType = arg2;
            getImageCode(mCharType, mDisturberType); // 从服务器获取验证码图片
        }
        public void onNothingSelected(AdapterView<?> arg0) {
        }
    }
    // 初始化干扰类型下拉框
    private void initDisturbSpinner() {
        ArrayAdapter<String> disturbAdapter = new ArrayAdapter<>(this,
                R.layout.item_select, disturbDescArray);
        Spinner sp_disturb = findViewById(R.id.sp_disturb);
        sp_disturb.setPrompt("请选择干扰类型");
        sp_disturb.setAdapter(disturbAdapter);
        sp_disturb.setOnItemSelectedListener(new DisturbSelectedListener());
        sp_disturb.setSelection(0);
    }
    private int mDisturberType = 0; // 干扰类型
    private String[] disturbDescArray={"干扰点", "干扰线"};
    class DisturbSelectedListener implements AdapterView.OnItemSelectedListener {
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            mDisturberType = arg2;
            getImageCode(mCharType, mDisturberType); // 从服务器获取验证码图片
        }
        public void onNothingSelected(AdapterView<?> arg0) {
        }
    }
    // 从服务器获取验证码图片
    private void getImageCode(int char_type, int disturber_type) {
        if (!ck_source.isChecked() || isGetting) {
            return;
        }
        isGetting = true;
        String imageUrl = String.format(mCodeUrl, char_type, disturber_type);
        OkHttpClient client = new OkHttpClient(); // 创建一个okhttp客户端对象
        // 创建一个GET方式的请求结构
        Request request = new Request.Builder().url(imageUrl).build();
        Call call = client.newCall(request); // 根据请求结构创建调用对象
        // 加入HTTP请求队列。异步调用,并设置接口应答的回调方法
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) { // 请求失败
                isGetting = false;
                // 回到主线程操纵界面
                runOnUiThread(() -> tv_code.setText("下载网络图片报错:"+e.getMessage()));
            }
            @Override
            public void onResponse(Call call, final Response response) { // 请求成功
                InputStream is = response.body().byteStream();
                // 从返回的输入流中解码获得位图数据
                Bitmap bitmap = BitmapFactory.decodeStream(is);
                isGetting = false;
                // 回到主线程操纵界面
                runOnUiThread(() -> showVerifyCode(bitmap));
            }
        });
    }
    // 识别并显示验证码数字
    private void showVerifyCode(Bitmap bitmap) {
        String number = CodeAnalyzer.getNumber(bitmap); // 从验证码位图获取验证码数字
        iv_code.setImageBitmap(bitmap);
        tv_code.setText("自动识别得到的验证码是:"+number);
//        List<Bitmap> bitmapList = CodeAnalyzer.splitImage(bitmap);
//        ImageView iv1 = findViewById(R.id.iv1);
//        ImageView iv2 = findViewById(R.id.iv2);
//        ImageView iv3 = findViewById(R.id.iv3);
//        ImageView iv4 = findViewById(R.id.iv4);
//        iv1.setImageBitmap(bitmapList.get(0));
//        iv2.setImageBitmap(bitmapList.get(1));
//        iv3.setImageBitmap(bitmapList.get(2));
//        iv4.setImageBitmap(bitmapList.get(3));
    }
}

XML文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <CheckBox
        android:id="@+id/ck_source"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="验证码是否来自服务器"
        android:textColor="#000000"
        android:textSize="17sp" />
    <LinearLayout
        android:id="@+id/ll_local"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:paddingLeft="5dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="请选择验证码图片 "
            android:textColor="#000000"
            android:textSize="17sp" />
        <Spinner
            android:id="@+id/sp_code"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:spinnerMode="dialog" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/ll_network"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:visibility="gone">
        <Spinner
            android:id="@+id/sp_char"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:spinnerMode="dialog" />
        <Spinner
            android:id="@+id/sp_disturb"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:spinnerMode="dialog" />
    </LinearLayout>
    <ImageView
        android:id="@+id/iv_code"
        android:layout_width="match_parent"
        android:layout_height="100dp" />
    <TextView
        android:id="@+id/tv_code"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:textColor="#000000"
        android:textSize="17sp" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_marginTop="20dp">
        <ImageView
            android:id="@+id/iv1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />
        <ImageView
            android:id="@+id/iv2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />
        <ImageView
            android:id="@+id/iv3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />
        <ImageView
            android:id="@+id/iv4"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    </LinearLayout>
</LinearLayout>

创作不易 觉得有帮助请点赞关注收藏~~~

相关文章
|
8天前
|
JSON 编译器 开发工具
VS Code阅读Android源码
VS Code阅读Android源码
12 1
|
7天前
|
Java Android开发
Android系统 修改无源码普通应用为默认Launcher和隐藏Settings中应用信息图标
Android系统 修改无源码普通应用为默认Launcher和隐藏Settings中应用信息图标
22 0
|
7天前
|
Linux 开发工具 Android开发
Docker系列(1)安装Linux系统编译Android源码
Docker系列(1)安装Linux系统编译Android源码
14 0
|
7天前
|
安全 Java Shell
Android13 adb input 调试命令使用和源码解析
Android13 adb input 调试命令使用和源码解析
12 0
|
8天前
|
Java Android开发
Android Mediatek 应用层重置USB设备功能
Android Mediatek 应用层重置USB设备功能
11 0
|
8天前
|
Linux Android开发
Android 内核关闭CAN 串口设备回显功能
Android 内核关闭CAN 串口设备回显功能
8 0
|
14天前
|
数据采集 小程序 数据可视化
Java Android原生智慧校园管理系统源码
对班牌的考试模式、班牌模式上课模式进行设置及管理,设置成功后,班牌端将同步应用。
21 0
|
15天前
|
传感器 小程序 Java
Java+saas模式 智慧校园系统源码Java Android +MySQL+ IDEA 多校运营数字化校园云平台源码
Java+saas模式 智慧校园系统源码Java Android +MySQL+ IDEA 多校运营数字化校园云平台源码 智慧校园即智慧化的校园,也指按智慧化标准进行的校园建设,按标准《智慧校园总体框架》中对智慧校园的标准定义是:物理空间和信息空间的有机衔接,使任何人、任何时间、任何地点都能便捷的获取资源和服务。
16 1
|
7天前
|
消息中间件 网络协议 Java
Android 开发中实现数据传递:广播和Handler
Android 开发中实现数据传递:广播和Handler
12 1
|
9天前
|
Linux 编译器 Android开发
FFmpeg开发笔记(九)Linux交叉编译Android的x265库
在Linux环境下,本文指导如何交叉编译x265的so库以适应Android。首先,需安装cmake和下载android-ndk-r21e。接着,下载x265源码,修改crosscompile.cmake的编译器设置。配置x265源码,使用指定的NDK路径,并在配置界面修改相关选项。随后,修改编译规则,编译并安装x265,调整pc描述文件并更新PKG_CONFIG_PATH。最后,修改FFmpeg配置脚本启用x265支持,编译安装FFmpeg,将生成的so文件导入Android工程,调整gradle配置以确保顺利运行。
32 1
FFmpeg开发笔记(九)Linux交叉编译Android的x265库