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

1.JAVA版demo

public static void main(String[] args) throws Exception {
String serverUrl = "http://api.xinhuokj.com:40072/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\":\"11\", \"image\": \"" + imgStr + "\", \"token\":\"" + token + "\" } "; //把参数拼接成json字符串
String res = HttpUtil.postGeneralUrl(serverUrl, "application/json", params, "UTF-8"); //向服务端发送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' => 11,
'token'=>'b4de50360-------5cddddc3klf8d9em' //您的token,可在https://www.xinhuokj.com/user获取
);
$url = "http://api.xinhuokj.com:40072/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 <httplib.h>
#include <nlohmann/json.hpp>
#include <base64.hpp>
#include <utils.hpp>
#include <iostream>
#include <string>
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"] = "11";
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:40072/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\":\"11\", \"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:40072/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": 11}
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)
}
})