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



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

提前感谢您的支持

我最终启用了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)

最新更新