ocr开发demo源码-利来app苹果

薪火科技利来app苹果首页
提示:下载源码后需要改3个地方(1.接口地址;2.type;3.token),具体参照接口文档。

1.java版demo

public static void main(string[] args) throws exception {
string serverurl = "http://api.xinhuokj.com:28901/ocr"; //服务端地址
string token = "9b242b-----------4c2b4b6f01b"; //注册后生成的token (登录、注册的网址 https://www.xinhuokj.com/user)

byte[] imgdata = fileutil.readfilebybytes("test.jpg"); //读取待测试的图片
string imgstr = base64util.encode(imgdata); //把图片转成base64编码
string params = "{\"type\":\"0\", \"image\": \"" imgstr "\", \"token\":\"" token "\" } "; //把参数拼接成json字符串
string res = httputil.postgeneral; //向服务端发送http请求,返回识别结果
system.out.println(res );
}
java版demo代码,点击下载: xh_ocr_test.zip

2.php版demo

$base64 = filetobase64('img.jpg'); //读取图片的base64值
$post_data = array(
'image' => $base64,
'type' => 0,
'token'=>'b4de50360-------5cddddc3klf8d9em' //您的token,可在https://www.xinhuokj.com/user获取
);
$url = "http://api.xinhuokj.com:28901/ocr";
$post_str = json_encode($post_data);
$data = request_post($url, $post_str);
$data = strstr($data, '{');
echo $data;
}
php版demo代码,点击下载: ocr_demo_php.zip

3.c 版demo

#include
#include
#include
#include
#include
#include
using json = nlohmann::json;
using namespace xhai;

int main(int argc, char* argv[]) {
if(argc != 2) {
std::cout << "usage: demo ./img.jpg" << std::endl;
return -1;
}
httplib::client cli("api.xinhuokj.com", 28901);

std::string image_data;
get_file_content(argv[1], &image_data);

json data;
data["token"] = "b4de50360------cddddcopkjytrks";     //您的token,可在https://www.xinhuokj.com/user获取
data["image"] = base64_encode(image_data.c_str(), image_data.size());
data["type"] = "0";
std::string data_str = data.dump();

auto res = cli.post("/ocr", data_str.c_str(), "application/json");
if (res && res->status == 200) {

auto ret_data = json::parse(res->body);
std::cout << ret_data.dump() << std::endl;

int num = ret_data["words_result_num"];
std::cout << "word_num: " << num << std::endl;
auto words_result = ret_data["words_result"];
for(auto it: words_result) {
std::cout << it["words"] << std::endl;

}
}
}
可供演示运行的源代码,点击下载: ocr_demo_cpp.zip

4.c#版demo

using system;
using system.io;
using system.net;
using system.text;

namespace ocr_demo_.net
{
class program
{
static void main(string[] args)
{
string token = "b4de50360------cddddcopkjytrks";//注册后生成的token (登录、注册的网址https://www.xinhuokj.com/user)
string host = "http://api.xinhuokj.com:28901/ocr" ;
encoding encoding = encoding.default;
httpwebrequest request = (httpwebrequest)webrequest.create(host);
request.method = "post";
request.keepalive = true;
// 图片的base64编码
string base64 = filetobase64("demo.jpg");//待测试的图片
string str = "{\"type\":\"0\", \"image\": \"" base64 "\", \"token\":\"" token "\" } ";
byte[] buffer = encoding.getbytes(str);
request.contentlength = buffer.length;
request.getrequeststream().write(buffer, 0, buffer.length);
httpwebresponse response = (httpwebresponse)request.getresponse();
streamreader reader = new streamreader(response.getresponsestream(), encoding.default);
string result = reader.readtoend();
console.writeline("ocr 通用识别:");
console.writeline(result);

}

public static string filetobase64(string filename)
{
filestream filestream = new filestream(filename, filemode.open);
byte[] arr = new byte[filestream.length];
filestream.read(arr, 0, (int)filestream.length);
string baser64 = convert.tobase64string(arr);
filestream.close();
return baser64;
}

}
}

5.python版demo

# encoding:utf-8
import requests
import base64
request_url = "http://api.xinhuokj.com:28901/ocr"
token = "b4de50360------cddddcopkjytrks"#注册后生成的token (登录、注册的网址https://www.xinhuokj.com/user)
# 二进制方式打开图片文件
f = open('test.jpg', 'rb')
img = base64.b64encode(f.read())
params = { "image": str( img, encoding="utf-8" ), "token": token, "type": 0}
headers = { 'content-type': 'application/json' }
response = requests.post(request_url, data = json.dumps(params), headers = headers)
if response:
    print (response.json())
else:
    print (response)

可供演示运行的源代码,点击下载: demo.py.zip

6.nodejs版demo

const fs = require('fs')
// 先安装request,执行 npm install request 即可
const request = require('request')

const token = '8f127********************5b7' //请到个人中心获取token,https://www.xinhuokj.com/user
const url = 'http://api.xinhuokj.com:40072/ocr'

// 文件的base64编码数据
var image = fs.readfilesync('./test.jpg').tostring('base64')

request({
url: url,
method: 'post',
json: true,
headers: {
   'content-type': 'application/json',
},
body: {
token: token,
image: image,
type:11
}
}, function(error, response, body) {
if (!error ) {
   // 请求成功的处理逻辑
   console.log(body)
}
})