第三方库:

  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中创建二维码、识别二维码的更多相关文章

  1. Flutter扫码识别二维码内容

    前面一篇写了生成二维码图片,这篇来写使用相机扫描识别二维码 识别二维码需要用到插件 barcode_scan 首先在 pubspec.yaml 文件中添加以下依赖,添加依赖后在 pubspec.yam ...

  2. winform 扫码识别二维码

    因为公司业务需求,需要在Windows系统下调用摄像头识别二维码需求,就有了这个功能. 我根据网上网友提供的一些资料,自己整合应用到项目中,效果还不错(就是感觉像素不是太好) 现在将调用摄像头+识别二 ...

  3. Opencv+Zbar二维码识别(一维码校正)

    一维码由一组规则排列的黑色线条.白色线条以及对应的字符组成.对倾斜的(没有严重形变)一维码的角度校正,可以根据其黑白相间.排列规则的特点,计算傅里叶频谱,通过傅里叶频谱中直线的倾斜角度计算空间域图像一 ...

  4. 使用 CommandScene 类在 XNA 中创建命令场景(十二)

    平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...

  5. spa(单页应用)中,使用history模式时,微信长按识别二维码在ios下失效的问题

    spa(单页应用,vue)中,使用history模式时,微信长按识别二维码在ios下失效的问题. 触发条件: spa单页应用: 路由模式 history 从其他页面跳转到带有微信二维码识别的页面(不是 ...

  6. 微信长按识别二维码,在 vue 项目中的实现

    微信长按识别二维码是 QQ 浏览器的内置功能,该功能的基础一定要使用 img 标签引入图片,其他方式的二维码无法识别. 在 vue 中使用 QrcodeVue 插件 demo1 在 template ...

  7. 页面中嵌套iframe,微信浏览器长按二维码识别不了

    问题:在微信浏览器内,页面中嵌套iframe,iframe中用户触发事件后有个弹框会显示二维码,用户长按二维码可以识别并跳转.尝试了一下,安卓是正常的,但是ios是识别不了的. 解决过程: 1.这里客 ...

  8. Python 创建本地服务器环境生成二维码

    一. 需求 公司要做一个H5手机端适配页面,因技术问题所以H5是外包的,每次前端给我们源码,我们把源码传到服务器让其他人访问看是否存在bug,这个不是很麻烦吗?有人说,可以让前端在他们的服务器上先托管 ...

  9. 使用ZXing.Net生成与识别二维码(QR Code)

    Google ZXing是目前一个常用的基于Java实现的多种格式的1D/2D条码图像处理库,出于其开源的特性其现在已有多平台版本.比如今天要用到的ZXing.Net就是针对微软.Net平台的版本.使 ...

随机推荐

  1. 基于Docker部署ETCD集群

    基于Docker部署ETCD集群 关于ETCD要不要使用TLS? 首先TLS的目的是为了鉴权为了防止别人任意的连接上你的etcd集群.其实意思就是说如果你要放到公网上的ETCD集群,并开放端口,我建议 ...

  2. Hadoop(五)—— HDFS NameNode、DataNode工作机制

    一.NN与2NN工作机制 NameNode(NN) 1.当HDFS启动时,会加载日志(edits)和镜像文件(fsImage)到内存中. 2-4.当元数据的增删改查请求进来时,NameNode会先将操 ...

  3. Beta3冲刺

    队名:福大帮 组长博客链接: 作业博客 : https://edu.cnblogs.com/campus/fzu/SoftwareEngineeringClassAofFuzhouUniversity ...

  4. curl的速度为什么比file_get_contents快以及具体原因

    一.背景      大家做项目的时候,不免会看到前辈的代码.博主最近看到前辈有的时候请求外部接口用的是file_get_contents,有的用的是curl.稍微了解这两部分的同学都知道,curl在性 ...

  5. RecyclerView 实现快速滚动

    RecyclerView 实现快速滚动 https://www.cnblogs.com/mamamia/p/8311449.html

  6. android -------- GifImageView 之gif图片加载

    目前支持Gif播放的开源库还是有的,试了一下还是这种好用点,所以来分享下android-gif-drawable是通过JNI来渲染帧的,这种方式比使用WebView或者Movie效率要高 要求Andr ...

  7. 爬虫的正则表达式re模块

    爬虫一共就四个主要步骤: 明确目标 (要知道你准备在哪个范围或者网站去搜索) 爬 (将所有的网站的内容全部爬下来) 取 (去掉对我们没用处的数据) 处理数据(按照我们想要的方式存储和使用) 对于dow ...

  8. RabbitMQ整合Spring Booot【消费者应答模式】

    生产者代码不变,消费者: package com.toov5.Consumer; import java.io.IOException; import java.util.concurrent.Tim ...

  9. App installation failed (A valid provisioning profile for this executable was not found)

    真机调试build success ,App installation failed (A valid provisioning profile for this executable was not ...

  10. Swift自定义AlertView

    今天项目加新需求,添加积分过期提醒功能: 第一反应就用系统的UIAlertViewController,但是message中积分是需要红色显示. // let str = "尊敬的顾客,您有 ...