C# 二维码扫描
Zint类用于产生二维码。https://sourceforge.net/projects/zint/
Zxing类用于读取二维码. https://github.com/zxing/zxing
AForge类用于初始摄像头等。http://www.aforgenet.com/framework/downloads.html
以上三个类为开源的第三方类,可直接引用。
以下为用AForge和ZXing实时读取二维码的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZXing;
using ZXing.Common;
using System.Drawing.Imaging;
using AForge;
using AForge.Controls;
using AForge.Imaging;
using AForge.Video;
using AForge.Video.DirectShow; namespace AutoScan
{
public partial class Form1 : Form
{
private FilterInfoCollection videoDevices;
private VideoCaptureDevice videoSource;
Bitmap bmp = null; //全局变量,保存每一次捕获的图像
int itop = ;// 全局变量,记录扫描线距离顶端的距离
public Form1()
{
InitializeComponent();
}
private void getVideoDevices()
{
cboDevices.Items.Clear();
try
{
videoDevices=new FilterInfoCollection(FilterCategory.VideoInputDevice);
if (videoDevices.Count>)
{
foreach(FilterInfo device in videoDevices)
cboDevices.Items.Add(device.Name);
}
}
catch(Exception ex)
{
MessageBox.Show("not found camera");
}
}
private void Open()
{
if (cboDevices.SelectedIndex == -)
return;
try
{
btnOpen.Enabled = false;
btnClose.Enabled = true;
CloseVideoSource();
videoSource = new VideoCaptureDevice(videoDevices[cboDevices.SelectedIndex].MonikerString);
videoSource.DesiredFrameRate = ;
videoSource.DesiredFrameSize = new Size(, ); videoSource.NewFrame += new NewFrameEventHandler(newframe);
videoSource.Start();
}
catch
{
btnOpen.Enabled = true;
btnClose.Enabled = false;
return;
}
timer1.Enabled = true;//解析二维码
timer2.Enabled = true;//启动绘制视频中的扫描线
} private void Stop()
{
CloseVideoSource();
} private void newframe(object sender, NewFrameEventArgs args)
{
bmp = (Bitmap)args.Frame.Clone();
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Enabled = false;
timer2.Enabled = false;
getVideoDevices();
}
private void CloseVideoSource()
{ timer1.Enabled = false;
timer2.Enabled = false;
if (videoSource == null) return;
if (videoSource.IsRunning)
{
videoSource.SignalToStop();
videoSource.WaitForStop();
videoSource = null;
pictureBox1.Image = null;
}
btnOpen.Enabled = true;
btnClose.Enabled = false;
txtMsg.Clear();
}
private void ScanBarcode()
{
if (pictureBox1.Image == null)
{
MessageBox.Show("请载入图像资源!");
return;
}
//设置读取二维码
DecodingOptions decodeOption = new DecodingOptions();
decodeOption.PossibleFormats = new List<BarcodeFormat>(){
BarcodeFormat.QR_CODE
}; //读取操作
BarcodeReader bar = new BarcodeReader();
bar.Options = decodeOption;
ZXing.Result rs = bar.Decode(pictureBox1.Image as Bitmap);
if (rs == null)
{
txtMsg.Text = "读取失败";
MessageBox.Show("读取失败");
}
else
{
txtMsg.Text = rs.Text;
MessageBox.Show("读取成功,内容:" + rs.Text);
}
} private void timer2_Tick(object sender, EventArgs e)
{
timer2.Enabled = false;
try
{
if (bmp == null) return;
Bitmap bmp2 = (Bitmap)bmp.Clone();
Pen p = new Pen(Color.GreenYellow);
p.Width = ;
Graphics g=Graphics.FromImage(bmp2);
System.Drawing.Point p1 = new System.Drawing.Point(, itop);
System.Drawing.Point p2 = new System.Drawing.Point(pictureBox1.Width,itop);
g.DrawLine(p, p1, p2);
g.Dispose();
itop += ; itop = itop % pictureBox1.Height;
pictureBox1.Image = bmp2;
}
finally
{
timer2.Enabled = true;
}
} private void timer1_Tick(object sender, EventArgs e)
{
Result result;
try
{
if (bmp == null) return;
try
{
BarcodeReader br = new BarcodeReader(); result = br.Decode(bmp);
}
catch(Exception ex)
{
return;
} /*
#region
//将图片转换成byte数组
MemoryStream ms = new MemoryStream();
bmp.Save(ms, ImageFormat.Bmp);
byte[] bt = ms.GetBuffer();
ms.Close(); #endregion
LuminanceSource source = new RGBLuminanceSource(bt, bmp.Width, bmp.Height);
BinaryBitmap bitmap = new BinaryBitmap(new ZXing.Common.HybridBinarizer(source)); try
{ result = new MultiFormatReader().decode(bitmap);
}
catch (ReaderException ex)
{
return;
}*/ if (result != null)
{
txtMsg.Text = result.Text;
}
}
finally
{
timer1.Enabled = true;
} } private void btnClose_Click(object sender, EventArgs e)
{
CloseVideoSource();
} private void btnOpen_Click(object sender, EventArgs e)
{
Open();
}
}
}
界面(黄绿色条为扫描时上下滚动的线条,把条形码放在摄像头处即可扫描):

C# 二维码扫描的更多相关文章
- XAMARIN ANDROID 二维码扫描示例
现在二维码的应用越来越普及,二维码扫描也成为手机应用程序的必备功能了.本文将基于 Xamarin.Android 平台使用 ZXing.Net.Mobile 做一个简单的 Android 条码扫描示 ...
- [Unity3D]自制UnityForAndroid二维码扫描插件
一周左右终于将二维码生成和扫描功能给实现了,终于能舒缓一口气了,从一开始的疑惑为啥不同的扫码客户端为啥扫出来的效果不同?通用的扫描器扫出来就是一个下载APK,自制的扫描器扫出来是想要的有效信息,然后分 ...
- 有关iOS系统中调用相机设备实现二维码扫描功能的注意点(3/3)
今天我们接着聊聊iOS系统实现二维码扫描的其他注意点. 大家还记得前面我们用到的输出数据的类对象吗?AVCaptureMetadataOutput,就是它!如果我们需要实现目前主流APP扫描二维码的功 ...
- Android仿微信二维码扫描
转载:http://blog.csdn.net/xiaanming/article/details/10163203 了解二维码这个东西还是从微信中,当时微信推出二维码扫描功能,自己感觉挺新颖的,从一 ...
- Android—ZXing二维码扫描遇到的问题
最近工作中需要开发带有二维码扫描功能的软件(基于开源项目ZXing),遇到的问题记录一下,也希望给大家带来帮助. 1.首先因为扫描要开摄像机所以加权限是一定的,不然后面什么都不能进行 <uses ...
- Ios二维码扫描(系统自带的二维码扫描)
Ios二维码扫描 这里给大家介绍的时如何使用系统自带的二维码扫描方法和一些简单的动画! 操作步骤: 1).首先你需要搭建UI界面如图:下图我用了俩个imageview和一个label 2).你需要在你 ...
- iOS 自带二维码扫描功能的实现
自从iOS7以后中新增了二维码扫描功能.因此可以在不借助第三方类库的情况下简单的写出二维码的扫描功能: 原生的二维码扫描功能在AVFoundation框架下,所以在使用原生的二维码扫描功能时要先导入A ...
- PhoneGap(二维码扫描 )
关于 phoneGap 如何做 二维码扫描 1. 先配置好, 环境 http://coenraets.org/blog/cordova-phonegap-3-tutorial/http: ...
- 如何用MediaCapture解决二维码扫描问题
二维码扫描的实现,简单的来说可以分三步走:“成像”.“截图”与“识别”. UWP开发中,最常用的媒体工具非MediaCapture莫属了,下面就来简单介绍一下如何利用MediaCapture来实现扫描 ...
- Swift 二维码扫描 简单实现
3.30看视频 学到了二维码简单的实现 还有一些动画的实现 今天就先记录一下二维码扫描的简单实现 不太好记手写一遍 学习的基础在于模仿嘛 创建一个实现二维码扫描的步骤 1.首先是懒加载创建 会话 ...
随机推荐
- js和jquery修改背景颜色的区别
html: <HTML> <head> <meta http-equiv="content-type" content="text/html ...
- Spring学习--切面优先级及重用切点表达式
指定切面的优先级: 在同一个链接点上应用不止一个切面时 , 除非明确指定 , 否则它们的优先级是不确定的. 切面的优先级可以通过实现 Ordered 接口或利用 @Order 注解指定. 实现 Ord ...
- 链接oracle数据库 生成表对应的javabean
package com.databi.utils; import java.io.File; import java.io.FileOutputStream; import java.io.IOExc ...
- PowerDesigner使用教程(转)
PowerDesigner是一款功能非常强大的建模工具软件,足以与Rose比肩,同样是当今最著名的建模软件之一.Rose是专攻UML对象模型的建模工具,之后才向数据库建模发展,而PowerDesign ...
- 3中转换JSON数据的方式
一:前言 来公司一个星期,把最近做的东西梳理下,并把觉得有必要的知识点记载下,现在传数据很多都是用JSON来传数据,所以我就找了集中传json的方式,其实是有五种的,但是有一个我没有用过,太陌生了,上 ...
- Spring - IoC(10): 生命周期
Spring 容器可以管理 singleton 作用域 Bean 的生命周期,容器能够跟踪 Bean 实例的创建.销毁.管理 Bean 生命周期行为主要有两个时机: 注入 Bean 的依赖关系之后 即 ...
- C++中的垃圾回收和内存管理
最开始的时候看到了许式伟的内存管理变革系列,看到性能测试结果的时候,觉得这个实现很不错,没有深入研究其实现.现在想把这个用到自己的一个项目中来,在linux下编译存在一些问题,所以打算深入研究一下. ...
- 【poj3415-Common Substrings】sam子串计数
题意: 给出两个串,问这两个串的所有的子串中(重复出现的,只要是位置不同就算两个子串),长度大于等于k的公共子串有多少个. 题解: 这题好像大神们都用后缀数组做..然而我在sam的题表上看到这题,做 ...
- Django【进阶】中间件
中间件 一.概念 django 中的中间件(middleware),在django中,中间件其实就是一个类,在请求到来和结束后,django会根据自己的规则在合适的时机执行中间件中相应的方法. 其 ...
- 安装VMware Tools的步骤和那些坑
背景环境:VMware workstation 12.5+Ubuntu16.04 首先VMware Tools在ubuntu中是及其不稳定的,也就是说,当你点击菜单栏中的install vmware ...