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

news2025/7/18 3:19:28

需要源码和图片集请点赞关注收藏后评论区留下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>

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/38731.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Oracle日志复制—国产自研Beedup(基于日志结构化数据复制产品)

Beedup能够实现大量交易数据的实时捕捉、变换和投递&#xff0c;实现源数据库与目标数据库的数据同步&#xff0c;保持亚秒级的数据延迟。 一、Beedup产品概述 由北京灵蜂纵横软件有限公司研发的数据库实时复制软件Beedup&#xff0c;提供数据库Oracle异地容灾备份&#xff0…

Vue笔记_01双向数据绑定原理

[1]什么叫双向数据绑定&#xff1f; 视图中的数据发生了变化&#xff0c;data中的数据也要对应改变&#xff1b;data中的数据发生了变化&#xff0c;视图上的数据也要对应改变&#xff1b; [2]双向绑定原理 vue 双向数据绑定是通过 数据劫持 结合 发布订阅模式的方式来实现的…

Pip版本问题导致Python模块安装失败

文章目录起因解决方案前言输入 %APPDATA%创建 pip.ini终端执行命令安装包结语起因 今天在视频号平台看到有小姐姐直播讲爬虫技术&#xff0c;我一看这不是挺简单的吗&#xff1f;于是我就想到自己四年前的今天刚进大一的时候最开始学的爬虫技术&#xff0c;后来因为一些事情跑…

怎么证明前端数据加密的三种方式

导读&#xff1a;前端最常见的三大加密方式https&#xff0c;SSH和MD5&#xff0c;这篇文章带你走进三大加密方式的原理对比。 1.https 1.1原理 A.就是在http加入SSL层&#xff0c;是http安全的基础; B.htts协议是在http基础上加了SSL协议; C.使用443端口&#xff0c;http是80…

2022CTF培训(三)windowslinux安卓平台调试机制原理

附件下载链接 windows平台调试机制原理 手动编写一个简易调试器 创建待调试进程 使用 CreateProcess 函数创建待调试进程&#xff0c;创建时指定 dwCreationFlags 参数为 DEBUG_ONLY_THIS_PROCESS 将会告诉操作系统我们需要让当前调用者&#xff08;线程&#xff09;接管所…

(十)centos7案例实战——实现nginx代理访问redis服务

前言 本节内容是关于实现nginx代理访问redis服务&#xff0c;由于在实际生产开发环境中&#xff0c;我们并不想将我们的中间键服务暴露在公网环境中&#xff0c;或者只能在内网环境中使用&#xff0c;例如本节内容&#xff0c;我们将redis安装到本地环境&#xff0c;但是又有需…

链表中倒数第k个结点、反转链表、合并两个排序的链表、树的子结构、删除链表中重复的结点

文章目录1、链表中倒数第k个结点2、反转链表3、合并两个排序的链表4、树的子结构5、 二叉树的镜像6、删除链表中重复的结点1、链表中倒数第k个结点 本题考点&#xff1a; 链表&#xff0c;前后指针的使用&#xff0c;边界条件检测 牛客链接 题目描述&#xff1a; 输入一个链表…

JVM【八股文】

JVM【八股文】 JVM内存区域划分 程序计数器栈堆方法区 一块大的区域&#xff0c;需要根据功能&#xff0c;来划分不同的小区域。 JVM内存是从操作系统里申请来的&#xff0c;之后堆这部分区域进行了划分。 1.程序计数器 内存中最小的区域&#xff0c;保存了下一条要执行指令…

android-加壳加固

title: android-加壳加固 categories: Android tags: [android, 加壳, 加固, 混淆] date: 2022-06-20 18:00:23 comments: false mathjax: true toc: true android-加壳 前篇 Android之Apk加壳 - https://blog.csdn.net/LVXIANGAN/article/details/84956476Android动态加载Dex…

李沐论文精度系列之七:Two-Stream双流网络、I3D

文章目录一、双流网络1.1 前言1.2 网络结构1.3 光流(Optical flow)1.3.1 什么是光流1.3.2 如何利用光流1.3.3 双向光流&#xff08;Bi-directional optical flow&#xff09;1.3.4 光流的局限性及和对应的预处理&#xff08;抽取&#xff09;方式1.3.5 视频模型测试1.4 实验1.4…

✿✿✿JavaScript基本语法一

目 录 1.js的发展史&#xff08;闲聊版&#xff09; 2.浏览器分成两部分&#xff1a;渲染引擎和 JS 引擎 3.js与html的关系以及结合方式 (1)js与html的关系 (2)js与html结合方式 4.JavaScript注释 5.js中的基本数据类型 6.js中的变量 7.运算符&#xff08;自动类型转…

9.前端笔记-CSS-盒子模型-border和padding

页面布局的三大核心&#xff1a; 盒子模型浮动定位 1、盒子模型 1.1 盒子模型组成 盒子模型本质还是一个盒子&#xff0c;包括边框border、外边距margin、内边距padding和实际内容content 1.1.1 边框border 组成 组成&#xff1a;颜色border-color、边框宽度border-wid…

518. 零钱兑换 II【完全背包:求组合数】

518. 零钱兑换 II 给你一个整数数组 coins 表示不同面额的硬币&#xff0c;另给一个整数 amount 表示总金额。 请你计算并返回可以凑成总金额的硬币组合数。如果任何硬币组合都无法凑出总金额&#xff0c;返回 0 。 假设每一种面额的硬币有无限个。 题目数据保证结果符合 32 位…

C++11 右值,右值引用,移动构造,移动赋值

目录 一、左值&#xff0c;左值引用&#xff0c;右值&#xff0c;右值引用的相关概念&#xff1a; 1. 什么是左值&#xff0c;什么是左值引用&#xff1f; 2. 什么是右值&#xff0c;什么是右值引用&#xff1f; 3. 右值的属性是右值&#xff0c;右值引用的属性是左值 4. …

棒子老虎鸡-第12届蓝桥杯Scratch选拔赛真题精选

[导读]&#xff1a;超平老师计划推出Scratch蓝桥杯真题解析100讲&#xff0c;这是超平老师解读Scratch蓝桥真题系列的第86讲。 蓝桥杯选拔赛每一届都要举行4~5次&#xff0c;和省赛、国赛相比&#xff0c;题目要简单不少&#xff0c;再加上篇幅有限&#xff0c;因此我精挑细选…

研究生有限元仿真应用中存在的问题与对策

作者&#xff1a;尚晓江 导读&#xff1a;有限元分析软件作为计算工具&#xff0c;在科研和工程领域都有广泛应用&#xff0c;而多数用户是在研究生阶段开始接触和使用这些计算软件的。本文以ANSYS结构分析为例&#xff0c;对现阶段研究生应用有限元分析软件的现状和存在的问题…

无人机设计仿真--在Isight平台上进行的基于CST参数化+Xfoil的无人机翼型优化

作者&#xff1a;Graychen 一、工程背景 翼型的选型和设计是飞行器气动设计中的一项基础性工作&#xff0c;翼型对飞行器的气动性能具有根本性的影响。现在高性能飞行器已不再从翼型库中选择适用翼型后直接使用&#xff0c;而是以现有翼型作为基准翼型进行气动优化&#xff…

java基本语法 下

目录 运算符 运算符&#xff1a;算术运算符 运算符&#xff1a;赋值运算符 运算符&#xff1a;比较运算符 运算符&#xff1a;逻辑运算符 运算符&#xff1a;三元运算符 运算符的优先级 程序流程控制 概念 顺序结构 if-else结构 switch-case结构 循环结构 循环结构…

Unity视差贴图多实现对比和改进

视差贴图多种实现方式对比和改进视差贴图视差映射陡峭视差映射视差遮蔽映射迭代视差映射-kerry视差贴图 参考 与法线贴图相同&#xff0c;可以模拟出物体得深度感&#xff0c;同时它得改进是能够随着视角得偏移显示不同得深度感&#xff0c;使得显示更加真实。 由于采样高度…

代码随想录刷题| 多重背包理论基础、背包问题的总结

目录 多重背包理论基础 多重背包的问题 多重背包的解法 多重背包的代码 背包问题的总结 01背包 完全背包 多重背包 多重背包理论基础 多重背包的问题 有N种物品和一个容量为V 的背包。第i种物品最多有Mi件可用&#xff0c;每件耗费的空间是Ci &#xff0c;价值是Wi 。…