//called when data for any output pin is requested
		public void Evaluate(int SpreadMax)
		{
			FOutput.SliceCount = 1;
			IStatus.SliceCount = 1;
			if ((FEnabled[0]) && (FStreamIn[0].Length > 16)) {

				var bmp = Bitmap.FromStream(FStreamIn[0]);
				var reader = new ZXing.BarcodeReader { TryHarder = FTryHard[0] };
				var results = reader.DecodeMultiple((Bitmap)bmp);

				if (results != null) {
					for (int i = 0; i < results.Length; i++) {
						var result = results[i];
						FOutput.SliceCount = results.Length;
						FPoints.SliceCount = results.Length;
						IStatus.SliceCount = results.Length;

						FOutput[i] = ((ZXing.Result)(result)).Text;
						FPoints[i].ResizeAndDismiss(0);
						//FLogger.Log(LogType.Debug,results.Length.ToString());

						for (int j = 0; j < result.ResultPoints.Length; j++) {
							var v = new Vector2D((2 * result.ResultPoints[j].X / bmp.Width) - 1, (-2 * result.ResultPoints[j].Y / bmp.Height) + 1);
							FPoints[i].Add(v);
						}
						IStatus[i] = 1;
					}
				} else {
					FOutput[0] = "";
					FPoints[0].SliceCount = 0;
					FPoints[0].ResizeAndDismiss(0);
					IStatus[0] = 0;
				}
			}
		}
Example #2
0
        //called when data for any output pin is requested
        public void Evaluate(int SpreadMax)
        {
            FOutput.SliceCount = 1;
            IStatus.SliceCount = 1;
            if ((FEnabled[0]) && (FStreamIn[0].Length > 16))
            {
                var bmp    = Bitmap.FromStream(FStreamIn[0]);
                var reader = new ZXing.BarcodeReader {
                    TryHarder = FTryHard[0]
                };
                var results = reader.DecodeMultiple((Bitmap)bmp);

                if (results != null)
                {
                    for (int i = 0; i < results.Length; i++)
                    {
                        var result = results[i];
                        FOutput.SliceCount = results.Length;
                        FPoints.SliceCount = results.Length;
                        IStatus.SliceCount = results.Length;

                        FOutput[i] = ((ZXing.Result)(result)).Text;
                        FPoints[i].ResizeAndDismiss(0);
                        //FLogger.Log(LogType.Debug,results.Length.ToString());

                        for (int j = 0; j < result.ResultPoints.Length; j++)
                        {
                            var v = new Vector2D((2 * result.ResultPoints[j].X / bmp.Width) - 1, (-2 * result.ResultPoints[j].Y / bmp.Height) + 1);
                            FPoints[i].Add(v);
                        }
                        IStatus[i] = 1;
                    }
                }
                else
                {
                    FOutput[0]            = "";
                    FPoints[0].SliceCount = 0;
                    FPoints[0].ResizeAndDismiss(0);
                    IStatus[0] = 0;
                }
            }
        }
Example #3
0
        private DecodeResult CallDecode(BitmapLuminanceSource imageAsLuminance, long singleStart)
        {
            var tempResults = new List <Result>();

            if (_config.TryMultipleBarcodeTypes)
            {
                tempResults = barcodeReader.DecodeMultiple(imageAsLuminance)?.ToList() ?? new List <Result>();
            }
            else
            {
                var result = barcodeReader.Decode(imageAsLuminance);
                tempResults.Add(result);
            }

            var mappedResult = new DecodeResult();

            if (tempResults.Count > 0)
            {
                mappedResult = new DecodeResult(tempResults[0]);
            }

            // search for the correct one (that works) if multiple results
            foreach (var item in tempResults)
            {
                if (item == null)
                {
                    continue;
                }

                var encodedData = StandardOutputTransformer.RetrieveNameAndPages(item.Text);
                if (!string.IsNullOrWhiteSpace(encodedData.Item1) && encodedData.Item2 != 0 && encodedData.Item3 != 0)
                {
                    mappedResult = new DecodeResult(item);
                    break;
                }
            }

            return(mappedResult);
        }
Example #4
0
        private int Decode(Bitmap inputBitmap, bool tryMultipleBarcodes, IList <BarcodeFormat> possibleFormats, int x, int y, float fXMultiplier, float fYMultiplier, out Rectangle rectFound)
        {
            rectFound = new Rectangle();
            lastResults.Clear();
            IList <Result> results         = null;
            var            previousFormats = barcodeReader.Options.PossibleFormats;

            if (possibleFormats != null)
            {
                barcodeReader.Options.PossibleFormats = possibleFormats;
            }

            if (tryMultipleBarcodes)
            {
                results = barcodeReader.DecodeMultiple(inputBitmap);
            }
            else
            {
                var result = barcodeReader.Decode(inputBitmap);
                if (result != null)
                {
                    if (results == null)
                    {
                        results = new List <Result>();
                    }
                    results.Add(result);
                }
            }

            barcodeReader.Options.PossibleFormats = previousFormats;

            if (results == null)
            {
                return(0);
            }

            return(results.Count);
        }
Example #5
0
        private List<string> QRDecodeMulti(Bitmap qrImage, bool showMask = true )
        {
            using (qrImage)
            {
            #if DEBUG
                qrImage.Save("screensnap.png");
            #endif
                //var br = new BarcodeReader( null,
                //                            bitmap => new BitmapLuminanceSource(bitmap),
                //                            luminance => new GlobalHistogramBinarizer(luminance));
                var br = new BarcodeReader();
                setDecodeOptions(br);

                try
                {
                    var results = br.DecodeMultiple(qrImage);
                    if (results != null)
                    {
                        var textList = new List<string>();
                        foreach (var result in results)
                        {
                            if ( showMask ) ShowQRCodeMask( result, qrImage );
                            textList.Add(result.Text);
                        }
                        //SystemSounds.Asterisk.Play();
                        SystemSounds.Beep.Play();
                        return (textList);
                    }
                    else
                    {
            #if DEBUG
                        MessageBox.Show(this, "Failed to find Code!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            #endif
                        SystemSounds.Exclamation.Play();
                        return (new List<string>());
                    }
                }
                catch
                {
                    SystemSounds.Exclamation.Play();
                    return (new List<string>());
                }
            }
        }
Example #6
0
        public void Evaluate(int SpreadMax)
        {
            if (FTextureIn.IsChanged)
            {
                for (int i = 0; i < FOutput.SliceCount; i++)
                {
                    FOutput[i].SliceCount = 0;
                    FFormat[i].SliceCount = 0;
                    FStatus[i].SliceCount = 0;
                }


                FOutput.SliceCount = 1;
                FFormat.SliceCount = 1;
                FStatus.SliceCount = 1;
            }

            if (this.FTextureIn.IsConnected)
            {
                RenderRequest?.Invoke(this, FHost);

                if (AssignedContext == null)
                {
                    FStatus.SliceCount = 0; return;
                }
                //Do NOT cache this, assignment done by the host

                var context = AssignedContext;

                FOutput.SliceCount = SpreadMax;
                FFormat.SliceCount = SpreadMax;
                FStatus.SliceCount = SpreadMax;

                for (int i = 0; i < SpreadMax; i++)
                {
                    if (FTextureIn[i].Contains(context) && FRead[i])
                    {
                        FOutput[i].SliceCount = 0;
                        FFormat[i].SliceCount = 0;
                        FStatus[i].SliceCount = 0;

                        var reader = new ZXing.BarcodeReader();

                        try
                        {
                            MemoryStream memoryStream = new MemoryStream();
                            Texture2D.ToStream(context.CurrentDeviceContext, FTextureIn[i][context].Resource, SlimDX.Direct3D11.ImageFileFormat.Bmp, memoryStream);

                            Bitmap bmp = new Bitmap(memoryStream);

                            var results = reader.DecodeMultiple(bmp);

                            if (results != null)
                            {
                                var count = results.Length;
                                FOutput[i].SliceCount = count;
                                FFormat[i].SliceCount = count;
                                FStatus[i].SliceCount = count;

                                for (int j = 0; j < count; j++)
                                {
                                    var result = results[j];
                                    FOutput[i][j] = result.Text;
                                    FFormat[i][j] = result.BarcodeFormat.ToString();
                                    FStatus[i][j] = "Success";
                                }
                            }
                            else
                            {
                                FOutput[i].SliceCount = 1;
                                FFormat[i].SliceCount = 1;
                                FStatus[i].SliceCount = 1;
                                FOutput[i][0]         = "";
                                FFormat[i][0]         = "";
                                FStatus[i][0]         = "No Barcode Found";
                            }
                        }
                        catch (Exception ex)
                        {
                            FLogger.Log(ex);
                            FOutput[i].SliceCount = 1;
                            FFormat[i].SliceCount = 1;
                            FStatus[i].SliceCount = 1;
                            FOutput[i][0]         = "";
                            FFormat[i][0]         = "";
                            FStatus[i][0]         = ex.Message;
                        }
                    }
                }
            }
            else
            {
                FOutput.SliceCount = 1;
                FFormat.SliceCount = 1;
                FStatus.SliceCount = 1;
            }
        }
Example #7
0
        /// <summary>
        /// Performs the analysis on the image
        /// </summary>
        public void Analyze(bool thorough = true)
        {
            if (this.m_disposed)
            {
                throw new ObjectDisposedException("ImageProcessor");
            }

            if (this.IsScannable)
            {
                return;
            }

            LuminanceSource source    = new BitmapLuminanceSource(this.m_bitmap);
            var             binarizer = new HybridBinarizer(source);
            var             binBitmap = new BinaryBitmap(binarizer);

            // Try to extract the form data
            var barReader = new ZXing.BarcodeReader();

            barReader.AutoRotate              = true;
            barReader.Options.TryHarder       = thorough;
            barReader.Options.PossibleFormats = new List <BarcodeFormat>()
            {
                BarcodeFormat.CODE_128
            };

            this.m_barcodeResults = barReader.DecodeMultiple(source);

            // Look for barcode markers if possible
            if (this.m_barcodeResults != null)
            {
                this.m_markerCodes = this.m_barcodeResults.Where(o => o.Text.StartsWith("OMR:")).ToArray();
            }
            this.IsScannable = true;
            // Get the template data
            var markerCode = this.m_markerCodes == null ? null : this.m_markerCodes.FirstOrDefault(o => o.Text.StartsWith("OMR:TL") || o.Text.StartsWith("OMR:ID"));

            // Get the guiding points by circles
            GrayscaleY grayFilter      = new GrayscaleY();
            Threshold  thresholdFilter = new Threshold(127);
            Invert     invertFilter    = new Invert();

            using (Bitmap searchImage = invertFilter.Apply(thresholdFilter.Apply(grayFilter.Apply(this.m_bitmap))))
            {
                // Blobs
                BlobCounter blobCounter = new BlobCounter();
                blobCounter.FilterBlobs = true;
                blobCounter.MinHeight   = 30;
                blobCounter.MinWidth    = 30;

                // Check for circles
                blobCounter.ProcessImage(searchImage);
                Blob[]              blobs         = blobCounter.GetObjectsInformation();
                SimpleShapeChecker  shapeChecker  = new SimpleShapeChecker();
                List <AForge.Point> controlPoints = new List <AForge.Point>();
                int currentCheck = 45;
                while (currentCheck-- > 20 && controlPoints.Count != 4)
                {
                    controlPoints.Clear();
                    // Get the positions
                    foreach (var blob in blobs)
                    {
                        AForge.Point center = new AForge.Point();
                        float        radius = 0;

                        if (shapeChecker.IsCircle(blobCounter.GetBlobsEdgePoints(blob), out center, out radius) &&
                            (radius * 2) >= currentCheck)

                        {
                            controlPoints.Add(center);
                        }
                    }
                }

                // Control points
                this.IsScannable &= controlPoints.Count == 4;
                if (!this.IsScannable)
                {
                    return;
                }

                // Now set markers
                this.m_topLeft     = controlPoints[0]; //new AForge.Point(this.m_bitmap.Width + 10, this.m_bitmap.Height + 10);
                this.m_topRight    = controlPoints[1];
                this.m_bottomLeft  = controlPoints[2];
                this.m_bottomRight = controlPoints[3];

                // Find the right most bubble
                float rightMost = controlPoints.Select(o => o.X).Max(),
                      leftMost  = controlPoints.Select(o => o.X).Min();
                // Organize those that are left/right
                AForge.Point[] lefties = controlPoints.Where(o => o.X < leftMost + (rightMost - leftMost) / 2).ToArray(),
                righties = controlPoints.Where(o => o.X > leftMost + (rightMost - leftMost) / 2).ToArray();

                // HACK:
                if (lefties[0].Y < lefties[1].Y)
                {
                    this.m_topLeft    = lefties[0];
                    this.m_bottomLeft = lefties[1];
                }
                else
                {
                    this.m_topLeft    = lefties[1];
                    this.m_bottomLeft = lefties[0];
                }

                // HACK:
                if (righties[0].Y < righties[1].Y)
                {
                    this.m_topRight    = righties[0];
                    this.m_bottomRight = righties[1];
                }
                else
                {
                    this.m_topRight    = righties[1];
                    this.m_bottomRight = righties[0];
                }
            }

            if (!this.IsScannable)
            {
                return;
            }

            // Get the template data
            if (this.m_markerCodes != null && markerCode != null)
            {
                String[] templateData = markerCode.Text.Split(':');
                if (templateData.Length > 2)
                {
                    this.TemplateName = templateData[2];
                    if (templateData.Length > 3)
                    {
                        this.Parameters = templateData.Skip(3).ToArray();
                    }
                }
            }
        }
Example #8
0
      private Result[] decodeMulti(Uri uri, string originalInput, IDictionary<DecodeHintType, object> hints)
      {
         Bitmap image;
         try
         {
            image = (Bitmap)Bitmap.FromFile(uri.LocalPath);
         }
         catch (Exception)
         {
            throw new FileNotFoundException("Resource not found: " + uri);
         }

         using (image)
         {
            LuminanceSource source;
            if (config.Crop == null)
            {
               source = new BitmapLuminanceSource(image);
            }
            else
            {
               int[] crop = config.Crop;
               source = new BitmapLuminanceSource(image).crop(crop[0], crop[1], crop[2], crop[3]);
            }
            if (config.DumpBlackPoint)
            {
               var bitmap = new BinaryBitmap(new HybridBinarizer(source));
               dumpBlackPoint(uri, image, bitmap, source);
            }

            var reader = new BarcodeReader {AutoRotate = config.AutoRotate};
            foreach (var entry in hints)
               reader.Options.Hints.Add(entry.Key, entry.Value);
            Result[] results = reader.DecodeMultiple(source);
            if (results != null && results.Length > 0)
            {
               if (config.Brief)
               {
                  Console.Out.WriteLine(uri + ": Success");
               }
               else
               {
                  foreach (var result in results)
                  {
                     ParsedResult parsedResult = ResultParser.parseResult(result);
                     var resultString = originalInput + " (format: " + result.BarcodeFormat + ", type: " + parsedResult.Type + "):" + Environment.NewLine;
                     for (int i = 0; i < result.ResultPoints.Length; i++)
                     {
                        ResultPoint rp = result.ResultPoints[i];
                        Console.Out.WriteLine("  Point " + i + ": (" + rp.X + ',' + rp.Y + ')');
                     }
                     resultString += "Raw result:" + Environment.NewLine + result.Text + Environment.NewLine;
                     resultString += "Parsed result:" + Environment.NewLine + parsedResult.DisplayResult + Environment.NewLine;

                     Console.Out.WriteLine(resultString);
                     ResultString = resultString;
                  }
               }
               return results;
            }
            else
            {
               var resultString = originalInput + ": No barcode found";
               Console.Out.WriteLine(resultString);
               ResultString = resultString;
            }
            return null;
         }
      }