unity3d中c 与java_将数据从Unity3D传递到C dll,反之亦然

我刚刚找到解决方案(ohh yeahhhhhhhh):)

首先,我需要将我的C#Mat(帧)转换为字节数组 . 然后我创建了一个指针,以指向字节数组地址(这是图像数据) . 在此之后,我调用了C dll方法并将指针作为参数(以及图像的宽度和高度)传递 . 最后在C dll中,我使用C#中的字节数组重建了C Mat .

Unity3D中的代码(C#):

//frame is the C# Mat and byte_array our array of byes

//to initialize the size of our byte array (image pixels * channels)

byte_array= new byte[frame.cols () * frame.rows () * frame.channels ()];

//copy the frame data to our array

Utils.copyFromMat (frame,byte_array);

//initialize a pointer with the apropriate size (array size)

IntPtr pointer = Marshal.AllocHGlobal(byte_array.Length);

Marshal.Copy (byte_array, 0, pointer , byte_array.Length);

//here i call the C++ method (I imported a C++ dll)

myCplusplusMethod(pointer ,frame.height(),frame.width());

//free the pointer

Marshal.FreeHGlobal(pointer );

现在,为了使用来自C#的传递数据在C中重建图像:

void myCplusplusMethod(unsigned char* data,int height,int width){

//btw you have to get the correct type. In my situation I use debug from C# and I found out that my image is CV_8UC3 type.

Mat constructed= Mat(height, width, CV_8UC3, data);

//..do whatever using the image.. (calculations , etc)..

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值