上一章使用CLion+OpenCV对身份证号码进行了检测,经过检测我们能拿到身份证号码区域的照片,现在我们对上一章拿到的图片中的数字进行识别
上一章链接:CLion+OpenCV 识别身份证号码---检测身份证号码_xxwbwm的博客-CSDN博客
环境:
Android Studio+OpenCV
下载openCV android 版本
拷贝图中文件到android 项目
库文件:libopencv_java3.so
头文件:opencv 和opencv2整个文件夹
新建android 项目将上一步的文件拷贝到如图位置:
配置CMakelist:
cmake_minimum_required(VERSION 3.10.2)
project("iddect")
#设置头文件查找路径
include_directories(include)
# -L 指定库的查找路径
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DANDROID_STL=c++_shared -L${CMAKE_SOURCE_DIR}/../jniLibs/${CMAKE_ANDROID_ARCH_ABI}")
add_library(
native-lib
SHARED
native-lib.cpp)
find_library(log-lib
log )
target_link_libraries(native-lib
${log-lib}
jnigraphics
opencv_java3)
识别OCR 使用google 开源的Tess-Two
build-gradle导入依赖:
implementation 'com.rmtheis:tess-two:7.0.0'
识别模型:
可以使用我源码中的文件可以自己训练,训练模式放在下一章讲解
流程:java把身份证原图给OpenCV(C++) --->Bitmap转Mat
OpenCV检测到身份证后需要把身份证号码图片返回给java做数字识别---->Mat转Bitmap
这里只讲解一下JNI函数中的重要步骤:
转化Mat--->OpenCV检测---->转Bitmap--->java识别
JNIEXPORT jobject JNICALL
Java_com_android_vivid_iddect_ImageProcess_getIdNumber(JNIEnv *env, jclass type, jobject src,
jobject config) {
Mat src_img;
Mat dst_img;
//imshow("src_", src_img);
//将bitmap转换为Mat型格式数据
Java_org_opencv_android_Utils_nBitmapToMat2(env, type, src, (jlong) &src_img, 0);
Mat dst;
//无损压缩//640*400
resize(src_img, src_img,FIX_IDCARD_SIZE);
//imshow("dst", src_img);
//灰度化
cvtColor(src_img, dst, COLOR_BGR2GRAY);
//imshow("gray", dst);
//二值化
threshold(dst, dst, 100, 255, CV_THRESH_BINARY);
//imshow("threshold", dst);
//膨胀+发酵
Mat erodeElement = getStructuringElement(MORPH_RECT, Size(20, 10));
erode(dst, dst, erodeElement);
vector< vector<Point> > contours;
vector<Rect> rects;
findContours(dst, contours, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
for (int i = 0; i < contours.size(); i++)
{
Rect rect = boundingRect(contours.at(i));
//rectangle(dst, rect, Scalar(0, 0, 255)); // 在dst 图片上显示 rect 矩形
if (rect.width > rect.height * 9) {
rects.push_back(rect);
rectangle(dst, rect, Scalar(0,255,255));
dst_img = src_img(rect);
}
}
if (rects.size() == 1) {
Rect rect = rects.at(0);
dst_img = src_img(rect);
}
else {
int lowPoint = 0;
Rect finalRect;
for (int i = 0; i < rects.size(); i++)
{
Rect rect = rects.at(i);
Point p = rect.tl();
if (rect.tl().y > lowPoint) {
lowPoint = rect.tl().y;
finalRect = rect;
}
}
rectangle(dst, finalRect, Scalar(255, 255, 0));
dst_img = src_img(finalRect);
}
jobject bitmap = createBitmap(env,dst_img,config);//将Mat格式的图片转化为bitmap(通过反射)
end:
src_img.release();
dst_img.release();
dst.release();
return bitmap;
}
最终效果:
https://download.csdn.net/download/xxwbwm/31420808
效果视屏资源是免费的
Android Studio源码: