1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | // 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, 'car.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/licenseplaterecognition/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.number); console.log( '颜色:' , response.data.result.color); console.log( '车牌类型:' , response.data.result.type); } else { console.log( '识别失败:' , response.data.msg); } }) . catch (error => { // 统一错误处理 console.error( '请求失败!' ); }); |
© 2015-2025 杭州极速互联科技有限公司 版权所有 浙ICP备17047587号-4 浙公网安备33010502005096 增值电信业务经营许可证:浙B2-20190875