首页 驾驶证识别 驾驶证识别示例代码 驾驶证识别[Nodejs]

驾驶证识别示例代码Nodejs

作者: 阅读数:1136 上传时间:2025-04-01

驾驶证识别

// 1. 前置依赖:确保已安装 axios(执行 npm install axios)
const fs = require('fs');
const path = require('path');
const axios = require('axios');

// 2. 读取图片文件并转换为 Base64(不带前缀)
let base64Data;

try {
  // 修改为你的图片路径
  const imagePath = path.join(__dirname, 'driverlicense.png');
  const imageBuffer = fs.readFileSync(imagePath);
  base64Data = imageBuffer.toString('base64'); // 正确赋值
} catch (err) {
  console.error('图片处理失败:', err.message);
  process.exit(1); // 直接退出进程(避免后续使用未定义的变量)
}

// 3. 配置请求参数
const appkey = 'your_appkey_here'; // 替换为你的真实 appkey
const apiUrl = `https://api.jisuapi.com/driverlicenserecognition/recognize?appkey=${appkey}`;

// 4. 直接发送 POST 请求
axios.post(apiUrl, `pic=${encodeURIComponent(base64Data)}`, {
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded' // 必须指定表单类型
  }
})
.then(response => {
  // 处理成功响应
  console.log('----------------------------------');
  console.log('HTTP 状态码:', response.status);
  
  // 业务逻辑判断(根据 API 文档调整)
  if (response.data.status === 0) {
    console.log('识别成功:');
    console.log('驾驶证号:', response.data.result.licensenumber);
    console.log('姓名:', response.data.result.realname);
    console.log('性别:', response.data.result.sex);
    console.log('地址:', response.data.result.address);
    console.log('出生年月:', response.data.result.birth);
    console.log('初始领证日期:', response.data.result.initialdate);
    console.log('准驾车型:', response.data.result.type);
    console.log('有效起始日期:', response.data.result.startdate);
    console.log('有效截止日期:', response.data.result.enddate);
  } else {
    console.log('识别失败:', response.data.msg);
  }
})
.catch(error => {
  // 统一错误处理
  console.error('请求失败!');
});