使用C#实现自动核验健康码:(1)二维码识别

本文介绍了一种利用计算机自动识别健康码的方法,以减少人工核验带来的人员拥堵和交叉感染风险。通过创建WinForm程序,结合AForge.Video.DirectShow和ZXing.Net库,实现摄像头扫描和二维码识别。程序能识别健康码颜色,并显示对应状态。尽管目前只能识别颜色,但未来还需识别更新时间以确保健康码有效性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

因为疫情的原因,无论是进入商场还是医院、车站,都需要出示健康码。

现在基本都是采取人工方式核验健康码,看到绿码就通过,否则就禁止进入。

但是,单靠人工核验健康码容易造成人员拥堵,增加病毒交叉感染的风险,其实完全可以使用计算机来实现自动核验

原理

如图所示,健康码其实就是个二维码,里面存储了健康码相关信息。

因此,只需通过摄像头扫描手机界面,识别出手机上的二维码即可。

实现

创建一个WinForm程序,添加下列控件:

  • button 开启摄像头

  • pictureBox 显示摄像头图像

  • time 定时识别摄像头图像,频率设为100

  • label 显示健康码状态

1. 开启摄像头

添加nuget包AForge.Video.DirectShow,设置button的Click事件:

 
  1. VideoCaptureDevice _camera;

  2. private void button1_Click(object sender, EventArgs e)

  3. {

  4.     _camera = new VideoCaptureDevice(new FilterInfoCollection(FilterCategory.VideoInputDevice)[0].MonikerString);

  5.     _camera.NewFrame += camera_NewFrame;

  6.     _camera.Start();

  7.     timer1.Enabled = true;

  8. }

  9. private void camera_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)

  10. {

  11.     //将摄像头每帧图像显示到pictureBox

  12.     pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();       

  13. }

2. 识别二维码

引用nuget包ZXing.Net,在timer的Tick事件中识别二维码:

 
  1. private void timer1_Tick(object sender, EventArgs e)

  2. {

  3.     if (pictureBox1.Image != null)

  4.     {

  5.         var img = (Bitmap)pictureBox1.Image.Clone();

  6.         var barcodeReader = new BarcodeReader();

  7.         var result = barcodeReader.Decode(img);

  8.         if (result != null)

  9.         {

  10.             var healthCode = JsonConvert.DeserializeAnonymousType(result.Text,

  11.                  new { Color = "" });

  12.             if (healthCode != null)

  13.             {

  14.                 var color = healthCode.Color;

  15.                 if (color == "green")

  16.                 {

  17.                     label1.Text = "绿码";

  18.                     label1.ForeColor = Color.Green;

  19.                 }

  20.                 else if (color == "red")

  21.                 {

  22.                     label1.Text = "红码";

  23.                     label1.ForeColor = Color.Red;

  24.                 }

  25.                 else if (color == "yellow")

  26.                 {

  27.                     label1.Text = "黄码";

  28.                     label1.ForeColor = Color.Yellow;

  29.                 }

  30.                 else

  31.                 {

  32.                     label1.Text = "异常";

  33.                 }

  34.             }

  35.         }

  36.     }

  37. }

健康码的内容是一个json字符串,其中Color属性代表健康码状态。

3. 运行效果

运行程序,点击“开启摄像头”,可以正常识别:

结论

健康码的内容不包含时间,因此下一步还需要把更新时间从图片中识别出来,保证是最新的健康码。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值