.net 实现pc端自动扫描条码,仿超市识别货物码,使用ZBar,zBar比ZXing更好用
程序员文章站
2022-05-06 09:29:43
...
仿超市识别二维码或条形码功能,用到的硬件有:摄像头
采用的技术:.net
用到的控件:zbar
实现过程:1.首页要有个能唤起摄像头的功能
//用来操作摄像头
private VideoCaptureDevice Camera = null;
//获取摄像头列表
var devs = new FilterInfoCollection(FilterCategory.VideoInputDevice);
//实例化设备控制类
Camera = new VideoCaptureDevice(devs[0].MonikerString);
//配置录像参数(宽,高,帧率,比特率,等)
Camera.VideoResolution = Camera.VideoCapabilities[0];
//设置回调,aforge会不断从这个回调推出图像数据
Camera.NewFrame += Camera_NewFrame;
//开始
Camera.Start();
2摄像头每次抓帧,然后进行解析帧
private void Camera_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
//写到文件
//VideoOutPut.WriteVideoFrame(eventArgs.Frame);
lock (bmp)
{
//释放上一个缓存
bmp.Dispose();
//保存一份缓存
bmp = eventArgs.Frame.Clone() as Bitmap;
// pictureBox1.Refresh();
// Action(XmlReadMode=>)
new Thread(() =>
{
Action<string> action = (data) =>
{
if (bmp != null)
{
// var b2 = CopyBitmap(bmp);
ScanBarCode2(bmp);
}
Resh(data);
};
Invoke(action, "as");
}).Start();
}
}
3.解析帧上的条形码或二维码
/// <summary>
/// 条码识别
/// </summary>
private void ScanBarCode(string fileName)
{
DateTime now = DateTime.Now;
Image primaryImage = Image.FromFile(fileName);
Bitmap pImg = MakeGrayscale3((Bitmap)primaryImage);
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);
scanner.SetConfiguration(ZBar.SymbolType.QRCODE, ZBar.Config.Enable, 1);
List<ZBar.Symbol> symbols = new List<ZBar.Symbol>();
symbols = scanner.Scan((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);
}
}
}
4最后看效果
上一篇: 如何在C#中做deep copy?
下一篇: c#银行本息和计算器