Beispiel #1
0
        public string ReadQrCode(byte[] content)
        {
            var bitmap = Image.Load(content);
            var reader = new ZXing.ImageSharp.BarcodeReader <Rgba32>();
            var result = reader.Decode(bitmap);

            return(result?.Text ?? "");
        }
        public async Task <IActionResult> DecodeQrCode(IFormFile qrCodeInput, string returnUrl)
        {
            if (!qrCodeInput.IsImage())
            {
                return(await QrCodeError(returnUrl));
            }

            var vm = new ValidatePersonModel()
            {
                ReturnUrl = returnUrl
            };

            using (var bitmap = new System.Drawing.Bitmap(qrCodeInput.OpenReadStream()))
            {
                var image = BitmapResizer.Resize(bitmap, 600, 800);

                var reader = new ZXing.ImageSharp.BarcodeReader <Rgba32>
                {
                    Options = { PossibleFormats = new List <BarcodeFormat> {
                                    BarcodeFormat.QR_CODE
                                } }
                };

                var result = reader.Decode(image);

                if (result == null)
                {
                    _logger.LogError("QR Code is invalid");
                    return(await QrCodeError(returnUrl));
                }

                var user = _userManager.FindByCodeAsync(result.Text);
                if (user == null)
                {
                    _logger.LogError("No user found for QR Code");
                    return(await QrCodeError(returnUrl));
                }

                vm.User = user;
            }

            return(View("ValidatePerson", vm));
        }
Beispiel #3
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"));
        }