前段时间看到一篇博客,是这个功能的,参考了那篇博客写了这个功能玩一玩,没有做商业用途。发现他的代码给的有些描述不清晰的,我就自己整理一下发出来记录一下。

参考博客链接:https://www.cnblogs.com/geeking/p/4181450.html
  好了 进入正题。

项目环境

项目代码的版本是.NET4.0的

主要采用的插件是

都是我在网上找的资源插件 版本的话 随意吧  我也不知道哪个版本最适用了。

AForge主要是调用摄像头的

zxing是调用解析二维码的 其实还有生成二维码的功能。

前台界面

 
这里的窗体只是放了一个列表标签,存储电脑上面的摄像头设备(如果没有就不能用这个功能了) 另外的一个开启关闭按钮,一个图片控件控制显示图片。一个文本框展示解析出来的二维码地址。
另外还有两个time控件完成图片的刷新,控制图片刷新的频率。

代码部分

后台代码如下:(不想看解析的直接划到最后 有全部的源码展示)

首先是加载部分的代码,主要用于调用插件获取摄像头设备。

   private void Form1_Load(object sender, EventArgs e)
{
//获取摄像头列表
getCamList();
}
   /// <summary>
/// 获取摄像头列表
/// </summary>
private void getCamList()
{
try
{
//AForge.Video.DirectShow.FilterInfoCollection 设备枚举类
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
//清空列表框
comboBox1.Items.Clear();
if (videoDevices.Count == )
throw new ApplicationException();
DeviceExist = true;
//加入设备
foreach (FilterInfo device in videoDevices)
{
comboBox1.Items.Add(device.Name);
}
//默认选择第一项
comboBox1.SelectedIndex = ;
}
catch (ApplicationException)
{
DeviceExist = false;
comboBox1.Items.Add("未找到可用设备");
}
}

下一步 是声明的全局变量代码

        FilterInfoCollection videoDevices; //所有摄像头
VideoCaptureDevice videoSource; //当前摄像头
public int selectedDeviceIndex = ;
/// <summary>
/// 全局变量,标示设备摄像头设备是否存在
/// </summary>
bool DeviceExist;
/// <summary>
/// 全局变量,记录扫描线距离顶端的距离
/// </summary>
int top = ;
/// <summary>
/// 全局变量,保存每一次捕获的图像
/// </summary>
Bitmap img = null;

然后是点击开始按钮的代码

  private void start_Click(object sender, EventArgs e)
{
if (start.Text == "开始")
{
if (DeviceExist)
{
//视频捕获设备
videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
//捕获到新画面时触发
videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);
//先关一下,下面再打开。避免重复打开的错误
CloseVideoSource();
//设置画面大小
videoSource.DesiredFrameSize = new Size(, );
//启动视频组件
videoSource.Start();
start.Text = "结束";
//启动定时解析二维码
timer1.Enabled = true;
//启动绘制视频中的扫描线
timer2.Enabled = true;
}
}
else
{
if (videoSource.IsRunning)
{
timer2.Enabled = false;
timer1.Enabled = false;
CloseVideoSource();
start.Text = "开始";
}
}
}

两个timer控件的代码

  private void timer1_Tick(object sender, EventArgs e)
{
if (img == null)
{
return;
}
#region 将图片转换成byte数组
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] bt = ms.GetBuffer();
ms.Close();
#endregion
#region 不稳定的二维码解析端口
LuminanceSource source = new RGBLuminanceSource(bt, img.Width, img.Height);
BinaryBitmap bitmap = new BinaryBitmap(new ZXing.Common.HybridBinarizer(source)); Result result; MultiFormatReader multiFormatReader = new MultiFormatReader(); try
{
//开始解码
result = multiFormatReader.decode(bitmap);//(不定期暴毙)
}
catch (Exception ex)
{
return;
}
finally
{
multiFormatReader.reset(); } if (result != null)
{
textBox1.Text = result.Text; }
#endregion }
private void timer2_Tick(object sender, EventArgs e)
{
if (img == null)
{
return;
}
Bitmap img2 = (Bitmap)img.Clone();
Pen p = new Pen(Color.Red);
Graphics g = Graphics.FromImage(img2);
Point p1 = new Point(, top);
Point p2 = new Point(pictureBox1.Width, top);
g.DrawLine(p, p1, p2);
g.Dispose();
top += ; top = top % pictureBox1.Height;
pictureBox1.Image = img2; }

以及关闭摄像头的方法:

  /// <summary>
/// 关闭摄像头
/// </summary>
private void CloseVideoSource()
{
if (!(videoSource == null))
if (videoSource.IsRunning)
{
videoSource.SignalToStop();
videoSource = null;
}
}

基本的操作都是在DLL方法里面封装的,zxing代码好像是用java写的吧 ,我自己的电脑上运行这里的代码 有时候会报错,所以对于源代码改了一下,现在至少跑起来应该还行,此文章只是为了自己总结知识点用的,如果涉及侵权,请通知,会立即删除。

另附所有代码内容

  public partial class Form1 : Form
{
FilterInfoCollection videoDevices; //所有摄像头
VideoCaptureDevice videoSource; //当前摄像头
public int selectedDeviceIndex = ;
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 全局变量,标示设备摄像头设备是否存在
/// </summary>
bool DeviceExist;
/// <summary>
/// 全局变量,记录扫描线距离顶端的距离
/// </summary>
int top = ;
/// <summary>
/// 全局变量,保存每一次捕获的图像
/// </summary>
Bitmap img = null; private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
img = (Bitmap)eventArgs.Frame.Clone(); } /// <summary>
/// 关闭摄像头
/// </summary>
private void CloseVideoSource()
{
if (!(videoSource == null))
if (videoSource.IsRunning)
{
videoSource.SignalToStop();
videoSource = null;
}
}
/// <summary>
/// 获取摄像头列表
/// </summary>
private void getCamList()
{
try
{
//AForge.Video.DirectShow.FilterInfoCollection 设备枚举类
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
//清空列表框
comboBox1.Items.Clear();
if (videoDevices.Count == )
throw new ApplicationException();
DeviceExist = true;
//加入设备
foreach (FilterInfo device in videoDevices)
{
comboBox1.Items.Add(device.Name);
}
//默认选择第一项
comboBox1.SelectedIndex = ;
}
catch (ApplicationException)
{
DeviceExist = false;
comboBox1.Items.Add("未找到可用设备");
}
} private void start_Click(object sender, EventArgs e)
{
if (start.Text == "开始")
{
if (DeviceExist)
{
//视频捕获设备
videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
//捕获到新画面时触发
videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);
//先关一下,下面再打开。避免重复打开的错误
CloseVideoSource();
//设置画面大小
videoSource.DesiredFrameSize = new Size(, );
//启动视频组件
videoSource.Start();
start.Text = "结束";
//启动定时解析二维码
timer1.Enabled = true;
//启动绘制视频中的扫描线
timer2.Enabled = true;
}
}
else
{
if (videoSource.IsRunning)
{
timer2.Enabled = false;
timer1.Enabled = false;
CloseVideoSource();
start.Text = "开始";
}
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (img == null)
{
return;
}
#region 将图片转换成byte数组
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] bt = ms.GetBuffer();
ms.Close();
#endregion
#region 不稳定的二维码解析端口
LuminanceSource source = new RGBLuminanceSource(bt, img.Width, img.Height);
BinaryBitmap bitmap = new BinaryBitmap(new ZXing.Common.HybridBinarizer(source)); Result result; MultiFormatReader multiFormatReader = new MultiFormatReader(); try
{
//开始解码
result = multiFormatReader.decode(bitmap);//(不定期暴毙)
}
catch (Exception ex)
{
return;
}
finally
{
multiFormatReader.reset(); } if (result != null)
{
textBox1.Text = result.Text; }
#endregion }
private void timer2_Tick(object sender, EventArgs e)
{
if (img == null)
{
return;
}
Bitmap img2 = (Bitmap)img.Clone();
Pen p = new Pen(Color.Red);
Graphics g = Graphics.FromImage(img2);
Point p1 = new Point(, top);
Point p2 = new Point(pictureBox1.Width, top);
g.DrawLine(p, p1, p2);
g.Dispose();
top += ; top = top % pictureBox1.Height;
pictureBox1.Image = img2; } private void Form1_Load(object sender, EventArgs e)
{
//获取摄像头列表
getCamList();
} }

参考文章 https://www.cnblogs.com/geeking/p/4181450.html

C# winfrom调用摄像头扫描二维码(完整版)的更多相关文章

  1. 使用vue做移动app时,调用摄像头扫描二维码

    现在前端技术发展飞快,前端都能做app了,那么项目中,也会遇到调用安卓手机基层的一些功能,比如调用摄像头,完成扫描二维码功能 下面我就为大家讲解一下,我在项目中调用这功能的过程. 首先我们需要一个中间 ...

  2. h5端呼起摄像头扫描二维码并解析

    2016年6月29日补充: 最近做了一些与表单相关的项目,使用了h5的input控件,在使用过程中遇到了很多的坑.也包括与这篇文章相关的. 首先我们应该知道使用h5新提供的属性getUserMedia ...

  3. Win10 UWP开发:摄像头扫描二维码/一维码功能

    这个示例演示整合了Aran和微软的示例,无需修改即可运行. 支持识别,二维码/一维码,需要在包清单管理器勾选摄像头权限. 首先右键项目引用,打开Nuget包管理器搜索安装:ZXing.Net.Mobi ...

  4. 在WPF中开启摄像头扫描二维码(Media+Zxing)

    近两天项目中需要添加一个功能,是根据摄像头来读取二维码信息,然后根据读出来的信息来和数据库中进行对比显示数据. 选择技术Zxing.WPFMediaKit.基本的原理就是让WPFmediaKit来对摄 ...

  5. c# winform调用摄像头识别二维码

    首先我们需要引用两个第三方组件:AForge和zxing. Aforge是摄像头操作组件,zxing是二维码识别组件.都是开源项目.避免重复造轮子. 其实一些操作代码我也是参照别人的,若侵犯您的版权, ...

  6. 打开手机摄像头扫描二维码或条形码全部操作(代码写的不好,请提出指教,共同进步,我只是一个Android的小白)

    (1)下载二维码的库源码 链接:http://pan.baidu.com/s/1pKQyw2n 密码:r5bv 下载完成后打开可以看到 libzxing 的文件夹,最后添加进 Android  Stu ...

  7. Vue-cli4 唤醒摄像头扫描二维码

    <template> <div class="scan"> <div id="bcid"> <div id=" ...

  8. Ionic2学习笔记(10):扫描二维码

    作者:Grey 原文地址: http://www.cnblogs.com/greyzeng/p/5575843.html 时间:6/11/2016     说明: 在本文发表的时候(2016-06-1 ...

  9. uni-app开发经验分享十三:实现手机扫描二维码并跳转全过程

    最近使用 uni-app 开发 app ,需要实现一个调起手机摄像头扫描二维码功能,官网API文档给出了这样一个demo: // 允许从相机和相册扫码 uni.scanCode({ success: ...

随机推荐

  1. 基于HTML的购物车模型的代码设计

    HTML代码 <html lang="en"> <head> <meta charset="UTF-8"> ​ ​ < ...

  2. C++ lambda expression

    Emerged since c++11, lambda expression/function is an unnamed function object capable of capturing v ...

  3. Supermap/Cesium 开发心得----飞天动线的实现

    在实际开发中,我遇到这样的问题,只给了两地点的坐标,要求会出从A地到B地的在天上飞的那种动态线 本质上动线的效果就是构造实体Entity中的polyline来实现,设置好材质,颜色和运动频率 具体实现 ...

  4. DFA最小化,语法分析初步

    1.将DFA最小化:教材P65 第9题 2.构造以下文法相应的最小的DFA S→ 0A|1B A→ 1S|1 B→0S|0 语言:(01 | 10)*(01 | 10) 自动机图: DFA状态转换矩阵 ...

  5. React搭建项目(全家桶)

    安装React脚手架: npm install -g create-react-app 创建项目: create-react-app app app:为该项目名称 或者跳过以上两步直接使用: npx ...

  6. Leetcode547: Friend Circles 朋友圈问题

    问题描述 在一个班级里有N个同学, 有些同学是朋友,有些不是.他们之间的友谊是可以传递的比如A和B是朋友,B和C是朋友,那么A和C也是朋友.我们定义 friend circle为由直接或者间接都是朋友 ...

  7. 0基础-scp命令一学就会

    scp -P 22 -r  /home/server Android@192.168.1.110:/opt    将本地/home/server的文件夹上传到远端服务器192.168.1.110的目录 ...

  8. ts--泛型

    //泛型:软件工程中,我们不仅要创建一致的定义良好的API,同时也要考虑可重用性,组件不仅要能支持当前的数据类型,同时也能支持未来的数据类型 //泛型就是解决 类  接口  方法的复用性 以及对不特定 ...

  9. asp.net core 3.0 选项模式1:使用

    本篇只是从应用角度来说明asp.net core的选项模式,下一篇会从源码来分析 1.以前的方式 以前我们使用web.config/app.config时是这样使用配置的 var count = Co ...

  10. js 实现ReplaceAll 的方法

    JS  字符串有replace() 方法.但这个方法只会对匹配到的第一个字串替换. 如下例: <HTML> <HEAD> <TITLE> New Document ...