C#调用摄像头(AForge)实现扫描条码解析(Zxing)功能
网上找了很多代码,都比较零散,以下代码纯自己手写,经过测试。下面有链接,下载后可以直接使用。
介绍:
自动识别:点击Start按钮会调用PC摄像头,代码内置Timer,会每100毫秒识别一下当前摄像头图像中的图像,并调用条码识别功能判定是否有条码,如果有的话就直接停止,否则继循环识别。
截图:也可以手动截图,截图后存在运行目录,请自行查找。
补充:识别通过率取决于摄像头的像素,我的笔记本比较烂,所以通过率不高。高像素的摄像头通过率很高。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; using AForge;
using AForge.Controls;
using AForge.Imaging;
using AForge.Video;
using AForge.Video.DirectShow;
using System.Drawing.Imaging; using ZXing;
using ZXing.Common;
using ZXing.QrCode;
using ZXing.QrCode.Internal; /// <summary>
/// 20190515 by hanfre
/// 关于原理:
/// C#调用摄像头+存储图片+Zxing/Zbar图片识别.当开启摄像头的时候利用Timer对当前图片进行解析处理,识别条码;
/// 关于条码解析:
/// 这个DEMO含两个条码解析组件,分别是Zxing和Zbar,使用哪个可以自己切换;
/// 关于作者:
/// Hanfre
/// </summary>
namespace WindowsFormsApplication1
{
/// <summary>
/// 20190515 by hanfre
/// </summary>
public partial class Form1 : Form
{
#region 全局变量定义
FilterInfoCollection videoDevices;
VideoCaptureDevice videoSource;
public int selectedDeviceIndex = 0;
#endregion public Form1()
{
InitializeComponent(); InitializeView();
} #region 事件
/// <summary>
/// 启动
/// 20190515 by hanfre
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BtnStart_Click(object sender, EventArgs e)
{
PbxScanner.Image = null; videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
selectedDeviceIndex = 0;
videoSource = new VideoCaptureDevice(videoDevices[selectedDeviceIndex].MonikerString);//连接摄像头 videoSource.NewFrame += new NewFrameEventHandler(VspContainerClone);//捕获画面事件 videoSource.VideoResolution = videoSource.VideoCapabilities[selectedDeviceIndex];
VspContainer.VideoSource = videoSource;
VspContainer.Start(); StartVideoSource();
} /// <summary>
/// 停止
/// 20190515 by hanfre
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BtnStop_Click(object sender, EventArgs e)
{
CloseVideoSource();
} /// <summary>
/// 保存
/// 20190515 by hanfre
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BtnScanner_Click(object sender, EventArgs e)
{
if (videoSource == null)
return;
Bitmap bitmap = VspContainer.GetCurrentVideoFrame();
string fileName = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-ff") + ".jpg"; bitmap.Save(Application.StartupPath + "\\" + fileName, ImageFormat.Jpeg);
bitmap.Dispose();
} /// <summary>
/// 同步事件
/// 20190515 by hanfre
/// </summary>
/// <param name="sender"></param>
/// <param name="eventArgs"></param>
private void VspContainerClone(object sender, NewFrameEventArgs eventArgs)
{
PbxScanner.Image = (Bitmap)eventArgs.Frame.Clone();
} /// <summary>
/// Timer定时器
/// 20190515 by hanfre
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TmScanner_Tick(object sender, EventArgs e)
{
if (PbxScanner.Image != null)
{
TmScanner.Enabled = false;
Bitmap img = (Bitmap)PbxScanner.Image.Clone();
if (DecodeByZxing(img))
///if (DecodeByZbar(img))
{
CloseVideoSource();
}
else
{
TmScanner.Enabled = true;
}
}
}
#endregion #region 方法
/// <summary>
/// 初始化
/// 20190515 by hanfre
/// </summary>
private void InitializeView()
{
BtnScanner.Enabled = false;
BtnStop.Enabled = false;
} /// <summary>
/// 启动
/// 20190515 by hanfre
/// </summary>
private void StartVideoSource()
{
TmScanner.Enabled = true;
BtnStart.Enabled = false;
BtnStop.Enabled = true;
BtnScanner.Enabled = true;
}
/// <summary>
/// 关闭
/// 20190515 by hanfre
/// </summary>
private void CloseVideoSource()
{
if (!(videoSource == null))
{
if (videoSource.IsRunning)
{
videoSource.SignalToStop();
videoSource = null;
}
} VspContainer.SignalToStop();
//videoSourcePlayer1.Stop();
//videoSourcePlayer1.Dispose(); TmScanner.Enabled = false;
BtnScanner.Enabled = false;
BtnStart.Enabled = true;
BtnStop.Enabled = false;
}
#endregion #region 方法/Zxing&Zbar
/// <summary>
/// 解码
/// 20190515 by hanfre
/// </summary>
/// <param name="b"></param>
/// <returns></returns>
private bool DecodeByZxing(Bitmap b)
{
try
{
BarcodeReader reader = new BarcodeReader();
reader.AutoRotate = true;
Result result = reader.Decode(b); TxtScannerCode.Text = result.Text;
}
catch (Exception e)
{
System.Console.WriteLine(e.Message);
TxtScannerCode.Text = "";
return false;
} return true;
} private bool DecodeByZbar(Bitmap b)
{
DateTime now = DateTime.Now; Bitmap pImg = ZbarMakeGrayscale3(b);
using (ZBar.ImageScanner scanner = new ZBar.ImageScanner())
{
scanner.SetConfiguration(ZBar.SymbolType.None, ZBar.Config.Enable, 0);
scanner.SetConfiguration(ZBar.SymbolType.CODE39, ZBar.Config.Enable, 1);
scanner.SetConfiguration(ZBar.SymbolType.CODE128, ZBar.Config.Enable, 1); List<ZBar.Symbol> symbols = new List<ZBar.Symbol>();
symbols = scanner.Scan((System.Drawing.Image)pImg); if (symbols != null && symbols.Count > 0)
{
string result = string.Empty;
symbols.ForEach(s => result += "条码内容:" + s.Data + " 条码质量:" + s.Quality + Environment.NewLine);
MessageBox.Show(result);
return true;
}
else
{
return false;
}
}
} /// <summary>
/// 处理图片灰度
/// </summary>
/// <param name="original"></param>
/// <returns></returns>
public static Bitmap ZbarMakeGrayscale3(Bitmap original)
{
//create a blank bitmap the same size as original
Bitmap newBitmap = new Bitmap(original.Width, original.Height); //get a graphics object from the new image
Graphics g = Graphics.FromImage(newBitmap); //create the grayscale ColorMatrix
System.Drawing.Imaging.ColorMatrix colorMatrix = new System.Drawing.Imaging.ColorMatrix(
new float[][]
{
new float[] {.3f, .3f, .3f, 0, 0},
new float[] {.59f, .59f, .59f, 0, 0},
new float[] {.11f, .11f, .11f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
}); //create some image attributes
ImageAttributes attributes = new ImageAttributes(); //set the color matrix attribute
attributes.SetColorMatrix(colorMatrix); //draw the original image on the new image
//using the grayscale color matrix
g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes); //dispose the Graphics object
g.Dispose();
return newBitmap;
}
#endregion
}
}
下载地址我已上传到CSDN,可以访问 https://download.csdn.net/download/fandoc/11180026 下载
C#调用摄像头(AForge)实现扫描条码解析(Zxing)功能的更多相关文章
- Smobiler实现扫描条码和拍照功能(开发日志八)
一.调用摄像头进行扫描 barcode000~2.jpg (198.62 KB, 下载次数: 5) 下载附件 2015-12-23 17:41 上传 具体步骤: 1. 加入TextBox控件:加入B ...
- 使用vue做移动app时,调用摄像头扫描二维码
现在前端技术发展飞快,前端都能做app了,那么项目中,也会遇到调用安卓手机基层的一些功能,比如调用摄像头,完成扫描二维码功能 下面我就为大家讲解一下,我在项目中调用这功能的过程. 首先我们需要一个中间 ...
- SNF开发平台WinForm-平板拍照及扫描二维码功能
在我们做项目的时候,经常会有移动平板处理检验,审核等,方便移动办公.这时就需要在现场拍照上传问题,把当场问题进行上传,也有已经拍完照的图片或加工过的图片进行上传.还有在车间现场一体机,工控机 这种产物 ...
- C# winfrom调用摄像头扫描二维码(完整版)
前段时间看到一篇博客,是这个功能的,参考了那篇博客写了这个功能玩一玩,没有做商业用途.发现他的代码给的有些描述不清晰的,我就自己整理一下发出来记录一下. 参考博客链接:https://www.cnbl ...
- C# - VS2019调用AForge库实现调用摄像头拍照功能
前言 作为一名资深Delphi7程序员,想要实现摄像头扫描一维码/二维码功能,发现所有免费的第三方库都没有简便的实现办法,通用的OpenCV或者ZXing库基本上只支持XE以上的版本,而且一维码的识别 ...
- Emgucv(一)Aforge切换摄像头并调用摄像头属性
一.新建一个Windows窗体应用程序,在Form1窗体上添加一个PictureBox控件.一个ComboBox控件,命名为PictureBox1.cbCapture,还有两个Button控件,Tex ...
- AForge调用摄像头拍照时设置分辨率
简单记录下AForge2.2.5.0版本调用摄像头拍照时设置分辨率的方法. FilterInfo info = _videoDevices[0];//获取第一个摄像头 _cameraDevice = ...
- Android入门(十六)调用摄像头相册
原文链接:http://www.orlion.ga/665/ 一.调用摄像头 创建一个项目ChoosePicDemo,修改activity_main.xml: <LinearLayout xml ...
- android: 调用摄像头拍照
很多应用程序都可能会使用到调用摄像头拍照的功能,比如说程序里需要上传一张图片 作为用户的头像,这时打开摄像头拍张照是最简单快捷的.下面就让我们通过一个例子来学 习一下,如何才能在应用程序里调用手机的摄 ...
随机推荐
- PHP中,json汉字编码
当用json与js或者其它客户端交互时,如果有中文,则会变成unicode.虽然能使用,但是影响观看.不好调试呀.从网上找到了几个方法 一,用下面这个函数,需要编码时,直接调用这个函数就成 funct ...
- Spring扩展点之Aware接口族
引言 Spring中提供了各种Aware接口,方便从上下文中获取当前的运行环境,比较常见的几个子接口有:BeanFactoryAware,BeanNameAware,ApplicationContex ...
- MySQL难点语法——子查询
本篇主要通过练习来讲解子查询的知识,进入正题之前,先熟悉数据表,表格的数据可以先不用管,主要是熟悉表格的字段名 这里子查询分为三个部分: 1.where条件子查询 这个子查询和普通的查询没什么区别,主 ...
- vue自带开发环境,生产环境,自己搭建测试环境
git 码云地址: https://gitee.com/qichangshui_admin/vueAddTest 参考地址: https://www.jianshu.com/p/bfcfe5fc253 ...
- Java 之 Set 源码分析
一.HashSet 构造方法: HashSet() :构造一个新的空 set,其底层 HashMap 实例的默认初始容量是 16,加载因子是 0.75 源码: 二.LinkedHashSet 构造方 ...
- 存货?交期?产能不足?APS系统帮你完成计划排程
信息化时代的今天,技术的进步.全球化的竞争与市场环境迅速变化,使得制造业企业的经营环境变得日益复杂. 集中表现在产品生命周期和交货期的缩短,与此同时顾客的需求也变得多样化和个性化.生产方式也从大批量生 ...
- yum无法下载,网关问题
由于网关地址改变没有及时更新配置,造成无法下载 failure: repodata/repomd.xml from base: [Errno 256] No more mirrors to try h ...
- NetScaler的常用配置
CITRIX NETSCALER常用功能有:LB,CS,GSLB,SSL LB实现的功能是服务器负载均衡,CS实现基于七层(域名,IP等)的负载均衡,GSLB实现的功能是全局负载均衡,SSL实现的功能 ...
- PAT 乙级 1091.N-自守数 C++/Java
题目来源 如果某个数 K 的平方乘以 N 以后,结果的末尾几位数等于 K,那么就称这个数为“N-自守数”.例如 3,而 2 的末尾两位正好是 9,所以 9 是一个 3-自守数. 本题就请你编写程序判断 ...
- 良心送分题(牛客挑战赛35E+虚树+最短路)
目录 题目链接 题意 思路 代码 题目链接 传送门 题意 给你一棵树,然后把这棵树复制\(k\)次,然后再添加\(m\)条边,然后给你起点和终点,问你起点到终点的最短路. 思路 由于将树复制\(k\) ...