主题
从内存加载OCR加密模型 - OcrLoadModelMemory
函数简介
从内存缓冲区加载欧拉 OCR 加密包(包头须含 OcrEncryptModel 写入的元数据)。
接口名称
OcrLoadModelMemoryDLL 调用
long OcrLoadModelMemory(long ola, long memoryAddr, int size, string password, int inferenceDevice);参数说明
| 参数名 | 类型 | 说明 |
| ------ | ---- | ---- |
| ola | 长整数型 | OLAPlug 对象指针,由 CreateCOLAPlugInterFace 生成。 |
| memoryAddr | 长整数型 | 加密包内存首地址。 |
| size | 整数型 | 加密包字节长度。 |
| password | 字符串 | 解密密码(UTF-8,欧拉官方模型密码为空)。 |
| inferenceDevice | 整数型 | 0=GPU,-1=CPU,其他为 GPU 索引;不可用时回退 CPU。 |
示例
以下示例演示 读入 OCR 加密包 → 取得内存首地址 → 解密并加载 的完整流程。
SDK 调用
cpp
#include "OLAPlugServer.h"
#include <fstream>
#include <vector>
OLAPlugServer ola;
const char* encPath = "models/ocr.olam";
std::ifstream ifs(encPath, std::ios::binary | std::ios::ate);
if (!ifs) return;
long pkgSize = (long)ifs.tellg();
ifs.seekg(0);
std::vector<unsigned char> pkgBuf(pkgSize);
ifs.read((char*)pkgBuf.data(), pkgSize);
long handle = ola.OcrLoadModelMemory((long)pkgBuf.data(), pkgSize, "", -1);
if (handle != 0) {
// handle 用于 OcrEx / OcrDetailsEx 等接口
}csharp
using System.IO;
using System.Runtime.InteropServices;
using OLAPlug;
var ola = new OLAPlugServer();
byte[] pkgBytes = File.ReadAllBytes(@"models\ocr.olam");
GCHandle pin = GCHandle.Alloc(pkgBytes, GCHandleType.Pinned);
try {
long handle = ola.OcrLoadModelMemory(
pin.AddrOfPinnedObject().ToInt64(), pkgBytes.Length, "", -1);
if (handle != 0)
{
// handle 用于 OcrEx / OcrDetailsEx 等接口
}
} finally { pin.Free(); }python
from OLAPlugServer import OLAPlugServer
import ctypes
ola = OLAPlugServer()
with open("models/ocr.olam", "rb") as f:
pkg_bytes = f.read()
buf = ctypes.create_string_buffer(pkg_bytes, len(pkg_bytes))
handle = ola.OcrLoadModelMemory(ctypes.addressof(buf), len(pkg_bytes), "", -1)
if handle:
# handle 用于 OcrEx / OcrDetailsEx 等接口
passjava
import com.olaplug.OLAPlugServer;
import com.sun.jna.Memory;
import com.sun.jna.Pointer;
import java.nio.file.Files;
import java.nio.file.Paths;
OLAPlugServer ola = new OLAPlugServer();
byte[] pkgBytes = Files.readAllBytes(Paths.get("models/ocr.olam"));
Memory mem = new Memory(pkgBytes.length);
mem.write(0, pkgBytes, 0, pkgBytes.length);
long handle = ola.OcrLoadModelMemory(Pointer.nativeValue(mem), pkgBytes.length, "", -1);
if (handle != 0) {
// handle 用于 OcrEx / OcrDetailsEx 等接口
}cpp
var ola = com("OlaPlug.OlaSoft")
var pkgSize = ola.GetFileSize("models/ocr.olam")
var pkgPtr = ola.ReadBytesFromFile("models/ocr.olam", 0, 0)
if(pkgPtr && pkgSize > 0) {
var handle = ola.OcrLoadModelMemory(pkgPtr, pkgSize, "", -1)
ola.FreeMemoryPtr(pkgPtr)
if(handle) {
// handle 用于 OcrEx / OcrDetailsEx 等接口
}
}vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
pkgSize = ola.GetFileSize("models/ocr.olam")
pkgPtr = ola.ReadBytesFromFile("models/ocr.olam", 0, 0)
If pkgPtr <> 0 And pkgSize > 0 Then
handle = ola.OcrLoadModelMemory(pkgPtr, pkgSize, "", -1)
ola.FreeMemoryPtr(pkgPtr)
If handle <> 0 Then
' handle 用于 OcrEx / OcrDetailsEx 等接口
End If
End Iftext
.局部变量 ola, OLAPlug
.局部变量 pkgData, 字节集
.局部变量 handle, 长整数型
ola.创建 ()
' ① 读入 OCR 加密包到字节集
pkgData = 读入文件 (“models/ocr.olam”, )
.如果真 (取字节集长度 (pkgData) > 0)
' ② 取变量数据地址 → 内存首地址;取字节集长度 → size
handle = ola.OcrLoadModelMemory (
取变量数据地址 (pkgData), 取字节集长度 (pkgData), “”, -1)
.如果真 (handle ≠ 0)
' handle 用于 OcrEx / OcrDetailsEx 等接口
.如果真结束
.如果真结束
' pkgData 在调用完成前须保持有效;欧拉官方模型 password 传空字符串aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var pkgData = io.load("models/ocr.olam");
var handle = ola.OcrLoadModelMemory(pkgData, #pkgData, "", -1);
if(handle) {
// handle 用于 OcrEx / OcrDetailsEx 等接口
}text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
变量 pkgData <类型 = 字节集类>
' ① 读入 OCR 加密包
pkgData = 读入文件("models/ocr.olam", )
如果真 (取字节集长度(pkgData) > 0)
{
' ② 取字节集指针 → 内存首地址
长整数 handle = ola.OcrLoadModelMemory(
取字节集指针(pkgData), 取字节集长度(pkgData), "", -1)
如果真 (handle ≠ 0)
{
' handle 用于 OcrEx / OcrDetailsEx 等接口
}
}
' 欧拉官方模型 password 传空字符串cpp
#include "OLAPlugServer.h"
#include <fstream>
#include <vector>
OLAPlugServer ola;
std::ifstream ifs("models/ocr.olam", std::ios::binary | std::ios::ate);
long pkgSize = (long)ifs.tellg();
ifs.seekg(0);
std::vector<unsigned char> pkgBuf(pkgSize);
ifs.read((char*)pkgBuf.data(), pkgSize);
long handle = ola.OcrLoadModelMemory((long)pkgBuf.data(), pkgSize, "", -1);
if (handle != 0) {
// handle 用于 OcrEx / OcrDetailsEx 等接口
}原生 DLL 调用
cpp
#include <fstream>
#include <vector>
long instance = CreateCOLAPlugInterFace();
std::ifstream ifs("models/ocr.olam", std::ios::binary | std::ios::ate);
auto pkgSize = (int)ifs.tellg();
ifs.seekg(0);
std::vector<unsigned char> buf(pkgSize);
ifs.read((char*)buf.data(), pkgSize);
long handle = OcrLoadModelMemory(instance, (long)buf.data(), pkgSize, "", -1);
if (handle != 0) {
// handle 用于 OcrEx / OcrDetailsEx 等接口
}csharp
using System.IO;
using System.Runtime.InteropServices;
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long OcrLoadModelMemory(long ola, long memoryAddr, int size, string password, int inferenceDevice);
long instance = CreateCOLAPlugInterFace();
byte[] pkgBytes = File.ReadAllBytes(@"models\ocr.olam");
GCHandle pin = GCHandle.Alloc(pkgBytes, GCHandleType.Pinned);
try {
long handle = OcrLoadModelMemory(instance,
pin.AddrOfPinnedObject().ToInt64(), pkgBytes.Length, "", -1);
if (handle != 0) {
// handle 用于 OcrEx / OcrDetailsEx 等接口
}
} finally { pin.Free(); }python
from ctypes import CDLL, c_int, c_int64, create_string_buffer
ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
ola.OcrLoadModelMemory.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
with open("models/ocr.olam", "rb") as f:
pkg_bytes = f.read()
buf = create_string_buffer(pkg_bytes, len(pkg_bytes))
handle = ola.OcrLoadModelMemory(instance, c_int64(id(buf.raw)), len(pkg_bytes), b"", -1)
if handle:
pass # handle 用于 OcrEx / OcrDetailsEx 等接口返回值
| 返回值 | 说明 |
| ------ | ---- |
| (返回值) | 长整数型:模型句柄,失败返回 0。 |
注意事项
| 项目 | 说明 |
| ---- | ---- |
| 字节流 → 地址 | 易语言:取变量数据地址(字节集) + 取字节集长度(字节集);火山:取字节集指针(字节集) + 取字节集长度(字节集)。 |
| ReadBytesFromFile | 返回指针可直接作为 memoryAddr;加载成功后须 FreeMemoryPtr 释放读盘缓冲。 |
| 加密包 | 须为 OcrEncryptModel 生成的包;可用 OcrIsEncryptedModelPackageMemory 预检。 |
| 密码 | 欧拉官方模型 password 传空字符串;自定义加密包须传入正确 UTF-8 密码。 |
| 适用场景 | 磁盘路径可用 OcrLoadModel;本接口适合 网络下载、资源嵌入 等无文件路径场景。 |
