using System; using System.Collections.Generic; using System.IO; using System.IO.Ports; using System.Linq; using System.Reflection; using System.Runtime.InteropServices; using System.Text; using static IDViewerDemo.IDViewerDefines; using static IDViewerDemo.IDViewerSDK; namespace IDViewerDemo { delegate void IDViewerCallBack(IntPtr device, IntPtr image, IntPtr decodeInfoHandel); class Program { static string filepath = "D:\\text.bmp"; static void Main(string[] args) { int option = 0; long result = IDViewerSDK.IDVIEWER_Init_(); while (0 == result) { ClearScreen(); Console.WriteLine("0) 退出程序"); Console.WriteLine("1) 读码器操作"); if (GetInputOption(ref option) == false) continue; switch (option) { case 0: return; case 1: MenuIDViewer(); break; default: break; } } } static void MenuIDViewer() { int option = 0; while (true) { ClearScreen(); Console.WriteLine("0) 返回上一级"); Console.WriteLine("1) 扫描所有读码器"); Console.WriteLine("2) 通过SN号连接读码器"); if (GetInputOption(ref option) == false) continue; switch (option) { case 0: return; case 1: DiscoveryIDViewer(); system("pause"); break; case 2: DiscoveryIDViewer_SN(); system("pause"); break; default: break; } } } static void DiscoveryIDViewer() { IntPtr deviceList = IDVIEWER_DiscoveryDevices_(); long length = IDVIEWER_GetDevicesLength_(deviceList); Console.WriteLine("获取到读码器个数为:" + length); IDDeviceInfo info = new IDDeviceInfo(); for (uint i = 0; i < length; i++) { long Inforesult = IDVIEWER_SelectIDDeviceInfo_(deviceList, ref info, i); //Console.WriteLine(Inforesult); Console.WriteLine("*******************************************"); if (i >= 0) Console.WriteLine("index:" + i); Console.WriteLine("networkIP:" + new string(info.networkIP)); Console.WriteLine("networkMask:" + new string(info.networkMask)); Console.WriteLine("networkGateway:" + new string(info.networkGateway)); Console.WriteLine("cameraIP:" + new string(info.cameraIP)); Console.WriteLine("cameraSN:" + new string(info.cameraSN)); Console.WriteLine("cameraType:" + new string(info.cameraType)); } } static void DiscoveryIDViewer_SN() { IntPtr device = IntPtr.Zero; IDDeviceInfo info = new IDDeviceInfo(); Console.WriteLine("请输入SN号"); String serialNum = Console.ReadLine(); long result = IDVIEWER_CreateDeviceBySN_(serialNum, ref device); if (device != IntPtr.Zero) { int option = 0; while (true) { ClearScreen(); Console.WriteLine("0) 返回上一级"); Console.WriteLine("1) 打开"); Console.WriteLine("2) 运行"); Console.WriteLine("3) 连接状态"); Console.WriteLine("4) 关闭"); if (GetInputOption(ref option) == false) continue; if (option == 0) { return; } if (option < 1 || option > 10) { continue; } switch (option) { case 1: IDDevice_Open(device); break; case 2: Device_StartRunning(device); break; case 3: Device_IsOpen(device); break; case 4: IDDevice_Close(device); break; default: break; } system("pause"); } } } static IDViewerCallBack callbackDelegate; static IDDecodeInfo decodeInfo; public static void CallBack(IntPtr device, IntPtr image, IntPtr decodeInfoHandel) { IDDecodeInfo info = new IDDecodeInfo(); IDCallback_GetIDDecodeInfo_A(decodeInfoHandel, ref info); //ROI解码信息数组的长度 获取到ROI的解码数量 if (info.decodeStatus) { for (uint roi = 0; roi < info.IDDecodeROIInfoLength; roi++) { IDDecodeROIInfo IDpecodeROIInfo = IDCallback_GetIDDecodeROIInfo(decodeInfoHandel, 0); if (IDpecodeROIInfo.decodeReusltLength > 0) { IDDecodeResult IDpecodeResult = IDCallback_GetIDDecodeResult(decodeInfoHandel, roi, 0); if (IDpecodeResult.dmResultsLength != 0) { for (uint dmSuccessNumber = 0; dmSuccessNumber < IDpecodeResult.dmResultsLength; dmSuccessNumber++) { IDDMCodeResult DMBarCodeResultData = new IDDMCodeResult(); long a = IDCallback_GetIDDMCodeResult(decodeInfoHandel, roi, 0, dmSuccessNumber, ref DMBarCodeResultData); Console.WriteLine("ROI:" + (roi + 1)); Console.WriteLine("SuccessNumber:" + (dmSuccessNumber + 1)); Console.WriteLine("subType:" + new string(DMBarCodeResultData.subType)); Console.WriteLine("codeString: " + new string(DMBarCodeResultData.codeString)); } } else if (IDpecodeResult.qrResultsLength != 0) { for (uint qrSuccessNumber = 0; qrSuccessNumber < IDpecodeResult.qrResultsLength; qrSuccessNumber++) { IDQRCodeResult QRBarCodeResultData = new IDQRCodeResult(); long a = IDCallback_GetID_QR_CODE_Result(decodeInfoHandel, roi, 0, qrSuccessNumber, ref QRBarCodeResultData); Console.WriteLine("ROI:" + (roi + 1)); Console.WriteLine("SuccessNumber:" + (qrSuccessNumber + 1)); Console.WriteLine("subType:" + new string(QRBarCodeResultData.subType)); Console.WriteLine("codeString: " + new string(QRBarCodeResultData.codeString)); } } else if (IDpecodeResult.pdf417ResultsLength != 0) { for (uint pdf417SuccessNumber = 0; pdf417SuccessNumber < IDpecodeResult.qrResultsLength; pdf417SuccessNumber++) { IDPDF417CodeResult PDF417BarCodeResultData = new IDPDF417CodeResult(); long a = IDCallback_GetIDPDF417CodeResult(decodeInfoHandel, roi, 0, pdf417SuccessNumber, ref PDF417BarCodeResultData); Console.WriteLine("ROI:" + (roi + 1)); Console.WriteLine("SuccessNumber:" + (pdf417SuccessNumber + 1)); Console.WriteLine("subType:" + new string(PDF417BarCodeResultData.subType)); Console.WriteLine("codeString: " + new string(PDF417BarCodeResultData.codeString)); } } else if (IDpecodeResult.barcodeResultsLength != 0) { for (uint barcodeSuccessNumber = 0; barcodeSuccessNumber < IDpecodeResult.qrResultsLength; barcodeSuccessNumber++) { IDBarCodeResult IDBarCodeResultData = new IDBarCodeResult(); long a = IDCallback_GetIDBarCodeResult(decodeInfoHandel, roi, 0, barcodeSuccessNumber, ref IDBarCodeResultData); Console.WriteLine("ROI:" + (roi + 1)); Console.WriteLine("SuccessNumber:" + (barcodeSuccessNumber + 1)); Console.WriteLine("codeString: " + new string(IDBarCodeResultData.codeString)); } } } } } } static void Device_StartRunning(IntPtr device) { IDDevice_StartRunning(device,1); callbackDelegate = new IDViewerCallBack(CallBack); IntPtr functionPointer = Marshal.GetFunctionPointerForDelegate(callbackDelegate); IDCallback_ResultNotify_(device, functionPointer); GC.KeepAlive(callbackDelegate); } static void Device_IsOpen(IntPtr device) { long rel = IDDevice_IsOpen(device); if (rel == 0) { Console.WriteLine("读码器未打开"); } else { Console.WriteLine("读码器已打开"); } } static bool GetInputOption(ref int option) { int op; try { op = int.Parse(Console.ReadLine()); } catch { return false; } option = op; return true; } static void ClearScreen() { system("cls"); Console.WriteLine("*******************************************"); Console.WriteLine("* IDViewer Demo *"); Console.WriteLine("*******************************************"); } [DllImport("msvcrt.dll", SetLastError = false, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] private extern static void system(string command); } }