C# 引用C++动态库方法
标准形式:
[DllImport("KSJApi.dll")]
public static extern int KSJ_PreviewSetPos( int nChannel, IntPtr hWnd, int x, int y, int nWidth, int nHeight );
例子:
using System; using System.Runtime.InteropServices; using System.Text; /// <summary> /// 2010.07.25 还有些简单函数没有列出,客户可参考例程自己加入 /// </summary> namespace KSJCamera { public class KSJApiB { public enum KSJ_DEVICETYPE { KSJ_UC130C_MRNN = 0, KSJ_UC130M_MRNN, KSJ_UC200C_MRNN, KSJ_UC320C_MRNN, KSJ_UC130C_MRYN, KSJ_UC130M_MRYN, KSJ_UC200C_MRYN, KSJ_UC320C_MRYN, KSJ_UC500C_MRNN, KSJ_UC500M_MRNN, KSJ_UC500C_MRYN, KSJ_UC500M_MRYN, KSJ_UC320C_OCR, KSJ_UC900C_MRNN, KSJ_UC1000C_MRNN, KSJ_UC900C_MRYN, KSJ_UC1000C_MRYN, KSJ_UC130C_MRYY, KSJ_UC130M_MRYY, KSJ_UD140C_SRNN, KSJ_UD140M_SRNN, KSJ_UC36C_MGNN, KSJ_UC36M_MGNN, KSJ_UC36C_MGYN, KSJ_UC36M_MGYN, KSJ_UC900C_MRYY, KSJ_UC1000C_MRYY, }; public enum KSJ_ADDRESSMODE { KSJ_SKIPNONE = 0, KSJ_SKIP2, KSJ_SKIP3, KSJ_SKIP4, KSJ_SKIP8 }; // 相机参数 public enum KSJ_PARAM { KSJ_EXPOSURE = 0, KSJ_RED, KSJ_GREEN, KSJ_BLUE, KSJ_GAMMA, KSJ_PREVIEW_COLUMNSTART, KSJ_PREVIEW_ROWSTART, KSJ_CAPTURE_COLUMNSTART, KSJ_CAPTURE_ROWSTART, KSJ_HORIZONTALBLANK, KSJ_VERTICALBLANK, KSJ_FLIP, KSJ_BIN, KSJ_MIRROR, KSJ_CONTRAST, KSJ_BRIGHTNESS, KSJ_VGAGAIN, KSJ_CLAMPLEVEL, KSJ_CDSGAIN, KSJ_RED_SHIFT, KSJ_GREEN_SHIFT, KSJ_BLUE_SHIFT, KSJ_COMPANDING }; public enum KSJ_PREVIEWMODE { PM_RAWDATA, PM_RGBDATA }; // 枚举类型贝耶尔模式 public enum KSJ_BAYERMODE { KSJ_BGGR_BGR24 = 0, KSJ_GRBG_BGR24, KSJ_RGGB_BGR24, KSJ_GBRG_BGR24, KSJ_BGGR_BGR24_FLIP, KSJ_GRBG_BGR24_FLIP, KSJ_RGGB_BGR24_FLIP, KSJ_GBRG_BGR24_FLIP, KSJ_BGGR_BGR32, KSJ_GRBG_BGR32, KSJ_RGGB_BGR32, KSJ_GBRG_BGR32, KSJ_BGGR_BGR32_FLIP, KSJ_GRBG_BGR32_FLIP, KSJ_RGGB_BGR32_FLIP, KSJ_GBRG_BGR32_FLIP, KSJ_BGGR_GRAY8, KSJ_GRBG_GRAY8, KSJ_RGGB_GRAY8, KSJ_GBRG_GRAY8, KSJ_BGGR_GRAY8_FLIP, KSJ_GRBG_GRAY8_FLIP, KSJ_RGGB_GRAY8_FLIP, KSJ_GBRG_GRAY8_FLIP }; public enum KSJ_TRIGGERMODE { KSJ_TRIGGER_INTERNAL = 0, KSJ_TRIGGER_EXTERNAL }; public enum KSJ_TRIGGERSTATUS { KSJ_NO=0, KSJ_HAVE }; public enum KSJ_ReturnCode { RET_SUCCESS=1, RET_PARAMERROR= -1, // User's Parameter Passed to API Error. RET_MALLOCFAIL= -2, // Memory Allocated Fail. RET_NOTSUPPORT= -3, // Function not supported by such type camera. RET_DEVICENOTEXIST=4, // Device doesn't be detected. RET_DEVICENOTINIT= -5, // Device haven't be initialized RET_VIOLATION= -6, // This operation is conflict to other operation. RET_NOPRIVILEGE= -7, // User no privilege RET_FAIL= -8, // Function Return Failed.( Normal Error, no Detail ) RET_WRONG = -9, // Same to RET_FAIL. RET_RECOVERY_SUCCESS = -10, // Device is recovered successfully. RET_RECOVERY_FAIL = -11, // Device recovered, but fail. RET_BADFRAME = -12, // Bad Frame comes from sensor, should be skipped. RET_INVALIDFRAME = -13, // Invalid Frame, Transmission Error, this frame should re-get. RET_ZEROFRAME = -14, // 帧存相机会返回此值,表示采集图像数据0字节,错误的帧 RET_VERSION_ERROR = -15 // 版本错误 }; public enum KSJ_COLORCORRECT { KSJ_CC_NONE=0, KSJ_CC_SHIFTRGB, KSJ_CC_EQRGB, KSJ_CC_EQALL, KSJ_CC_WB }; // 通过函数返回值可以得到执行信息 [DllImport("KSJApi.dll")] public static extern int KSJ_GetErrorInfo(int nErrorNo, IntPtr pszErrorInfo); // 初始化动态库,必须调用 [DllImport("KSJApi.dll")] public static extern int KSJ_Init(); [DllImport("KSJApi.dll")] public static extern int KSJ_UnInit(); // 得到接入设备数目 [DllImport("KSJApi.dll")] public static extern int KSJ_DeviceGetCount( ); // 得到设备信息(型号,序号,固件版本号) [DllImport("KSJApi.dll")] public static extern int KSJ_DeviceGetInformation(int nChannel, ref int pDeviceType, ref int pnIndex, ref ushort pwFirmwareVersion); // 设置采集视场大小 [DllImport("KSJApi.dll")] public static extern int KSJ_CaptureSetFieldOfView( int nChannel, int nColumnStart, int nRowStart, int nColumnSize, int nRowSize, KSJ_ADDRESSMODE ColumnAddressMode, KSJ_ADDRESSMODE RowAddressMode ); // 获取采集视场的实际大小 [DllImport("KSJApi.dll")] public static extern int KSJ_CaptureGetFieldOfView( int nChannel, ref int pnColumnStart, ref int pnRowStart,ref int pnColumnSize, ref int pnRowSize,ref KSJ_ADDRESSMODE pColumnAddressMode,ref KSJ_ADDRESSMODE pRowAddressMode ); [DllImport("KSJApi.dll")] public static extern int KSJ_PreviewSetMode( int nChannel, KSJ_PREVIEWMODE PreviewMode ); // 获取预览模式 [DllImport("KSJApi.dll")] public static extern int KSJ_PreviewGetMode( int nChannel, ref KSJ_PREVIEWMODE PreviewMode ); [DllImport("KSJApi.dll")] public static extern int KSJ_PreviewGetDefaultMode( int nChannel, ref KSJ_PREVIEWMODE PreviewMode ); // 得到采集图像的宽度和高度(单位:像素) [DllImport("KSJApi.dll")] public static extern int KSJ_CaptureGetSize(int nChannel, ref int pnWidth, ref int pnHeight); // 得到采集图像的宽度和高度(单位:像素)及图像比特位数 [DllImport("KSJApi.dll")] public static extern int KSJ_CaptureGetSizeEx( int nChannel, ref int pnWidth, ref int pnHeight, ref int pnBitCount); [DllImport("KSJApi.dll")] public static extern int KSJ_CaptureRgbData( int nChannel, byte [] pRgbData ); // 获得参数的设置范围 [DllImport("KSJApi.dll")] public static extern int KSJ_GetParamRange( int nChannel, KSJ_PARAM Param, ref int pnMinValue, ref int pnMaxValue ); // 设置参数 [DllImport("KSJApi.dll")] public static extern int KSJ_SetParam ( int nChannel, KSJ_PARAM Param, int nValue ); // 获取参数当前值 [DllImport("KSJApi.dll")] public static extern int KSJ_GetParam ( int nChannel, KSJ_PARAM Param, ref int pnValue ); // 设置Bayer模式,同时影响采集和预览,这个函数除特殊需要调用,否则用户不需调用此功能。 [DllImport("KSJApi.dll")] public static extern int KSJ_BayerSetMode( int nChannel, KSJ_BAYERMODE BayerMode ); // 获得当前Bayer模式 [DllImport("KSJApi.dll")] public static extern int KSJ_BayerGetMode(int nChannel, ref KSJ_BAYERMODE pBayerMode); // 设置Gamma值,范围 -9 - +9 [DllImport("KSJApi.dll")] public static extern int KSJ_GammaSetValue( int nChannel, int nValue ); // 打开或关闭Gamma校正,即影响预览又影响采集 [DllImport("KSJApi.dll")] public static extern int KSJ_GammaOn(int nChannel, bool bOn); // 设置自动白平衡区域 [DllImport("KSJApi.dll")] public static extern int KSJ_AWBSetRegion( int nChannel, int nX, int nY, int nW, int nH ); // 自动白平衡回调函数 public delegate void KSJ_AWBCALLBACK( bool bSuccess, int nRValue, int nGValue, int nBValue, IntPtr lpContext ); // 定义自动白平衡(操作硬件)操作结束后的回调函数,当自动曝光结束时,KSJ开发包会自动调用AECallback用户指定的回调函数 [DllImport("KSJApi.dll")] public static extern int KSJ_AWBSetCallback(int nChannel, KSJ_AWBCALLBACK fpAWBCallback, IntPtr lpContext); // 开始或终止自动白平衡调节 [DllImport("KSJApi.dll")] public static extern int KSJ_AWBStart( int nChannel, bool bStart, int nMaxCount, int nRTarget, int nGTarget, int nBTarget ); // 定义委托类型 // 自动曝光回调函数 public delegate void KSJ_AECALLBACK( bool bSuccess, int nResult, IntPtr lpContext ); // 设置自动曝光的区域,这个区域是相对预览图像的左上角像素位置 [DllImport("KSJApi.dll")] public static extern int KSJ_AESetRegion(int nChannel, int nX, int nY, int nW, int nH); // 定义自动曝光操作结束后的回调函数,当自动曝光结束时,KSJ开发包会自动调用AECallback用户指定的回调函数 [DllImport("KSJApi.dll")] public static extern int KSJ_AESetCallback(int nChannel, KSJ_AECALLBACK pfAECallback, IntPtr lpContext); // 开始或终止自动曝光调节 [DllImport("KSJApi.dll")] public static extern int KSJ_AEStart(int nChannel, bool bStart, int nMaxCount, int nTarget); [DllImport("KSJApi.dll")] public static extern int KSJ_RecordStart( int nChannel, bool bStart, float fRecordRate,string pFileName ); [DllImport("KSJApi.dll")] public static extern int KSJ_RecordStatusGet( int nChannel, ref bool bStart ); // 设置外部触发 [DllImport("KSJApi.dll")] public static extern int KSJ_TriggerModeSet(int nChannel, KSJApiB.KSJ_TRIGGERMODE mode); [DllImport("KSJApi.dll")] public static extern int KSJ_TriggerModeGet(int nChannel, ref KSJApiB.KSJ_TRIGGERMODE mode); // KSJ_NO表示没有图像可以供主机读取,KSJ_HAVE表示有一帧图像可以供主机读取,用户可以调用KSJ_CaptureRgbData等采集函数来读取。 [DllImport("KSJApi.dll")] public static extern int KSJ_TriggerStatusGet(int nChannel,ref KSJ_TRIGGERSTATUS TriggerStatus); [DllImport("KSJApi.dll")] public static extern int KSJ_EmptyFrameBuffer(int nChannel); // 保存为Bmp文件 [DllImport("KSJApi.dll")] public static extern int KSJ_HelperSaveToBmp( byte [] pData, int nWidth, int nHeight, int nBitCount, string pszFileName ); // 保存为Jpg文件 [DllImport("KSJApi.dll")] public static extern int KSJ_HelperSaveToJpg( byte [] pData, int nWidth, int nHeight, int nBitCount, int nQulity, string pszFileName ); [DllImport("KSJApi.dll")] public static extern int KSJ_CaptureSetTimeOut(int nChannel, ref ulong dwTimeOut ); [DllImport("KSJApi.dll")] public static extern int KSJ_CaptureGetTimeOut(int nChannel, ref ulong dwTimeOut ); [DllImport("KSJApi.dll")] public static extern int KSJ_ColorCorrectSet( int nChannel, KSJ_COLORCORRECT ColorCorrect, int nRTarget, int nGTarget, int nBTarget ); [DllImport("KSJApi.dll")] public static extern int KSJ_ColorCorrectGet(int nChannel, ref KSJ_COLORCORRECT ColorCorrect ); [DllImport("KSJApi.dll")] public static extern int KSJ_ProcessContrastSet(int nChannel, int nContrast); [DllImport("KSJApi.dll")] public static extern int KSJ_ProcessContrastGet(int nChannel, ref int nContrast); [DllImport("KSJApi.dll")] public static extern int KSJ_ProcessBrightnessSet(int nChannel, int nBrightness); [DllImport("KSJApi.dll")] public static extern int KSJ_ProcessBrightnessGet(int nChannel, ref int nBrightness); } public class KSJApiC { public delegate void KSJ_PREVIEWCALLBACK(byte[] pImageData, int nWidth, int nHeight, int nBitCount, IntPtr lpContext); [DllImport("KSJApi.dll")] public static extern int KSJ_PreviewSetPos( int nChannel, IntPtr hWnd, int x, int y, int nWidth, int nHeight ); [DllImport("KSJApi.dll")] public static extern int KSJ_PreviewStart( int nChannel, bool bStart ); [DllImport("KSJApi.dll")] public static extern int KSJ_PreviewSetCallback( int nChannel, KSJ_PREVIEWCALLBACK PreviewCallback, IntPtr lpContext ); [DllImport("KSJApi.dll")] public static extern int KSJ_PreviewGetFrameRate(int nChannel, ref float fFrameRate); [DllImport("KSJApi.dll")] public static extern int KSJ_PreviewPause(int nChannel, bool bPause); } }