欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 社会 > JAVA+微信小程序前后端源码 微信OCR识别 识别身份证信息

JAVA+微信小程序前后端源码 微信OCR识别 识别身份证信息

2024/11/6 17:13:07 来源:https://blog.csdn.net/guochanof/article/details/143570642  浏览:    关键词:JAVA+微信小程序前后端源码 微信OCR识别 识别身份证信息

官方文档:身份证识别 | 微信开放文档

实现效果

:

用的奥巴马的网络图片测试,图片

后端JAVA代码

这里用的若依的后端,前后端分离版的

package com.ruoyi.common.utils;import java.io.File;
import java.io.IOException;import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;public class WeixinImgOcrUtils {private static final String BASE_URL = "https://api.weixin.qq.com/cv/ocr/idcard";public static void main(String[] args) {// 替换为你的 access_tokenString accessToken = "86_vAioMfr3QYrqoHTYWyrmEjsVqqL6IWYmbQgDCad1oa6CAoqDbDX4mK2ZOS8pruJyUguKjeHhRYVVoEp2zMBU9MPsAOnV48_99k8IhuXsbJ5v8HIQMtYPnmforDIKWUbAGADPN";// 替换为你的图片路径String imgPath = "D:/1.jpg";try (CloseableHttpClient httpClient = HttpClients.createDefault()) {HttpPost httpPost = new HttpPost(BASE_URL);MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();entityBuilder.addBinaryBody("img", newFile(imgPath), ContentType.APPLICATION_OCTET_STREAM, "img.jpg");entityBuilder.addTextBody("access_token", accessToken);HttpEntity entity = entityBuilder.build();httpPost.setEntity(entity);try (CloseableHttpResponse response = httpClient.execute(httpPost)) {int statusCode = response.getStatusLine().getStatusCode();if (statusCode!= 200) {System.out.println("请求失败,状态码: " + statusCode);return;}HttpEntity responseEntity = response.getEntity();String responseBody = EntityUtils.toString(responseEntity);System.out.println("响应结果: " + responseBody);}} catch (IOException e) {e.printStackTrace();}}
}

运行结果

特别注意:

这个接口有免费的,有收费的,免费的限制每天100次,超出需要购买.

即便是免费的,也要0元购买,不然会调用失败,

返回101003   not enough market quota的失败信息

购买地址:微信服务市场

前端小程序代码:

html

        <view class="cu-form-group"><view class="title"><span class="text-red margin-right-sm">*</span>身份证号</view><input placeholder="请输入身份证号" name="idCard" value="{{idCard}}" disabled="true" ></input><button style="width: 120rpx;" class="cu-btn round line-blue" bindtap='idcard'>识别</button></view>

js

这里请求的后端是若依分离版自带的文件上传

    idcard() {wx.chooseImage({count: 1, sizeType: ['original', 'compressed'], sourceType: ['album'], success: (res) => {wx.showLoading({title: '识别中...',})wx.uploadFile({url: config.api_base_url + '/common/upload',header: {'content-type': 'application/json','Authorization': 'Bearer ' + wx.getStorageSync('token')},filePath: res.tempFilePaths[0],name: 'file',success: (resp) => {var jsonObj = JSON.parse(resp.data);let imgurl = jsonObj.fileNameinfoModel.getOCR(imgurl, data => {let obj = JSON.parse(data.msg);if(obj.errmsg == "ok"){    wx.showToast({title: '识别成功',icon: 'success',duration: 2000, });this.editUserIdCarc(obj.id)this.setData({idCard: obj.id})}else{wx.showToast({title: '识别失败,请重试!',icon: 'error',duration: 2000})}wx.hideLoading();})},fail: (res) => {console.log(res);reject(res); },});},ail: chooseError => {console.error('选择照片失败:', chooseError);}});},

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com