Skip to content

Commit

Permalink
nodejs demo
Browse files Browse the repository at this point in the history
  • Loading branch information
Physton committed Oct 11, 2022
1 parent 83ebf35 commit d23d061
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
44 changes: 44 additions & 0 deletions Demo/nodejs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const axios = require('axios')
const md5 = require('md5')
const fs = require('fs')
const readline = require('readline');

let ApiKey = ''
let rl = readline.createInterface({input: process.stdin, output: process.stdout})
rl.question('请输入接口秘钥:', function (input) {
if (input !== '')
ApiKey = input

inputFile()
})

const inputFile = function () {
rl.question('\n请输入图片路径:', function (file) {
try {
fs.accessSync(file, fs.F_OK)
ocr(file).then(res => {
console.log(res.data.Data)
inputFile()
}).catch(err => {
console.log(err);
inputFile()
})
} catch (e) {
console.log('error: 文件不存在 ' + file);
inputFile()
}
})
}

const ocr = function (file) {
return new Promise((resolve, reject) => {
let bitmap = fs.readFileSync(file)
let Image = new Buffer.from(bitmap).toString('base64')
let Token = ApiKey !== '' ? md5(Image + ApiKey) : ''
axios.post('http://127.0.0.1:9292/api/ocr', {Image, Token}).then((res) => {
resolve(res)
}).catch(err => {
reject(err)
})
})
}
6 changes: 6 additions & 0 deletions Demo/nodejs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"dependencies": {
"axios": "^1.1.2",
"md5": "^2.3.0"
}
}

0 comments on commit d23d061

Please sign in to comment.