diff --git a/MMMojoCall/examples/c#/Program.cs b/MMMojoCall/examples/c#/Program.cs new file mode 100644 index 0000000..0ef08a8 --- /dev/null +++ b/MMMojoCall/examples/c#/Program.cs @@ -0,0 +1,251 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; + +namespace TencenOCR.CSharp +{ + internal class Program + { + + + /// + /// 加载mmmojo(_64).dll并获取导出函数, 只需要调用一次. + /// + /// + /// + [DllImport("MMMojoCall_d.dll", CallingConvention = CallingConvention.Cdecl)] + extern static bool InitMMMojoDLLFuncs(string mmmojo_dll_path); + + /// + /// 初始化MMMojo (包括ThreadPool等), 只需要调用一次. + /// + /// + /// + [DllImport("MMMojoCall_d.dll", CallingConvention = CallingConvention.Cdecl)] + extern static bool InitMMMojoGlobal(int argc,string[] argv); + + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + public delegate void OCRCALLBACK(string pic_path, IntPtr data, int data_size); + + [DllImport("MMMojoCall_d.dll", CallingConvention = CallingConvention.Cdecl)] + public static extern void SetReadOnPush(OCRCALLBACK callback); + + //下面为正式封装的方法,SetReadOnPushW是回调方法 + //[UnmanagedFunctionPointer(CallingConvention.Cdecl)] + //public delegate void OCRCALLBACK(string pic_path, IntPtr data, int data_size); + + [DllImport("MMMojoCallWrapper.dll", CallingConvention = CallingConvention.Cdecl)] + public static extern void SetReadOnPushW(OCRCALLBACK callback); + + static void Main(string[] args) + { + Console.WriteLine("请输入WeChatOCR.exe的路径,包含WeChatOCR.exe"); + var ocrPath = ""; //Console.ReadLine(); + ocrPath = "D:\\TencentOCR\\QQScreenShot\\Bin\\TencentOCR.exe"; + + Console.WriteLine("请输入mmmojo.dll的路径,不包含mmmojo.dll 例如 \"D:\\\\WeChat\\\\[3.9.7.29]\" 不带双引号"); + var mmPath = ""; //Console.ReadLine(); + mmPath = "D:\\TencentOCR\\QQScreenShot"; + if (!InitMMMojoDLLFuncsW(mmPath)) + { + Console.WriteLine("mmmojocall::InitMMMojoDLLFuncs ERR"); + return; + } + if (!InitMMMojoGlobalW(0, null)) + { + Console.WriteLine("初始化MMMojo ERR"); + return; + } + if (!SetExePathW(ocrPath)) + { + Console.WriteLine("初始化MMMojo ERR: "+GetLastErrStrW()); + return; + } + if (!SetUsrLibDirW(mmPath)) + { + Console.WriteLine("初始化用户路径失败: " + GetLastErrStrW()); + return; + } + + SetCallbackDataMode(true); + + var oper = new OCRCALLBACK(GetMsg); + SetReadOnPushW(oper); + + if (!StartWeChatOCRW()) + { + Console.WriteLine("启动Tencent OCR失败: " + GetLastErrStrW()); + return ; + } + while (true) + { + Console.WriteLine("输入图片名称,包含完整路径。"); + var input = Console.ReadLine(); + if (!string.IsNullOrWhiteSpace(input) && input == "q") + { + KillWeChatOCRW(); + ShutdownMMMojoGlobalW(); + } + if (!string.IsNullOrWhiteSpace(input) && input != "q") + { + if (!DoOCRTaskW(input)) + { + Console.WriteLine("发送图片路劲失败: "+input + GetLastErrStrW()); + return; + } + } + } + } + + static void GetMsg(string pic_path, IntPtr data, int data_size) + { + var msg = Marshal.PtrToStringAnsi(data); + var itmes = msg.Split('\n'); + foreach (var item in itmes) + { + if (string.IsNullOrWhiteSpace(item)) + { continue; } + if (item.Contains("single_str_utf8") || item.Contains("one_str_utf8")) + { + string encryMsg = ""; + var arry = item.Split('"'); + if (arry.Last()==","||string.IsNullOrEmpty(arry.Last())) + encryMsg= arry[arry.Length-2]; + else + encryMsg = arry.Last(); + encryMsg = DecodeBase64("utf-8", encryMsg); + Console.WriteLine(encryMsg); + } + } + //Marshal.FreeHGlobal(data); + //Console.WriteLine(msg); + } + + ///编码 + public static string EncodeBase64(string code_type, string code) + { + string encode = ""; + byte[] bytes = Encoding.GetEncoding(code_type).GetBytes(code); + try + { + encode = Convert.ToBase64String(bytes); + } + catch + { + encode = code; + } + return encode; + } + ///解码 + public static string DecodeBase64(string code_type, string code) + { + string decode = ""; + byte[] bytes = Convert.FromBase64String(code); + try + { + decode = Encoding.GetEncoding(code_type).GetString(bytes); + } + catch + { + decode = code; + } + return decode; + } + + + /// + /// 加载mmmojo(_64).dll并获取导出函数, 只需要调用一次. + /// + /// + /// + [DllImport(".\\MMMojoCallWrapper.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] + public extern static bool InitMMMojoDLLFuncsW(string mmmojo_dll_path); + + /// + /// 设置mmmojo.dll路径,就是微信所在的路径 Such as "D:\\WeChat\\[3.9.7.29]" + /// + /// + /// + /// + //MMMojoCallWrapper.dll + [DllImport("MMMojoCallWrapper.dll", CallingConvention = CallingConvention.Cdecl)] + public extern static bool InitMMMojoGlobalW(int argc, string[] argv); + + + /// + /// 设置WeChatOCR.exe的路径 + /// + /// + /// + /// + //MMMojoCallWrapper.dll + [DllImport("MMMojoCallWrapper.dll", CallingConvention = CallingConvention.Cdecl)] + public extern static bool SetExePathW(string exe_path); + + //SetUsrLibDirW + /// + /// 设置--user-lib-dir命令,即微信mmmojo.dll所在路径 + /// + /// + /// + /// + [DllImport("MMMojoCallWrapper.dll", CallingConvention = CallingConvention.Cdecl)] + public extern static bool SetUsrLibDirW(string usr_lib_dir); + + //SetCallbackDataMode + /// + /// 设置返回给用户的数据类型,ture的话为json + /// + /// + /// + [DllImport("MMMojoCallWrapper.dll", CallingConvention = CallingConvention.Cdecl)] + public extern static void SetCallbackDataMode(bool use_json); + + //StartWeChatOCR + /// + /// * @brief 启动MMMojo环境以及WeChatOCR.exe程序. + /// @return 成功返回true + /// + /// + /// + [DllImport("MMMojoCallWrapper.dll", CallingConvention = CallingConvention.Cdecl)] + public extern static bool StartWeChatOCRW(); + + //DoOCRTaskW + /// + /// * @brief 发送一次OCR请求. + ///* @param pic_path 图片路径 + ///* @return 成功返回true + /// + /// + /// + [DllImport("MMMojoCallWrapper.dll", CallingConvention = CallingConvention.Cdecl)] + public extern static bool DoOCRTaskW(string pic_path); + + //KillWeChatOCRW + /// + /// 销毁MMMojo环境以及WeChatOCR.exe程序. + /// + [DllImport("MMMojoCallWrapper.dll", CallingConvention = CallingConvention.Cdecl)] + public extern static void KillWeChatOCRW(); + + //ShutdownMMMojoGlobalW + /// + /// 结束全局MMMojo环境 (全局调一次). + /// + [DllImport("MMMojoCallWrapper.dll", CallingConvention = CallingConvention.Cdecl)] + public extern static bool ShutdownMMMojoGlobalW(); + + //GetLastErrStrW + /// + /// 获取最后一次报错信息. + /// + [DllImport("MMMojoCallWrapper.dll", CallingConvention = CallingConvention.Cdecl)] + public extern static string GetLastErrStrW(); + + } +} diff --git a/MMMojoCall/examples/cpp/MMMojoCallWrapper.cpp b/MMMojoCall/examples/cpp/MMMojoCallWrapper.cpp new file mode 100644 index 0000000..40ca986 --- /dev/null +++ b/MMMojoCall/examples/cpp/MMMojoCallWrapper.cpp @@ -0,0 +1,68 @@ +#include "pch.h" +#include "MMMojoCallWrapper.h" +#include +using namespace qqimpl; + +namespace MMMojoCallWrapper +{ + mmmojocall::OCRManager ocr_manager; + + bool __stdcall SetExePathW(const char* exe_path) + { + return ocr_manager.SetExePath(exe_path); + } + + + bool __stdcall SetUsrLibDirW(const char* usr_lib_dir) + { + return ocr_manager.SetUsrLibDir(usr_lib_dir); + } + + bool __stdcall InitMMMojoDLLFuncsW(const char* mmmojo_dll_path) + { + std::cout << mmmojo_dll_path << std::endl; + return mmmojocall::InitMMMojoDLLFuncs(mmmojo_dll_path); + } + + bool __stdcall InitMMMojoGlobalW(int argc, const char* const* argv) + { + return mmmojocall::InitMMMojoGlobal(argc, argv); + } + + void __stdcall SetCallbackDataMode(bool use_json) + { + return ocr_manager.SetCallbackDataMode(use_json); + } + + void __stdcall SetReadOnPushW(LPFN_OCRREADONPUSHW pfunc) + { + return ocr_manager.SetReadOnPush(pfunc); + } + + bool __stdcall StartWeChatOCRW() + { + return ocr_manager.StartWeChatOCR(); + } + + bool __stdcall DoOCRTaskW(const char* pic_path) + { + return ocr_manager.DoOCRTask(pic_path); + } + + /* + */ + void __stdcall KillWeChatOCRW() + { + return ocr_manager.KillWeChatOCR(); + } + + bool __stdcall ShutdownMMMojoGlobalW() + { + return mmmojocall::ShutdownMMMojoGlobal(); + } + + const char* __stdcall GetLastErrStrW() + { + return ocr_manager.GetLastErrStr(); + } +} \ No newline at end of file diff --git a/MMMojoCall/src/include/MMMojoCallWrapper.h b/MMMojoCall/src/include/MMMojoCallWrapper.h new file mode 100644 index 0000000..4d1c002 --- /dev/null +++ b/MMMojoCall/src/include/MMMojoCallWrapper.h @@ -0,0 +1,31 @@ +#pragma once +#include "pch.h" +#include +#include "mmmojo_ocr.h" + +namespace MMMojoCallWrapper +{ + typedef void (*LPFN_OCRREADONPUSHW)(const char* pic_path, const void* data, int data_size); + + extern "C" __declspec(dllexport) bool __stdcall InitMMMojoDLLFuncsW(const char* mmmojo_dll_path); + + extern "C" __declspec(dllexport) bool __stdcall InitMMMojoGlobalW(int argc, const char* const* argv); + + extern "C" __declspec(dllexport) bool __stdcall SetExePathW(const char* exe_path); + + extern "C" __declspec(dllexport) bool __stdcall SetUsrLibDirW(const char* usr_lib_dir); + + extern "C" __declspec(dllexport) void __stdcall SetCallbackDataMode(bool use_json); + + extern "C" __declspec(dllexport) void __stdcall SetReadOnPushW(LPFN_OCRREADONPUSHW pfunc); + + extern "C" __declspec(dllexport) bool __stdcall StartWeChatOCRW(); + + extern "C" __declspec(dllexport) bool __stdcall DoOCRTaskW(const char* pic_path); + + extern "C" __declspec(dllexport) void __stdcall KillWeChatOCRW(); + + extern "C" __declspec(dllexport) bool __stdcall ShutdownMMMojoGlobalW(); + + extern "C" __declspec(dllexport) const char* __stdcall GetLastErrStrW(); +}