本人尝试封装paddleocr,给C#直接调用,但是每次到调封装的C++方法时就会报错:“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。” ,始终难以解决,下面时我的C#代码:
[DllImport("ocr_system.dll", EntryPoint = "LoadModel", SetLastError = true, CharSet = CharSet.Ansi)]
static extern IntPtr LoadModel(ref byte[] input, int height, int width); //out IntPtr seg_res
private void button1_Click(object sender, EventArgs e)
{
string image_path = "C:/Users/soft016/Desktop/Test/t1.jpg";
Bitmap bmp = new Bitmap(image_path);
int stride;
byte[] source = GetBGRValues(bmp, out stride);
IntPtr seg_img = LoadModel(ref source, bmp.Width, bmp.Height);
Mat img = new Mat(seg_img);
Bitmap seg_show = new Bitmap(img.Cols, img.Rows, (int)img.Step(), System.Drawing.Imaging.PixelFormat.Format24bppRgb, img.Data);
pictureBox1.Image = seg_show;
}
下面时C++里面的方法:
extern "C" __declspec(dllexport) cv::Mat * LoadModel(char* input, int width, int height);
__declspec(dllexport) cv::Mat* LoadModel(char* input, int width, int height)
{
C++代码
}
执行到C#代码的 IntPtr seg_img = LoadModel(ref source, bmp.Width, bmp.Height); 就会报错提示 ,求解答