dota2竞猜首页

1.JAVA版demo

public static void main(String[] args) throws Exception {
String serverUrl = "http://112.30.110.198:28901/ocr"; //服务端地址
String token = "9b242b-----------4c2b4b6f01b"; //注册后生成的token (登录、注册的网址 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.postGeneralUrl(serverUrl, "application/json", params, "UTF-8"); //向服务端发送http请求,返回识别结果
System.out.println(res );
}
需要依赖的Java类源代码,点击下载: Base64Util.java、HttpUtil.java、FileUtil.java

2.PHP版demo

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

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("112.30.110.198", 28901);

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

json data;
data["token"] = "b4de50360------cddddcopkjytrks";     //您的token,可在http://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 (登录、注册的网址 www.xinhuokj.com/user)
string host = "http://112.30.110.198: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://112.30.110.198:28901/ocr"
token = "b4de50360------cddddcopkjytrks";//注册后生成的token (登录、注册的网址 www.xinhuokj.com/user)
# 二进制方式打开图片文件
f = open('test.jpg', 'rb')
img = base64.b64encode(f.read())
params = {"image":img, "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())