Skip to content

Commit

Permalink
fix: recognized text may have special chars hurting json
Browse files Browse the repository at this point in the history
  • Loading branch information
lixungeng committed Nov 6, 2024
1 parent 2faebdc commit de82245
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
29 changes: 26 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,29 @@ int APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserve
return TRUE;
}

static string json_encode(const string& input) {
string output;
output.reserve(input.size() + 2);
for (auto c : input) {
switch (c) {
case '"': output += "\\\""; break;
case '\\': output += "\\\\"; break;
case '\n': output += "\\n"; break;
case '\r': output += "\\r"; break;
case '\t': output += "\\t"; break;
default:
if (c>=0 && c < 0x20) {
char buf[16];
snprintf(buf, sizeof(buf), "\\u%04x", c);
output += buf;
} else {
output.push_back(c);
}
}
}
return output;
}

extern "C" __declspec(dllexport)
bool wechat_ocr(const wchar_t* ocr_exe, const wchar_t * wechat_dir, const char * imgfn, void(*set_res)(const char * dt))
{
Expand All @@ -28,7 +51,7 @@ bool wechat_ocr(const wchar_t* ocr_exe, const wchar_t * wechat_dir, const char *
string json;
json += "{";
json += "\"errcode\":" + std::to_string(res.errcode) + ",";
json += "\"imgpath\":\"" + res.imgpath + "\",";
json += "\"imgpath\":\"" + json_encode(res.imgpath) + "\",";
json += "\"width\":" + std::to_string(res.width) + ",";
json += "\"height\":" + std::to_string(res.height) + ",";
json += "\"ocr_response\":[";
Expand All @@ -39,10 +62,10 @@ bool wechat_ocr(const wchar_t* ocr_exe, const wchar_t * wechat_dir, const char *
json += "\"right\":" + std::to_string(blk.right) + ",";
json += "\"bottom\":" + std::to_string(blk.bottom) + ",";
json += "\"rate\":" + std::to_string(blk.rate) + ",";
json += "\"text\":\"" + blk.text + "\"";
json += "\"text\":\"" + json_encode(blk.text) + "\"";
json += "},";
}
if (!res.ocr_response.empty()) {
if (json.back() == ',') {
json.pop_back();
}
json += "]}";
Expand Down
2 changes: 1 addition & 1 deletion src/pyspt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "pyspt.h"
#include "wechatocr.h"

#ifdef USE_PYTHON
#if USE_PYTHON+0

wstring utf8towstr(const char* utf8)
{
Expand Down
17 changes: 10 additions & 7 deletions src/pyspt.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#pragma once

#ifndef USE_PYTHON
#if __has_include(<Python.h>)
#define USE_PYTHON 1
#define Py_LIMITED_API 0x03080000

// we dont have python310_d.lib, so temporarily disable debug mode
#pragma push_macro("_DEBUG")
#undef _DEBUG
#include <Python.h>
#pragma pop_macro("_DEBUG")
#endif
#endif

#if USE_PYTHON+0
# define Py_LIMITED_API 0x03080000
# pragma push_macro("_DEBUG")
# undef _DEBUG // we dont have python310_d.lib, so temporarily disable debug mode
# include <Python.h>
# pragma pop_macro("_DEBUG")
#endif
2 changes: 1 addition & 1 deletion vs.proj/wcocr.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>NDEBUG;USE_PYTHON=0;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp20</LanguageStandard>
<AdditionalIncludeDirectories>.;../include;../spt;X:\tools\Python310\include</AdditionalIncludeDirectories>
Expand Down

0 comments on commit de82245

Please sign in to comment.