使用c#中的枚举参数从dll调用c ++函数

时间:2013-08-27 17:24:12

标签: c# c++ dll enums pinvoke

我正在使用一个新版本的facetracker,它提供了一些我用来编写dll文件的头文件。此文件稍后与Unity 3D一起使用。因为我的Unity版本只适用于c#,我必须编写一个Wrapper,这就是问题的起点。我不知道为什么来自facetracker的公司决定改变某些功能,因为它与以前的版本一样使用起来更复杂。

以下是c ++标题中一个特定函数的声明:

virtual bool SetFromRawBuffer(const unsigned char * rawImageData, Format format, int width, int height, Origin origin) = 0;

cpp文件:

C_API bool IImage_SetFromRawBuffer(IImage *pObj, const unsigned char* rawImageData, IImage::Format format, int width, int height, IImage::Origin origin) {
if(pObj) {
    return pObj->SetFromRawBuffer(rawImageData, format, width, height, origin);
}
return false;

}

c#:(因为我不知道如何处理枚举,我只是将它们从原始标题复制到我自己的enumeration.cs。)

[DllImport(@"C:\Users\mad1Z\Desktop\LiveDriverNativeWrapper NEW\LiveDriveNativeWrapper_packed\Release\LiveDriverNativeWrapper.dll")]
static private extern bool IImage_SetFromRawBuffer(IntPtr pObj, byte[] data, Enumerations.Format format, int width, int height, Enumerations.Origin origin);

定义c#:

public bool SetFromRawBuffer(byte[] data, Enumerations.Format format, int width, int height, Enumerations.Origin picorigin)
    {
        return IImage_SetFromRawBuffer(m_pObj, data, format, width, height, picorigin); 
    }

这是崩溃Unity 3D的函数调用:

if (inputFrame.SetFromRawBuffer(data, Enumerations.Format.BGRX8888, 640, 480, Enumerations.Origin.TopLeft))

我也不确定我是否使用了正确的格式。我只是从我的网络摄像头获取imagedata并将其存储在缓冲区(数据)中,然后发送到FaceTracker。

这里是原始头文件中的格式定义:

/// @brief Each member of the following Format enum represents the order the channels
/// are encountered as one walks the memory, byte by byte, from lower to higher    addresses.
///
/// So for example, RGBX8888 means that walking the memory byte by byte from lower to higher addresses,
/// one would expect to encounter R first, G next, and B and X afterwards.  Equivalently, on little endian
/// machines, if the data is read in word-sized chunks, one would expect to find R in bits 0-7, B in 8-15,
/// G in 16-23, and X in 24-31.
///
/// Often you will see documentation stating that images are RGB. On Windows, however, these are actually laid
/// out in memory as BGR. For instance Windows bitmap BI_RGB format actually has data laid out as BGR in memory.
/// Similarly, in DirectShow, when the webcam format is RGB, the data is laid out BGR.
///
/// Finally, BGRX8888 is the preferred format performance-wise, as all code paths are heavily optimized internally
/// with that layout in mind. For best performance, prefer to provide images in BGRX8888 over other supported formats.
/// @if LIVE_DRIVER_SDK_FACEPAINT
/// In addition, all face painting operations happen on BGRX888 data. Therefore, the image returned to you in
/// IOutputFrameFacePaint::GetFacePaintImage will be in BGRX888 format.
/// @endif
enum Format
{
 // RGB formats
BGRX8888,
RGBX8888,
BGR888,
RGB888,

// Grayscale formats
Intensity8,

// YUV 4:2:2 formats
YUV_UYVY,       // YUV_Y422, YUV_UYNV
YUV_YUY2,       // YUV_YUYV, YUV_YUNV
YUV_YVYU,

// YUV 4:2:0 formats
YUV_NV12,
YUV_NV21,       // YUV_420sp
YUV_YV12,       // YUV_420p
YUV_IYUV,       // YUV_I420

FormatCount,

FORMATERROR = 0
};

如果您需要更多信息,请告诉我。

0 个答案:

没有答案