예제 #1
0
        public static string ParseImageViaSkia(byte[] imageBytes)
        {
            ZXing.SkiaSharp.BarcodeReader reader = new ZXing.SkiaSharp.BarcodeReader();

            reader.Options.TryHarder = true;

            using (SKBitmap bitmap = SKBitmap.Decode(imageBytes))
            {
                return(reader.Decode(bitmap)?.Text);
            }
        }
예제 #2
0
        public async Task <IActionResult> DecodeFiles(ICollection <IFormFile> files)
        {
            ViewData["TextAreaResult"] = "No result.";

            foreach (var formFile in files)
            {
                if (formFile.Length > 0)
                {
                    using (var stream = formFile.OpenReadStream())
                    {
                        try
                        {
                            using (var image = SixLabors.ImageSharp.Image.Load(stream))
                            {
                                var reader = new ZXing.ImageSharp.BarcodeReader <SixLabors.ImageSharp.Rgba32>();
                                var result = reader.Decode(image);
                                ViewData["TextAreaResult"] = "ImageSharp: " + result?.Text;
                            }
                        }
                        catch (Exception exc)
                        {
                            ViewData["TextAreaResult"] = "ImageSharp: " + exc.Message;
                        }

                        try
                        {
                            stream.Seek(0, SeekOrigin.Begin);
                            using (var openCVImage = OpenCvSharp.Mat.FromStream(stream, OpenCvSharp.ImreadModes.GrayScale))
                            {
                                var openCVReader = new ZXing.OpenCV.BarcodeReader();
                                var openCVResult = openCVReader.Decode(openCVImage);
                                ViewData["TextAreaResult"] = ViewData["TextAreaResult"] + "OpenCV: " + openCVResult?.Text;
                            }
                        }
                        catch (Exception exc)
                        {
                            ViewData["TextAreaResult"] = ViewData["TextAreaResult"] + "OpenCV: " + exc.Message;
                        }

                        try
                        {
                            stream.Seek(0, SeekOrigin.Begin);
                            using (var coreCompatImage = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromStream(stream))
                            {
                                var coreCompatReader = new ZXing.CoreCompat.System.Drawing.BarcodeReader();
                                var coreCompatResult = coreCompatReader.Decode(coreCompatImage);
                                ViewData["TextAreaResult"] = ViewData["TextAreaResult"] + "CoreCompat.System.Drawing: " + coreCompatResult?.Text;
                            }
                        }
                        catch (Exception exc)
                        {
                            ViewData["TextAreaResult"] = ViewData["TextAreaResult"] + "CoreCompat.System.Drawing: " + exc.Message;
                        }

                        try
                        {
                            stream.Seek(0, SeekOrigin.Begin);
                            using (var magickImage = new ImageMagick.MagickImage(stream))
                            {
                                var magickReader = new ZXing.Magick.BarcodeReader();
                                var magickResult = magickReader.Decode(magickImage);
                                ViewData["TextAreaResult"] = ViewData["TextAreaResult"] + "Magick: " + magickResult?.Text;
                            }
                        }
                        catch (Exception exc)
                        {
                            ViewData["TextAreaResult"] = ViewData["TextAreaResult"] + "Magick: " + exc.Message;
                        }

                        try
                        {
                            stream.Seek(0, SeekOrigin.Begin);
                            using (var skiaImage = SkiaSharp.SKBitmap.Decode(stream))
                            {
                                var skiaReader = new ZXing.SkiaSharp.BarcodeReader();
                                var skiaResult = skiaReader.Decode(skiaImage);
                                ViewData["TextAreaResult"] = ViewData["TextAreaResult"] + "SkiaSharp: " + skiaResult?.Text;
                            }
                        }
                        catch (Exception exc)
                        {
                            ViewData["TextAreaResult"] = ViewData["TextAreaResult"] + "SkiaSharp: " + exc.Message;
                        }
                    }
                }
            }

            ViewData["Message"] = "Your decoding page.";

            return(View("Decode"));
        }