来自非托管内存的 byte[] 数组
byte[] array from unmanaged memory
我正在 monodevelop 中 linux (Ubuntu) 下为 Basler 相机的 C/C++ Pylon 库编写一个简单的 .NET 包装器。我正在 Code::Blocks 中构建一个 .so (.dll) 文件,并在 monodevelop 中构建 P/Invoke 文件。
我有两个简单的任务:获取单个图像和获取一系列图像。
第一部分我是这样管理的:
在 C++ 中我有一个函数:
void GetImage(void* ptr)
{
CGrabResultPtr ptrGrabResult;
//here image is grabbing to ptrGrabResult
camera.GrabOne(5000,ptrGrabResult,TimeoutHandling_ThrowException);
//i'm copying byte array with image to my pointer ptr
memcpy(ptr,ptrGrabResult->GetBuffer(),_width*_height);
if (ptrGrabResult->GrabSucceeded())
return;
else
cout<<endl<<"Grab Failed;"<<endl;
}
在 C# 中:
[DllImport("libPylonInterface.so")]
private static extern void GetImage(IntPtr ptr);
public static void GetImage(out byte[] arr)
{
//allocating unmanaged memory for byte array
IntPtr ptr = Marshal.AllocHGlobal (_width * _height);
//and "copying" image data to this pointer
GetImage (ptr);
arr = new byte[_width * _height];
//copying from unmanaged to managed memory
Marshal.Copy (ptr, arr, 0, _width * _height);
Marshal.FreeHGlobal(ptr);
}
然后我可以从这个 byte[] arr
.
构建图像
我必须复制大量字节两次(1. 在 c++ memcpy()
中;2. 在 c# Marshal.Copy()
中)。我尝试使用指向图像缓冲区的直接指针 ptr = ptrGrabResult -> GetBuffer()
,但在编组字节时出现单声道环境错误。
所以这里有一个问题:它是一个很好的解决方案吗?我的方向对吗?
P.S。请给我一些建议我应该如何处理图像序列?
您可以让编组器为您完成这项工作。像这样:
[DllImport("libPylonInterface.so")]
private static extern void GetImage([Out] byte[] arr);
....
arr = new byte[_width * _height];
GetImage(arr);
这避免了第二次内存复制,因为编组器将固定托管数组并将其地址传递给非托管代码。然后非托管代码可以直接填充托管内存。
第一个副本看起来更难避免。这可能是您正在使用的相机库强加给您的。我会评论说,如果抓取成功,您应该只执行该复制。
我正在 monodevelop 中 linux (Ubuntu) 下为 Basler 相机的 C/C++ Pylon 库编写一个简单的 .NET 包装器。我正在 Code::Blocks 中构建一个 .so (.dll) 文件,并在 monodevelop 中构建 P/Invoke 文件。 我有两个简单的任务:获取单个图像和获取一系列图像。
第一部分我是这样管理的:
在 C++ 中我有一个函数:
void GetImage(void* ptr)
{
CGrabResultPtr ptrGrabResult;
//here image is grabbing to ptrGrabResult
camera.GrabOne(5000,ptrGrabResult,TimeoutHandling_ThrowException);
//i'm copying byte array with image to my pointer ptr
memcpy(ptr,ptrGrabResult->GetBuffer(),_width*_height);
if (ptrGrabResult->GrabSucceeded())
return;
else
cout<<endl<<"Grab Failed;"<<endl;
}
在 C# 中:
[DllImport("libPylonInterface.so")]
private static extern void GetImage(IntPtr ptr);
public static void GetImage(out byte[] arr)
{
//allocating unmanaged memory for byte array
IntPtr ptr = Marshal.AllocHGlobal (_width * _height);
//and "copying" image data to this pointer
GetImage (ptr);
arr = new byte[_width * _height];
//copying from unmanaged to managed memory
Marshal.Copy (ptr, arr, 0, _width * _height);
Marshal.FreeHGlobal(ptr);
}
然后我可以从这个 byte[] arr
.
我必须复制大量字节两次(1. 在 c++ memcpy()
中;2. 在 c# Marshal.Copy()
中)。我尝试使用指向图像缓冲区的直接指针 ptr = ptrGrabResult -> GetBuffer()
,但在编组字节时出现单声道环境错误。
所以这里有一个问题:它是一个很好的解决方案吗?我的方向对吗?
P.S。请给我一些建议我应该如何处理图像序列?
您可以让编组器为您完成这项工作。像这样:
[DllImport("libPylonInterface.so")]
private static extern void GetImage([Out] byte[] arr);
....
arr = new byte[_width * _height];
GetImage(arr);
这避免了第二次内存复制,因为编组器将固定托管数组并将其地址传递给非托管代码。然后非托管代码可以直接填充托管内存。
第一个副本看起来更难避免。这可能是您正在使用的相机库强加给您的。我会评论说,如果抓取成功,您应该只执行该复制。