如何在IE中打开USB摄像头扫描条形码

由于IE不支持WebRTC,本文介绍了一种通过本地Node.js服务获取USB摄像头数据并在IE浏览器中显示的方法,同时增加了条形码扫描功能。详细步骤包括C/C++的Node.js条形码模块编译、通过Node.js在IE中实现实时扫描条形码,并提供了相关源码链接。
摘要由CSDN通过智能技术生成

IE不支持WebRTC,所以没有办法通过JS接口在浏览器中直接访问USB摄像头。解决的方法就是通过本地启动一个服务去获取摄像头数据,然后发送到IE的web页面上,通过img元素不断刷新来显示,这个在上一篇文章中已经说过。这篇文章是基于上一篇里Node.js的代码,增加条形码扫描功能。

基于C/C++的Node.js条形码模块编译

C/C++的封装代码在https://github.com/Dynamsoft/nodejs-barcode。

用于解码的接口是decodeBufferAsync()。以下是相关的C/C++代码:

void DecodeBufferAsync(const FunctionCallbackInfo<Value>&amp; args) {
   
    if (!createDBR()) {
   return;}
    Isolate* isolate = Isolate::GetCurrent();
    Local<Context> context = isolate->GetCurrentContext();
 
    // get arguments
    unsigned char* buffer = (unsigned char*) node::Buffer::Data(args[0]); // file stream
    int width = args[1]->Int32Value(context).ToChecked();   // image width
    int height = args[2]->Int32Value(context).ToChecked();  // image height
    int stride = args[3]->Int32Value(context).ToChecked(); // stride
    int iFormat = args[4]->Int32Value(context).ToChecked(); // barcode types
    Local<Function> cb = Local<
以下是一个调用摄像头扫描条形码并在文本框显示的WinForm应用程序的完整代码和所需组件: 1. 在项目添加一个PictureBox控件和一个TextBox控件。 2. 在NuGet包管理器搜索并安装ZXing.Net包。这是一个用于解码条形码的开源库。 3. 将以下代码添加到Form1.cs文件: ```csharp using System; using System.Drawing; using System.Windows.Forms; using ZXing; namespace BarcodeScanner { public partial class Form1 : Form { private readonly Webcam webcam; public Form1() { InitializeComponent(); // 初始化摄像头 webcam = new Webcam(pictureBox1); webcam.OnCaptured += Webcam_OnCaptured; } private void Form1_Load(object sender, EventArgs e) { // 启动摄像头 webcam.Start(); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { // 停用摄像头 webcam.Stop(); } private void Webcam_OnCaptured(object source, WebcamEventArgs e) { // 解码条形码 var barcodeReader = new BarcodeReader(); var result = barcodeReader.Decode((Bitmap)e.Frame.Clone()); if (result != null) { // 在文本框显示条形码内容 textBox1.Invoke(new Action(() => textBox1.Text = result.Text)); } } } } ``` 4. 添加一个名为“Webcam”的新类: ```csharp using System; using System.Drawing; using System.Windows.Forms; namespace BarcodeScanner { public class Webcam { private readonly PictureBox pictureBox; private readonly Timer timer; public event EventHandler<WebcamEventArgs> OnCaptured; public Webcam(PictureBox pictureBox) { this.pictureBox = pictureBox; this.timer = new Timer(); this.timer.Tick += Timer_Tick; this.timer.Interval = 100; } public void Start() { var devices = new WebCamLib.VideoCaptureDevices(); if (devices.Count == 0) { throw new Exception("No webcam found!"); } var device = devices[0]; device.VideoResolution = device.SupportedVideoResolutions[0]; device.Start(); timer.Start(); } public void Stop() { timer.Stop(); var devices = new WebCamLib.VideoCaptureDevices(); if (devices.Count > 0) { devices[0].Stop(); } } private void Timer_Tick(object sender, EventArgs e) { var devices = new WebCamLib.VideoCaptureDevices(); if (devices.Count > 0) { var device = devices[0]; var frame = device.GetCurrentFrame(); pictureBox.Image = frame; OnCaptured?.Invoke(this, new WebcamEventArgs(frame)); } } } public class WebcamEventArgs : EventArgs { public Bitmap Frame { get; } public WebcamEventArgs(Bitmap frame) { Frame = frame; } } } ``` 5. 运行应用程序,将摄像头对准条形码扫描条形码内容将显示在文本框
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值