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

时间:2019-04-23 14:27:53

标签: c# asp.net zxing

我目前正在从事一个由患者数据库组成的医疗项目。每当将患者添加到记录中时,我都会使用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,然后自动将用户重定向到患者页面?

非常感谢您的支持

1 个答案:

答案 0 :(得分: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允许移动设备访问摄像头功能。 (https://www.pluralsight.com/guides/visual-studio-2017-resolving-ssl-tls-connections-problems-with-iis-express

相关问题