项目WINFORM调用双目摄像头.c#在调用非托管C++dll库时,在调用函数获取图片时头大了,求高手帮忙翻译. 上代码
头文件
/*
Function: DVTLDCam_GetImage();
Description: Get normal image after event returned EVENT_OK, 3 image types supported, refer to LD_IMG_TYPE .
Parameters:
hCam -- Handle of the opened camera.
nImageType -- image type, refer to LD_IMG_TYPE.
pBuff -- buffer to receive image data, recommended buffer length is width*height*5. (640*480*5)
nBuffLen -- length of buffer
pRetLen -- returned data length.
return:
0 -- No error
others-- Error
*/
DVTLDCAMSDK_API int STDCALL DVTLDCam_GetImage(HANDLE hCam, int nImageType, unsigned char* pBuff, int nBuffLen, int *pRetLen);
c# 调用
[StructLayoutAttribute(LayoutKind.Sequential,CharSet = CharSet.Ansi, Pack = 1)]
public struct ByteBuff
{
/// unsigned char[30]
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 1536000)]
public byte[] pBuff;
}
[DllImport("DVTLDCamSDK.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
internal static extern int DVTLDCam_GetImage(IntPtr hCam, Int32 nImageType, IntPtr pBuff, Int32 nBuffLen, ref Int32 pRetLen);
public void EventCallback(int event_id)
{
int nRet;
int nLen =1536000;
int nRetLen = 0;
int nFaceW = 0, nFaceH = 0;
string strPath = GetFilePath();
string savePath = string.Empty;
string saveFacePath = string.Empty;
ByteBuff pBuff = new ByteBuff();
if (event_id == 1)
{
int nSelect = -1;
nSelect = ImageFormatIndex;
nRet = DVTLDCam_GetImage_Delegate(m_hCam, nSelect, ref pBuff, nLen, ref nRetLen);
if (nRet != 0)
{
MessageBox.Show("Get image error");
goto done;
}
if (nSelect == 0) //CameraDev.LD_IMG_TYPE.IMG_RGB24
{
savePath = strPath + ".bmp";
此处pBuff.pBuff byte[] 始终是1536000 全部是0
WriteBMPFile2(savePath, pBuff.pBuff, nRetLen);
}
else
{
if (nSelect == 1) //CameraDev.LD_IMG_TYPE.IMG_JPG
{
savePath = strPath + ".jpg";
}
else if (nSelect == 2) //CameraDev.LD_IMG_TYPE.IMG_RGB24_BASE64
{
savePath = strPath + "_rgb.txt";
}
else if (nSelect == 3) //CameraDev.LD_IMG_TYPE.IMG_JPG_BASE64
{
savePath = strPath + "_base64.txt";
}
WriteFile(savePath, pData, nRetLen);
}
MessageBox.Show("Detect success");
}
else
{
MessageBox.Show("Detect error, event = " + event_id.ToString());
}
done:
if (event_id == CameraDev.EVENT_CALLCED)
{
}
else
{
StartDetect();
}
}
private int DVTLDCam_GetImage_Delegate(IntPtr hCam, Int32 nImageType, ref CameraDev.ByteBuff pBuff, Int32 nBuffLen, ref Int32 pRetLen)
{
System.IntPtr IntPtrBuff = IntPtr.Zero;
try
{
int param_size = Marshal.SizeOf(pBuff);
IntPtrBuff = Marshal.AllocHGlobal(param_size);
Marshal.StructureToPtr(pBuff, IntPtrBuff, false);
int ret = CameraDev.DVTLDCam_GetImage(hCam, nImageType, IntPtrBuff, nBuffLen, ref pRetLen);
pBuff = (CameraDev.ByteBuff)Marshal.PtrToStructure(IntPtrBuff, typeof(CameraDev.ByteBuff));
return ret;
}
catch (Exception ex)
{
Debug.Assert(false, ex.ToString());
}
finally
{
try
{
Marshal.FreeHGlobal(IntPtrBuff);
}
catch (Exception ex)
{
Debug.Assert(false, ex.ToString());
}
}
return 0;
}[code=c#][/code]