如何使用ZXing直接从移动摄像头读取QR码[ASP.Net WebForm]

问题描述 投票:5回答:1

我目前正在开展一个由患者数据库组成的医疗项目。每次将患者添加到记录中时,我使用zxing生成QR码,QR码包含患者的ID。

生成代码如下

 //GENERATE QRCODE
        private void GenerateCode(string patientIdString)
        {           

            var writer = new BarcodeWriter();
            writer.Format = BarcodeFormat.QR_CODE;
            var result = writer.Write(patientIdString);
            string path = Server.MapPath("~/images/" + patientIdString + ".jpg");
            var barcodeBitmap = new Bitmap(result);

            using (MemoryStream memory = new MemoryStream())
            {
                using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite))
                {
                    barcodeBitmap.Save(memory, ImageFormat.Jpeg);
                    byte[] bytes = memory.ToArray();
                    fs.Write(bytes, 0, bytes.Length);
                }
            }
            patientQRCode.Visible = true;
            patientQRCode.ImageUrl = "~/images/"+ patientIdString + ".jpg";
        }

然后在AddPatient功能上调用此方法,该功能完全正常。

在我的扫描页面上,我有两个功能,用户点击在dataTable上查看的患者ID,将其重定向到视图患者页面,或者用户具有使用他们的移动相机的功能。

读取QR码并翻译的代码如下

//READ CODE FROM QR IMAGE
        private void ReadQRCode()
        {
            var reader = new BarcodeReader();
            string filename = Path.Combine(Request.MapPath("~/images/"), "QRImage.jpg");
            //Detatch and decode the barcode inside the bitmap
            var result = reader.Decode(new Bitmap(filename));
            if (result != null)
            {
                lblQRCode.Text = "QR Code : " + result.Text;
            }
        }

我用于移动用户打开相机的方法如下:

        <p class="lead" style="text-align: center"><input class="btn btn-success btn-sm" type="file" accept="image/*" runat="server" capture="camera" /></p>

问题是相机实际上并没有扫描/拍照,它只是作为一个镜头。有没有办法让它读取并转换代码以获取患者ID,然后自动将用户重定向到患者页面?

谢谢你先进的支持

c# asp.net zxing
1个回答
1
投票

我最终启用了WebRTC javascript插件,以启用在手机上使用相机的面板。 (本教程https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API/Taking_still_photos#Using_specific_devices

然后使用此示例启用后置摄像头,因为第一部分仅允许使用前置摄像头。 (https://webrtc.github.io/samples/src/content/devices/input-output/

这给了我想象的图像捕获所需的结果。

然后我使用ZXing创建所需的QR,然后再读取WebRTC摄像头捕获的图像。

我还记得当我尝试在我的手机上运行网站时显示空白屏幕的相机,原来是因为网站没有验证SSL证书,这意味着网站仍然是HTTP而不是HTTPS由于某种原因允许移动设备访问摄像头功能。 (https://www.pluralsight.com/guides/visual-studio-2017-resolving-ssl-tls-connections-problems-with-iis-express

© www.soinside.com 2019 - 2024. All rights reserved.