官方文档:身份证识别 | 微信开放文档
实现效果
:
用的奥巴马的网络图片测试,图片
后端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);}});},