|
|
|
|
|
#include <fstream> |
|
|
#include <iostream> |
|
|
#include <thread> |
|
|
#include <chrono> |
|
|
#include <cstring> |
|
|
#include <filesystem> |
|
|
|
|
|
#include "OcrManager.h" |
|
|
#include "Constant.h" |
|
|
#include "protobuf/ocr_protobuf.pb.h" |
|
|
|
|
|
|
|
|
OcrManager::OcrManager(const std::string& path) : isWeChatOcrRunning(false), isConnected(false) { |
|
|
|
|
|
for (int i = 0; i < OcrMaxTaskId; i++) { |
|
|
queueIds.push(i); |
|
|
timers[i] = std::chrono::high_resolution_clock::now(); |
|
|
} |
|
|
|
|
|
if (path.empty()) { |
|
|
|
|
|
SetExePath(Constant::WeChatOcrData); |
|
|
SetUsrLibDir(Constant::WeChatOcrData); |
|
|
} |
|
|
else { |
|
|
|
|
|
std::string ocrExePath = ""; |
|
|
std::string wechatDir = ""; |
|
|
|
|
|
|
|
|
|
|
|
ocrExePath = path + "\\WeChatOCR.exe"; |
|
|
wechatDir = path; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SetExePath(ocrExePath); |
|
|
SetUsrLibDir(wechatDir); |
|
|
} |
|
|
} |
|
|
|
|
|
OcrManager::~OcrManager() { |
|
|
if (isWeChatOcrRunning) { |
|
|
KillWeChatOcr(); |
|
|
} |
|
|
} |
|
|
|
|
|
void OcrManager::SetUsrLibDir(const std::string& usrLibDir) { |
|
|
AppendSwitchNativeCmdLine("user-lib-dir", usrLibDir); |
|
|
} |
|
|
|
|
|
void OcrManager::SetOcrResultCallback(std::function<void(const std::string&, std::string&)> func) { |
|
|
Callback = func; |
|
|
} |
|
|
|
|
|
void OcrManager::StartWeChatOcr(void* ocrManager) { |
|
|
SetCallbackUsrData(ocrManager); |
|
|
SetDefaultCallbacks(); |
|
|
InitMmMojoEnv(); |
|
|
isWeChatOcrRunning = true; |
|
|
} |
|
|
|
|
|
void OcrManager::KillWeChatOcr() { |
|
|
isConnected = false; |
|
|
isWeChatOcrRunning = false; |
|
|
StopMmMojoEnv(); |
|
|
} |
|
|
|
|
|
void OcrManager::DoOcrTask(const std::string& imgPath) { |
|
|
if (!isWeChatOcrRunning) { |
|
|
throw std::runtime_error("请先调用StartWeChatOCR启动"); |
|
|
} |
|
|
|
|
|
|
|
|
std::ifstream file(imgPath); |
|
|
if (!file.good()) { |
|
|
throw std::runtime_error("给定图片路径不存在: " + imgPath); |
|
|
} |
|
|
file.close(); |
|
|
|
|
|
|
|
|
std::string fullPath = std::filesystem::absolute(imgPath).string(); |
|
|
|
|
|
|
|
|
int numTry = 0; |
|
|
while (!isConnected) { |
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
|
|
if (numTry++ > 3) { |
|
|
throw std::runtime_error("连接Ocr服务超时!"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
int taskId = GetIdleTaskId(); |
|
|
if (taskId < 0) { |
|
|
throw std::overflow_error("当前队列已满,请等待后重试!"); |
|
|
} |
|
|
|
|
|
|
|
|
SendOcrTask(taskId, fullPath); |
|
|
} |
|
|
|
|
|
void OcrManager::SendOcrTask(int taskId, const std::string& picPath) { |
|
|
dicImageID[taskId] = picPath; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ocr_protobuf::OcrRequest ocrRequest; |
|
|
ocrRequest.set_unknow(0); |
|
|
ocrRequest.set_task_id(taskId); |
|
|
auto* picPaths = ocrRequest.mutable_pic_path(); |
|
|
picPaths->add_pic_path(picPath); |
|
|
|
|
|
|
|
|
std::string serializedData; |
|
|
ocrRequest.SerializeToString(&serializedData); |
|
|
|
|
|
|
|
|
SendPbSerializedData((const unsigned char*)serializedData.c_str(), serializedData.length(), |
|
|
(int)MMMojoInfoMethod::kMMPush, 0, (int)RequestIdOcr::OcrPush); |
|
|
} |
|
|
|
|
|
WeChatOcrResult* OcrManager::ParseJsonResponse(const std::string& jsonResponseStr) { |
|
|
return ParseOcrResult::ParseJson(jsonResponseStr); |
|
|
} |
|
|
|
|
|
void OcrManager::SetConnectState(bool connect) { |
|
|
isConnected = connect; |
|
|
} |
|
|
|
|
|
int OcrManager::GetIdleTaskId() { |
|
|
int taskId = -1; |
|
|
try { |
|
|
if (!queueIds.empty()) { |
|
|
taskId = queueIds.front(); |
|
|
queueIds.pop(); |
|
|
timers[taskId] = std::chrono::high_resolution_clock::now(); |
|
|
} |
|
|
} |
|
|
catch (const std::exception&) { |
|
|
|
|
|
} |
|
|
return taskId; |
|
|
} |
|
|
|
|
|
void OcrManager::SetTaskIdIdle(int taskId) { |
|
|
queueIds.push(taskId); |
|
|
|
|
|
|
|
|
auto now = std::chrono::high_resolution_clock::now(); |
|
|
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(now - timers[taskId]).count(); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
void OcrManager::SetDefaultCallbacks() { |
|
|
SetOneCallback("kMMRemoteConnect", (void*)&OcrManager::OCRRemoteOnConnect); |
|
|
SetOneCallback("kMMRemoteDisconnect", (void*)&OcrManager::OCRRemoteOnDisconnect); |
|
|
SetOneCallback("kMMReadPush", (void*)&OcrManager::OCRReadOnPush); |
|
|
} |
|
|
|
|
|
void OcrManager::OCRRemoteOnConnect(bool isConnected, void* userData) { |
|
|
if (userData == nullptr) return; |
|
|
|
|
|
OcrManager* manager = static_cast<OcrManager*>(userData); |
|
|
manager->SetConnectState(true); |
|
|
} |
|
|
|
|
|
void OcrManager::OCRRemoteOnDisconnect(void* userData) { |
|
|
if (userData != nullptr) { |
|
|
OcrManager* manager = static_cast<OcrManager*>(userData); |
|
|
manager->SetConnectState(false); |
|
|
} |
|
|
} |
|
|
|
|
|
void OcrManager::OCRReadOnPush(unsigned int requestId, void* requestInfo, void* userData) { |
|
|
if (userData == nullptr) return; |
|
|
|
|
|
OcrManager* manager = static_cast<OcrManager*>(userData); |
|
|
|
|
|
|
|
|
unsigned int pbSize = 0; |
|
|
void* pbData = manager->GetPbSerializedData(requestInfo, &pbSize); |
|
|
if (pbSize <= 20) return; |
|
|
|
|
|
|
|
|
manager->CallUserCallback(requestId, pbData, (int)pbSize); |
|
|
|
|
|
|
|
|
manager->RemoveReadInfo(requestInfo); |
|
|
} |
|
|
|
|
|
void OcrManager::CallUserCallback(unsigned int requestId, void* serializedData, int dataSize) { |
|
|
std::cout << "回调函数 CallUserCallback 被调用,request_id: " << requestId << std::endl; |
|
|
|
|
|
|
|
|
unsigned char* ocrResponseArray = new unsigned char[dataSize]; |
|
|
memcpy(ocrResponseArray, serializedData, dataSize); |
|
|
|
|
|
|
|
|
ocr_protobuf::OcrResponse ocrResponse; |
|
|
ocrResponse.ParseFromArray(ocrResponseArray, dataSize); |
|
|
|
|
|
|
|
|
delete[] ocrResponseArray; |
|
|
|
|
|
|
|
|
int taskId = ocrResponse.task_id(); |
|
|
|
|
|
|
|
|
auto it = dicImageID.find(taskId); |
|
|
if (it == dicImageID.end()) return; |
|
|
|
|
|
std::string pth_img = it->second; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
WeChatOcrResult* result = new WeChatOcrResult(); |
|
|
result->TaskId = ocrResponse.task_id(); |
|
|
result->OcrResult = std::make_shared<OcrResult>(); |
|
|
|
|
|
|
|
|
if (ocrResponse.has_ocr_result()) { |
|
|
const auto& protoOcrResult = ocrResponse.ocr_result(); |
|
|
|
|
|
|
|
|
for (int i = 0; i < protoOcrResult.single_result_size(); i++) { |
|
|
const auto& protoSingleResult = protoOcrResult.single_result(i); |
|
|
SingleResult singleResult; |
|
|
|
|
|
|
|
|
if (!protoSingleResult.single_str_utf8().empty()) { |
|
|
singleResult.SingleStrUtf8 = protoSingleResult.single_str_utf8(); |
|
|
} |
|
|
|
|
|
|
|
|
singleResult.SingleRate = protoSingleResult.single_rate(); |
|
|
singleResult.Left = protoSingleResult.left(); |
|
|
singleResult.Top = protoSingleResult.top(); |
|
|
singleResult.Right = protoSingleResult.right(); |
|
|
singleResult.Bottom = protoSingleResult.bottom(); |
|
|
|
|
|
|
|
|
if (protoSingleResult.has_single_pos()) { |
|
|
singleResult.SinglePos = std::make_shared<SinglePos>(); |
|
|
const auto& protoSinglePos = protoSingleResult.single_pos(); |
|
|
|
|
|
for (int j = 0; j < protoSinglePos.pos_size(); j++) { |
|
|
const auto& protoPosXY = protoSinglePos.pos(j); |
|
|
Pos pos; |
|
|
pos.X = protoPosXY.x(); |
|
|
pos.Y = protoPosXY.y(); |
|
|
singleResult.SinglePos->Pos.push_back(pos); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
for (int j = 0; j < protoSingleResult.one_result_size(); j++) { |
|
|
const auto& protoOneResult = protoSingleResult.one_result(j); |
|
|
OneResult oneResult; |
|
|
|
|
|
|
|
|
if (!protoOneResult.one_str_utf8().empty()) { |
|
|
oneResult.OneStrUtf8 = protoOneResult.one_str_utf8(); |
|
|
} |
|
|
|
|
|
|
|
|
if (protoOneResult.has_one_pos()) { |
|
|
oneResult.OnePos = std::make_shared<OnePos>(); |
|
|
const auto& protoOnePos = protoOneResult.one_pos(); |
|
|
|
|
|
for (int k = 0; k < protoOnePos.pos_size(); k++) { |
|
|
const auto& protoPosXY = protoOnePos.pos(k); |
|
|
Pos pos; |
|
|
pos.X = protoPosXY.x(); |
|
|
pos.Y = protoPosXY.y(); |
|
|
oneResult.OnePos->Pos.push_back(pos); |
|
|
} |
|
|
} |
|
|
|
|
|
singleResult.OneResult.push_back(oneResult); |
|
|
} |
|
|
|
|
|
result->OcrResult->SingleResult.push_back(singleResult); |
|
|
} |
|
|
} |
|
|
|
|
|
std::string ocr_result; |
|
|
if (!result->OcrResult->SingleResult.empty()) { |
|
|
ocr_result = result->OcrResult->ToJson(pth_img); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::string jsonResponseStr = ocrResponse.DebugString(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (Callback) { |
|
|
Callback(pth_img, ocr_result); |
|
|
} |
|
|
|
|
|
|
|
|
SetTaskIdIdle(taskId); |
|
|
} |