/// <summary> /// Wait for an expected image /// </summary> /// <param name="acqTicket">Use -1 if Auto-triggering</param> /// <returns></returns> private ICogImage WaitForImage(int acqTicket) { long startTime0 = U.DateTimeNow; ICogImage cogImage = null; int completeTicket, triggerNumber, numPending, numReady = 0; bool busy; do { lock (_lockAcqFifo) { try { _cogAcqFifo.GetFifoState(out numPending, out numReady, out busy); //System.Diagnostics.Debug.WriteLine(string.Format("{0} Acquire numPending={1} numReady={2} busy={3}", Name, numPending, numReady, busy)); if (numReady > 0) { long startTime1 = U.DateTimeNow; cogImage = _cogAcqFifo.CompleteAcquire(acqTicket, out completeTicket, out triggerNumber); long startTime2 = U.DateTimeNow; if (cogImage == null) { U.LogPopup("Unexpected CogImage type = {0}", cogImage.GetType().Name); return(null); } if (cogImage is CogImage8Grey) { CogImage8Grey cogImage8Grey = cogImage as CogImage8Grey; _mConGrey.InputImage = cogImage8Grey; _mConGrey.Run(); _mConGrey.InputImage = null; (cogImage as IDisposable).Dispose(); return(_mConGrey.OutputImage as CogImage8Grey); } else if (cogImage is CogImage24PlanarColor) { CogImage24PlanarColor copyColorImage = null; if (RotateImage == 0) { copyColorImage = new CogImage24PlanarColor((cogImage as CogImage24PlanarColor).ToBitmap()); } else { if (_mOneImageTool.Operators.Count == 0) { switch (RotateImage) { case 90: _ImageRotate.OperationInPixelSpace = CogIPOneImageFlipRotateOperationConstants.Rotate270Deg; break; case -90: case 270: _ImageRotate.OperationInPixelSpace = CogIPOneImageFlipRotateOperationConstants.Rotate270Deg; break; case 180: _ImageRotate.OperationInPixelSpace = CogIPOneImageFlipRotateOperationConstants.Rotate180Deg; break; default: U.LogPopup("Unexpected rotate value ({0}) for camera {1}", RotateImage, Nickname); break; } _mOneImageTool.Operators.Add(_ImageRotate); } _mOneImageTool.InputImage = cogImage; _mOneImageTool.Run(); _mOneImageTool.InputImage = null; copyColorImage = _mOneImageTool.OutputImage as CogImage24PlanarColor; } long startTime3 = U.DateTimeNow; double ms10 = U.TicksToMS(startTime1 - startTime0); double ms21 = U.TicksToMS(startTime2 - startTime1); double ms32 = U.TicksToMS(startTime3 - startTime2); Debug.WriteLine(string.Format("GotImage={0} Complete={1} Rotate={2}", ms10, ms21, ms32)); (cogImage as IDisposable).Dispose(); return(copyColorImage); } else { U.LogPopup("Unexpected CogImage type = {0}", cogImage.GetType().Name); } (cogImage as IDisposable).Dispose(); return(null); } U.SleepWithEvents(10); } catch (Exception ex) { Debug.WriteLine(string.Format("{0} WaitForImage Exception={1}", Name, ex.ToString())); _cogAcqFifo.Flush(); U.LogError(ex, "Error in CompleteAcquire"); System.Threading.Thread.Sleep(100); } } } while (numReady <= 0 && acqTicket >= 0); return(null); }
public string ReadBarcode(int nMode) { ICogImage cogOutputImage; CogImageConvertTool ConvertTool = new CogImageConvertTool(); // Cog2DSymbolTool cog2DSymbol = new Cog2DSymbolTool(); // CogBarcodeTool cog1dSymbol = new CogBarcodeTool(); CogIDTool cogIDTool = new CogIDTool(); CogImageFileTool SaveImage = new CogImageFileTool(); CogIPOneImageTool oneTool = new CogIPOneImageTool(); CogRectangle cogRect = new CogRectangle(); CogIPOneImageEqualize EqulParam = new CogIPOneImageEqualize(); CogIPOneImageMultiplyConstant MultiplyConstant = new CogIPOneImageMultiplyConstant(); MultiplyConstant.ConstantValue = 2; string strReadBarcode = ""; if (BarcodeRect.Width <= 0) { return(strReadBarcode); } if (m_imgScreenShot == null) { return(""); } ConvertTool.InputImage = m_imgScreenShot; ConvertTool.Run(); if (nMode == 1) { oneTool.InputImage = ConvertTool.OutputImage; //oneTool.Operators.Add(EqulParam); oneTool.Operators.Add(MultiplyConstant); oneTool.Run(); cogOutputImage = oneTool.OutputImage;// ConvertTool.OutputImage; } else if (nMode == 2) { oneTool.InputImage = ConvertTool.OutputImage; oneTool.Operators.Add(EqulParam); //oneTool.Operators.Add(MultiplyConstant); oneTool.Run(); cogOutputImage = oneTool.OutputImage;// ConvertTool.OutputImage; } else { cogOutputImage = ConvertTool.OutputImage; } if (BarcodeRect.Width > 0 && BarcodeRect.Height > 0) { cogRect.SetXYWidthHeight(BarcodeRect.X, BarcodeRect.Y, BarcodeRect.Width, BarcodeRect.Height); try { ICogRegion cogRegion; cogRegion = new CogRectangle(); cogRegion.FitToBoundingBox(cogRect); cogIDTool.InputImage = (CogImage8Grey)cogOutputImage; cogIDTool.RunParams.DisableAllCodes(); cogIDTool.RunParams.DataMatrix.Enabled = true; cogIDTool.RunParams.DataMatrix.ProcessControlMetrics = CogIDDataMatrixProcessControlMetricsConstants.None; cogIDTool.Region = cogRegion; cogIDTool.Run(); if (cogIDTool.Results != null && cogIDTool.Results.Count == 1) { strReadBarcode = cogIDTool.Results[0].DecodedData.DecodedString; } /* * if (cog2DSymbol.Pattern.Trained) * cog2DSymbol.Pattern.Untrain(); * cog2DSymbol.InputImage = (CogImage8Grey)cogOutputImage; * cog2DSymbol.Pattern.TrainImage = (CogImage8Grey)cogOutputImage; * cog2DSymbol.SearchRegion = cogRegion;// barcodeRegion; * cog2DSymbol.Pattern.Train(); * if (cog2DSymbol.Pattern.Trained) * { * try * { * cog2DSymbol.SearchRegion = cogRegion;// barcodeRegion; * cog2DSymbol.Run(); * if (cog2DSymbol.Result != null) * strReadBarcode = cog2DSymbol.Result.DecodedString; * * } * catch (Cognex.VisionPro.Exceptions.CogException) * { * * } * } * if (strReadBarcode == "") * { * cog1dSymbol.InputImage = (CogImage8Grey)cogOutputImage; * cog1dSymbol.Region = cogRegion; * cog1dSymbol.Run(); * strReadBarcode = cog1dSymbol.Results.ToString(); * }*/ } catch (Cognex.VisionPro.Exceptions.CogException) { } } return(strReadBarcode); }
private void btnUpload1_Click(object sender, EventArgs e) { try { var cogImageFileTool = new CogImageFileTool(); OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Multiselect = true; openFileDialog.Filter = "All Image Files|*.bmp;*.ico;*.gif;*.jpeg;*.jpg;*.png;*.tif;*.tiff|" + LanguageHelper.GetString("um_AF") + "(*.*)|*.*||"; openFileDialog.AutoUpgradeEnabled = true; openFileDialog.Title = LanguageHelper.GetString("um_SP"); if (openFileDialog.ShowDialog() == DialogResult.OK) { pbLoadingBar1.Visible = true; Camera1ImageQueue = new Queue <CogImage8Grey>(); new Thread(new ThreadStart(delegate() { for (var index = 0; index < openFileDialog.FileNames.Count(); index++) { cogImageFileTool.Operator.Open(openFileDialog.FileNames[index], CogImageFileModeConstants.Read); cogImageFileTool.Run(); if (cogImageFileTool.OutputImage.GetType().Equals(typeof(CogImage24PlanarColor))) { CogImageConvertTool cogImageConvertTool = new CogImageConvertTool(); cogImageConvertTool.InputImage = cogImageFileTool.OutputImage; cogImageConvertTool.Run(); Camera1ImageQueue.Enqueue((CogImage8Grey)cogImageConvertTool.OutputImage); } else { Camera1ImageQueue.Enqueue((CogImage8Grey)cogImageFileTool.OutputImage); } } if (this.InvokeRequired) { this.Invoke(new MethodInvoker(delegate() { pbLoadingBar1.Visible = false; if (Camera1ImageQueue.Count > 0) { btnRun1.Enabled = true; btnRun1.Text = LanguageHelper.GetString("um_Run") + "(" + Camera1ImageQueue.Count.ToString() + ")"; } })); } else { pbLoadingBar1.Visible = false; if (Camera1ImageQueue.Count > 0) { btnRun1.Enabled = true; btnRun1.Text = LanguageHelper.GetString("um_Run") + "(" + Camera1ImageQueue.Count.ToString() + ")"; } } })).Start(); } } catch (Exception ex) { pbLoadingBar1.Visible = false; MessageBox.Show(ex.Message); } }