Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OcrKing for Node #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Node/56623.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 45 additions & 0 deletions Node/OcrKing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const xml2json = require('node-xml2json');
const request = require('request');
const fs = require('fs');
const url = require('url');

function getCaptcha(filePath, option) {
const defaultOption = {
service: 'OcrKingForPhoneNumber',
language: 'eng',
charset: '11',
type: 'http://www.unknown.com',
};
const result = url.format({
protocol: 'http:',
hostname: 'lab.ocrking.com/ok.html',
query: Object.assign({}, defaultOption, option),
});

return new Promise((resolve, reject) => {
const formData = {
file: fs.createReadStream(filePath),
};

request.post({
url: result,
formData,
}, (err, httpResponse, body) => {
if (err) {
reject({
error: err,
});
}
try {
bodyObj = xml2json.parser(body).results.resultlist || {};
resolve(bodyObj);
} catch (e) {
reject({
error: e,
});
}
});
});
}

module.exports = getCaptcha;
21 changes: 21 additions & 0 deletions Node/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# OcrKing Node

## 安装依赖

```js

npm install

```

## Example

```shell

node example.js

```

## option

option 详见 [接口说明](../线上识别http接口说明.txt)
13 changes: 13 additions & 0 deletions Node/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const getCaptcha = require('./OcrKing');
const path = require('path');


const filePath = path.resolve('./56623.jpg');

getCaptcha(filePath, {
apiKey: '51ef8dc7b3b0b5be53qUWQPIpynwaRCZb2MxvzUXqlsPQYPtbp0CG0Df55dh9vp5BScpESjJcH',
}).then((result) => {
console.log(result);
}).catch((err) => {
console.log(err);
});
28 changes: 28 additions & 0 deletions Node/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "ocrking",
"version": "1.0.0",
"description": "OcrKing for Node",
"main": "OcrKing.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/smackgg/OcrKing.git"
},
"keywords": [
"OcrKing",
"Node",
"captha"
],
"author": "smackgg",
"license": "ISC",
"bugs": {
"url": "https://github.com/smackgg/OcrKing/issues"
},
"homepage": "https://github.com/smackgg/OcrKing#readme",
"dependencies": {
"node-xml2json": "^1.0.0",
"request": "^2.81.0"
}
}