private void GrabImageTimer(object sender, System.Timers.ElapsedEventArgs e) { // 响应停止请求 if (m_reqStop == 0) { m_reqStop = 1; myTimer.Enabled = false; } if (R3(m_ip).getStatus() == CAM_STATUS.CAM_IS_STARTED && R3(m_ip).isGrabbed()) { CSImgT imgTable = new CSImgT(); if (R3(m_ip).getImageData(imgTable) == CAM_STATUS.All_OK) { Bitmap imgGray; // 读取图片 convert image to show var mode = GetGrabMode(); if (!imgTable.has(mode)) { return; } switch (mode) { case DataNames.SEN: imgGray = BuiltGrayBitmap( imgTable.getDataByte(mode), imgTable.getS_cols(), imgTable.getS_rows() ); break; case DataNames.REF: case DataNames.SCA: case DataNames.REF_CAL: case DataNames.SCA_CAL: case DataNames.REF2: case DataNames.SCA2: case DataNames.REF2_CAL: case DataNames.SCA2_CAL: imgGray = BuiltGrayBitmap( imgTable.getDataByte(mode), imgTable.getR_cols(), imgTable.getR_rows() ); break; case DataNames.RAN: case DataNames.RAN2: imgGray = BuiltGrayBitmap( imgTable.getDataWord(mode), imgTable.getR_cols(), imgTable.getR_rows() ); break; case DataNames.RAN_CAL: case DataNames.RAN2_CAL: imgGray = BuiltGrayBitmap( imgTable.getDataFloat(mode), imgTable.getR_cols(), imgTable.getR_rows() ); break; default: imgTable.Dispose(); return; } // 显示图片 Show Image // Code below will cost a lot of memory. // This is a side effect of C# gc. #if true System.Drawing.Image imgShow = System.Drawing.Image.FromHbitmap(imgGray.GetHbitmap()); pictureBox1.BackgroundImage = imgShow.GetThumbnailImage(pictureBox1.Size.Width, pictureBox1.Size.Height, null, IntPtr.Zero); imgShow.Dispose(); imgGray.Dispose(); imgShow = null; imgGray = null; GC.Collect(); GC.WaitForPendingFinalizers(); #endif lock (m_imgLocker) { m_lastImg._deepCopy(imgTable); } // 响应停止请求 if (m_reqStop == 0) { m_reqStop = 1; myTimer.Enabled = false; } // show ID wFlog(imgTable.get_ID().ToString()); } imgTable.Dispose(); GC.Collect(); GC.WaitForPendingFinalizers(); } }
static bool test_CS_CPP_Graber_thread(bool isPrintToScream = true, int nImg = 5) { bool enable_CPP_Grab_thread = true; // --- test CSR3S. (Please note: "CS" = C_Sharp, "R3S" = Ranger3_Shared_runtime_environment, So "CSR3S" = class of C_Sharp_Ranger3_Shared_runtime_environment.) String logPath = "D:\\log.txt"; String ctiPath = ""; bool isPrintLog = isPrintToScream; bool isWriteLog = true; GenICam3D r3s = new GenICam3D(logPath, ctiPath, isPrintLog, isWriteLog); String[] IPs = r3s.getConDevListIP(); String[] Nas = r3s.getConDevList(); var scanRes = r3s.scanDevice(); // --- test CSR3. (Please note: "CS" = C_Sharp, "R3" = Ranger3, So "CSR3" = class of C_Sharp_Ranger3.) ///String mac = "0006770c69e1"; ///Ranger3 r3 = new Ranger3(mac, false); Ranger3 r3 = new Ranger3(IPs[0], true); if (r3.isReady() != CAM_STATUS.All_OK) { unit_test_print(isPrintToScream, "Camera is not ready!"); return(false); } /// If program might last time, camera status will be incorrect. re-conencting to make it ready! ///Debug tips. r3.connectCamera(false); r3.disconnectCamera(); var camStatus = r3.connectCamera(enable_CPP_Grab_thread); if (camStatus != CAM_STATUS.All_OK) { unit_test_print(isPrintToScream, "Camera connecting failed!"); return(false); } /// test CS getting paramters info if (r3.updateParamtersInfo() == false) { return(false); } String[] CategoryList = r3.paramCategoryList(); String[] FullNameList = r3.paramFullNameList(); int fnId = 0; unit_test_print(isPrintToScream, ""); unit_test_print(isPrintToScream, "--------------"); foreach (String cate in CategoryList) { unit_test_print(isPrintToScream, "|"); unit_test_print(isPrintToScream, "---- Cate: {0}", cate); String[] categries = r3.paramFullNameOfCategory(cate); foreach (String fn in categries) { unit_test_print(isPrintToScream, " |"); unit_test_print(isPrintToScream, " --- FullName: {0}", fn); unit_test_print(isPrintToScream, " |"); unit_test_print(isPrintToScream, " --- CurrentValue: {0}", r3.paramCurrentValue(fn)); unit_test_print(isPrintToScream, " |"); unit_test_print(isPrintToScream, " --- Optional values:"); String[] opt = r3.paramOptionalValues(fn, ref fnId); foreach (String sub in opt) { unit_test_print(isPrintToScream, " |"); unit_test_print(isPrintToScream, " ---- {0}", sub); } } } // --- test CSR3 setting paramters info ///r3.setParameterValue("DeviceScanType", "Linescan3D"); /// 3D Range image r3.setParameterValue("DeviceScanType", "Areascan"); /// 2D Sensor image // --- Memory using check: X Mb before starting camStatus = r3.startCamera(); if (camStatus != CAM_STATUS.All_OK) { unit_test_print(isPrintToScream, "Camera starting failed!"); return(false); } // --- test CSR3 acquisition Thread.Sleep(1000); int i = 0; #if true while (true) { if (r3.isGrabbed()) { // --- test CS ImgT CSImgT imgT = new CSImgT(); camStatus = r3.getImageData(imgT); if (camStatus != CAM_STATUS.All_OK) { unit_test_print(isPrintToScream, "Camera get data failed!"); return(false); } test_CS_CPP_Graber_thread_process_img(isPrintToScream, imgT); // --- Important to list these code here and collect memory allocating to imgT. imgT.Dispose(); GC.Collect(); GC.WaitForFullGCComplete(); ++i; } else { Thread.Sleep(50); } if (i == nImg) { break; } } #else while (true) { Thread.Sleep(50); ++i; Console.WriteLine("i = {0}", i); if (i == nImg) { break; } } #endif // --- Memory using check: around X Mb after stopping. camStatus = r3.stopCamera(); if (camStatus != CAM_STATUS.All_OK) { unit_test_print(isPrintToScream, "Camera stopping failed!"); return(false); } camStatus = r3.disconnectCamera(); if (camStatus != CAM_STATUS.All_OK) { unit_test_print(isPrintToScream, "Camera disconnecting failed!"); return(false); } // --- delete variable. r3s.Dispose(); return(true); }