将数组从c#传递到c++;相似参数
我在C#中使用本机代码。我试图通过Pinvoke将C数组从C++传递到C++。一次在C++中,我想填充我作为参数传递的数组。指一个cv::Mat。问题是我使用的是copy函数,我不认为这在参数中发生得很好。有人能帮我吗?你好,谢谢你 C++代码:将数组从c#传递到c++;相似参数,c#,c++,opencv,C#,C++,Opencv,我在C#中使用本机代码。我试图通过Pinvoke将C数组从C++传递到C++。一次在C++中,我想填充我作为参数传递的数组。指一个cv::Mat。问题是我使用的是copy函数,我不认为这在参数中发生得很好。有人能帮我吗?你好,谢谢你 C++代码: __declspec(dllexport) void ProcessFrame(unsigned char* arr) { VideoCapture camera; if (!camera.open(0)) { return; } Mat
__declspec(dllexport)
void ProcessFrame(unsigned char* arr) {
VideoCapture camera;
if (!camera.open(0))
{
return;
}
Mat frame;
camera >> frame;
if (frame.empty()) {
return;
}
int tam = frame.cols * frame.rows;
copy(frame.begin, frame.end, arr);
}
C#代码:
[DllImport(“nativoprinciio.dll”)]
公共静态外部void ProcessFrame(out字节[]img);
byte[]imgData=新字节[webcamTexture.width*webcamTexture.height];
ProcessFrame(输出imgData);
Debug.Log(imgData.Length);
Texture2D tex=新的Texture2D(webcamTexture.width、webcamTexture.height、TextureFormat.RGB24、false);
tex.LoadRawTextureData(imgData);
tex.Apply();
GetComponent().material.mainTexture=tex;
您不能使用新字节
。您必须调用封送拆收器并使签名为IntPtr
公共静态extedn void ProcessFrame(IntPtr img)然后调用它GCHandle-hArray=GCHandle.Alloc(byteArray,GCHandleType.pinted);IntPtr ptrArray=hArray.AddrOfPinnedObject();进程帧(ptrArray);自由的代码>不要忘记在C++调用中添加<代码>外部> C >代码,以防止名称的篡改。另一方面,统一瀑布。问题出现在c++复制函数中。我的搭档告诉我不要用memcpy
[DllImport("NativoPrincipio.dll")]
public static extern void ProcessFrame(out byte[] img);
byte[] imgData = new byte[webcamTexture.width * webcamTexture.height];
ProcessFrame(out imgData);
Debug.Log(imgData.Length);
Texture2D tex = new Texture2D(webcamTexture.width, webcamTexture.height, TextureFormat.RGB24, false);
tex.LoadRawTextureData(imgData);
tex.Apply();
GetComponent<Renderer>().material.mainTexture = tex;