C#_WPF中创建二维码、识别二维码
第三方库:
WPFMediaKit.dll (WPFMediaKit摄像头处理)
zing.dll
NuGet安装这两个第三方dll
项目截图预览:
项目代码:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using WPFMediaKit.DirectShow.Controls;
using ZXing;
using ZXing.Common;
using ZXing.QrCode.Internal; namespace QRcode
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{ /// <summary>
/// ZXING 二维码扫描类
/// </summary>
BarcodeReader codeReader = new BarcodeReader(); /// <summary>
/// 定时器
/// </summary>
DispatcherTimer cameraTimer = new DispatcherTimer(); public MainWindow()
{
InitializeComponent(); // 配置的摄像头名称
var camera = "Lenovo EasyCamera";
if (MultimediaUtil.VideoInputNames.Contains(camera))
{
//控件制定摄像头
vce.VideoCaptureSource = camera;
cameraTimer.IsEnabled = false;
cameraTimer.Interval = new TimeSpan(200); //执行间隔0.2秒
cameraTimer.Tick += cameraTimer_Tick; } }
/// <summary>
/// 计时器方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void cameraTimer_Tick(object sender, EventArgs e)
{
RenderTargetBitmap bmp = new RenderTargetBitmap((int)vce.ActualWidth, (int)vce.ActualHeight, 96, 96, PixelFormats.Default);
vce.Measure(vce.RenderSize);
vce.Arrange(new Rect(vce.RenderSize));
bmp.Render(vce);
BitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmp));
using (MemoryStream ms = new MemoryStream())
{
encoder.Save(ms);
Bitmap btiMap = new Bitmap(ms);
var result = codeReader.Decode(btiMap);//解析条码
if (result != null)
{
// 1:停止识别
cameraTimer.Stop();
vce.Play();
MessageBox.Show($"识别内容为:{result}"); }
}
} private void BtnShiBie_Click(object sender, RoutedEventArgs e)
{
cameraTimer.Start();
} private void BtnShnegCeng_Click(object sender, RoutedEventArgs e)
{
var codimg= Create("hello world!!");
imgQR.Source = ChangeBitmapToImageSource(codimg);
}
/// <summary>
/// 创建二维码
/// </summary>
/// <param name="msg">二维码中保存的信息</param>
/// <returns></returns>
public static Bitmap Create(string msg)
{
MultiFormatWriter writer = new MultiFormatWriter();
Dictionary<EncodeHintType, object> hint = new Dictionary<EncodeHintType, object>();
//设置二维码为utf-8编码
hint.Add(EncodeHintType.CHARACTER_SET, "utf-8");
//设置纠错等级, 高
hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
BitMatrix bm = writer.encode(msg, BarcodeFormat.QR_CODE, 200, 120, hint);
BarcodeWriter barcodeWriter = new BarcodeWriter();
Bitmap bitmap = barcodeWriter.Write(bm);
string codePath = Directory.GetCurrentDirectory() + "/code.jpg";
if (File.Exists(codePath))
File.Delete(codePath);
bitmap.Save(codePath);
return bitmap;
}
/// <summary>
/// 从bitmap转换成ImageSource
/// </summary>
/// <param name="icon"></param>
/// <returns></returns>
public static ImageSource ChangeBitmapToImageSource(Bitmap bitmap)
{
IntPtr hBitmap = bitmap.GetHbitmap(); ImageSource wpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
hBitmap,
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions()); if (!DeleteObject(hBitmap))
{
throw new System.ComponentModel.Win32Exception();
}
return wpfBitmap;
} [DllImport("gdi32.dll", SetLastError = true)]
private static extern bool DeleteObject(IntPtr hObject);
}
}
项目源代码地址:https://download.csdn.net/download/qingchundaima/11151083
C#_WPF中创建二维码、识别二维码的更多相关文章
- Flutter扫码识别二维码内容
前面一篇写了生成二维码图片,这篇来写使用相机扫描识别二维码 识别二维码需要用到插件 barcode_scan 首先在 pubspec.yaml 文件中添加以下依赖,添加依赖后在 pubspec.yam ...
- winform 扫码识别二维码
因为公司业务需求,需要在Windows系统下调用摄像头识别二维码需求,就有了这个功能. 我根据网上网友提供的一些资料,自己整合应用到项目中,效果还不错(就是感觉像素不是太好) 现在将调用摄像头+识别二 ...
- Opencv+Zbar二维码识别(一维码校正)
一维码由一组规则排列的黑色线条.白色线条以及对应的字符组成.对倾斜的(没有严重形变)一维码的角度校正,可以根据其黑白相间.排列规则的特点,计算傅里叶频谱,通过傅里叶频谱中直线的倾斜角度计算空间域图像一 ...
- 使用 CommandScene 类在 XNA 中创建命令场景(十二)
平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...
- spa(单页应用)中,使用history模式时,微信长按识别二维码在ios下失效的问题
spa(单页应用,vue)中,使用history模式时,微信长按识别二维码在ios下失效的问题. 触发条件: spa单页应用: 路由模式 history 从其他页面跳转到带有微信二维码识别的页面(不是 ...
- 微信长按识别二维码,在 vue 项目中的实现
微信长按识别二维码是 QQ 浏览器的内置功能,该功能的基础一定要使用 img 标签引入图片,其他方式的二维码无法识别. 在 vue 中使用 QrcodeVue 插件 demo1 在 template ...
- 页面中嵌套iframe,微信浏览器长按二维码识别不了
问题:在微信浏览器内,页面中嵌套iframe,iframe中用户触发事件后有个弹框会显示二维码,用户长按二维码可以识别并跳转.尝试了一下,安卓是正常的,但是ios是识别不了的. 解决过程: 1.这里客 ...
- Python 创建本地服务器环境生成二维码
一. 需求 公司要做一个H5手机端适配页面,因技术问题所以H5是外包的,每次前端给我们源码,我们把源码传到服务器让其他人访问看是否存在bug,这个不是很麻烦吗?有人说,可以让前端在他们的服务器上先托管 ...
- 使用ZXing.Net生成与识别二维码(QR Code)
Google ZXing是目前一个常用的基于Java实现的多种格式的1D/2D条码图像处理库,出于其开源的特性其现在已有多平台版本.比如今天要用到的ZXing.Net就是针对微软.Net平台的版本.使 ...
随机推荐
- Markdown 打出上下标
1. 打上标,下标 2<sup>10</sup> <!--下标--> 2.同时打上下标 $x^p_ {ij}$ <!--上标为p,下标为ij,{}是用来组合i ...
- 【2019.12.11】SDN上机第7次作业
打开P4的目录,运行主程序 make run 此时输入命令 pingall 会显示所有的网络不通 改为下方代码 /* -*- P4_16 -*- */ #include <core.p4> ...
- NIO Channel Scatter/Gather 管道Pipe类
通道提供了一种被称为Scatter/Gather的重要新功能(有时也被称为矢量I/O).Scatter/Gather是一个简单却强大的概念,它是指在多个缓冲区上实现一个简单的I/O操作.对于一个wri ...
- 在mybatis中写sql语句的一些体会
本文会使用一个案例,就mybatis的一些基础语法进行讲解.案例中使用到的数据库表和对象如下: article表:这个表存放的是文章的基础信息 -- ------------------------- ...
- 团队作业-Beta冲刺(4/4)
队名:软工9组 组长博客:https://www.cnblogs.com/cmlei/ 作业博客:https://edu.cnblogs.com/campus/fzu/SoftwareEngineer ...
- 剑指offer:丑数
题目描述: 把只包含质因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含质因子7. 习惯上我们把1当做是第一个丑数.求按从小到大的顺序的第N个丑数. 解题 ...
- 部分NLP工程师面试题总结
面试题 https://www.cnblogs.com/CheeseZH/p/11927577.html 其他 大数据相关面试题 https://www.cnblogs.com/CheeseZH/p/ ...
- vi中使用删除键(backspace)只能删除到行首不能跳到上一行怎么处理?
答: 在~/.vimrc中加入以下内容: set backspace=2
- 常见的 35 个 Python 面试题及答案
1. Python 面试问题及答案 作为一个 Python 新手,你必须熟悉基础知识.在本文中我们将讨论一些 Python 面试的基础问题和高级问题以及答案,以帮助你完成面试.包括 Python 开发 ...
- Python3 多线程(连接池)操作MySQL插入数据
1.主要模块DBUtils : 允许在多线程应用和数据库之间连接的模块套件Threading : 提供多线程功能 2.创建连接池PooledDB 基本参数: mincached : 最少的空闲连接数, ...