// 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, 'word.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/generalrecognition/recognize?appkey=${appkey}`; const type = 'cnen'; // 4. 直接发送 POST 请求 axios.post(apiUrl, `pic=${encodeURIComponent(base64Data)}&type=${encodeURIComponent(type)}`, { 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('识别内容:'); for (const [key, value] of Object.entries(response.data.result)) { console.log(value); } } else { console.log('识别失败:', response.data.msg); } }) .catch(error => { // 统一错误处理 console.error('请求失败!'); });
© 2015-2025 杭州极速互联科技有限公司 版权所有 浙ICP备17047587号-4 浙公网安备33010502005096 增值电信业务经营许可证:浙B2-20190875