第三方库:

  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

最新文章

  1. FORM
  2. IntelliJ IDEA 15 在线激活地址
  3. Javascript实现页面跳转的几种方式
  4. cocoapods版本更新
  5. [Leetcode]String to Integer (atoi) 简易实现方法
  6. JavaScript资源大全
  7. frequentism-and-bayesianism-chs-ii
  8. ffmpeg 从内存中读取数据(或将数据输出到内存)
  9. 关于Ajax使用 Callback 函数
  10. 真正菜鸟用教程之WQSG Scrip Export WQSG (脚本导出导入工具,PSP、NDS汉化必备 )
  11. xhost
  12. TP 控制器扩展_initialize方法实现原理
  13. HDU2516-取石子游戏
  14. C语言中处理结构体的原理
  15. AutoDesk Forge 获取令牌认证
  16. 2017 Pig-0.16.0安装
  17. Lodop打印设计、维护、预览、直接打印简单介绍
  18. Codeforces 854C Planning 【贪心】
  19. 安装SQL Server For Linux(Install SQL Server)
  20. Vim常用命令:移动 跳转 到 文档开头或末尾

热门文章

  1. 通过phoenix导入数据到hbase出错记录
  2. vuejs cli3 env配置文件实践指南
  3. mocha单元测试简易教程
  4. vue---封装request做数据请求
  5. ubuntu 18.04屏幕共享 -------(转载) ( Windows远程登录Ubuntu )
  6. 基于ifc数据的bim模型吊装模拟-1
  7. layui自定义模块
  8. base64方式显示控件
  9. [LeetCode] 654. Maximum Binary Tree 最大二叉树
  10. 最常见的Java面试题及答案汇总(一)