using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using com.google.zxing.common;
using AForge.Video.DirectShow;
using com.google.zxing;
using AForge.Video; namespace CameraQRCode
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
} ///调用AForge使用摄像头
FilterInfoCollection Use_Webcams = null;
VideoCaptureDevice cam = null; OpenFileDialog openFileDialog1 = new OpenFileDialog(); /// <summary>
/// 生成二维码并保存图片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnQRCode_Click(object sender, EventArgs e)
{
string content = txtInputForQR.Text;
ByteMatrix byteMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 300, 300);
Bitmap bitmap = toBitmap(byteMatrix);
pictureBox1.Image = bitmap;
SaveFileDialog sFD = new SaveFileDialog();
sFD.Filter = "保存图片(*.png) |*.png|所有文件(*.*) |*.*";
sFD.DefaultExt = "*.png|*.png";
sFD.AddExtension = true;
if (sFD.ShowDialog() == DialogResult.OK)
{
if (sFD.FileName != "")
{
writeToFile(byteMatrix, System.Drawing.Imaging.ImageFormat.Png, sFD.FileName);
} }
} /// <summary>
/// 解析
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnQRDeCode_Click(object sender, EventArgs e)
{
if (this.openFileDialog1.ShowDialog() != DialogResult.OK)
{
return;
}
Image img = Image.FromFile(this.openFileDialog1.FileName);
Bitmap bmap;
try
{
bmap = new Bitmap(img);
}
catch (System.IO.IOException ioe)
{
MessageBox.Show(ioe.ToString());
return;
}
if (bmap == null)
{
MessageBox.Show("Could not decode image");
return;
}
LuminanceSource source = new RGBLuminanceSource(bmap, bmap.Width, bmap.Height);
com.google.zxing.BinaryBitmap bitmap1 = new com.google.zxing.BinaryBitmap(new HybridBinarizer(source));
Result result;
try
{
result = new MultiFormatReader().decode(bitmap1);
}
catch (ReaderException re)
{
MessageBox.Show(re.ToString());
return;
}
txtOutputQR.Text = result.Text;
//MessageBox.Show(result.Text);
} /// <summary>
/// 关闭摄像头
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnStop_Click(object sender, EventArgs e)
{
if (cam != null)
{
//关闭摄像头
if (cam.IsRunning)
{
cam.Stop();
}
} } /// <summary>
/// 打开摄像头
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnOpen_Click(object sender, EventArgs e)
{ cam.Start();
if (!timer1.Enabled)
{
timer1.Enabled = true;
} } private void Cam_NewFrame(object obj, NewFrameEventArgs eventArgs)
{
this.pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
} public static void writeToFile(ByteMatrix matrix, System.Drawing.Imaging.ImageFormat format, string file)
{
Bitmap bmap = toBitmap(matrix);
bmap.Save(file, format);
} public static Bitmap toBitmap(ByteMatrix matrix)
{
int width = matrix.Width;
int height = matrix.Height;
Bitmap bmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
bmap.SetPixel(x, y, matrix.get_Renamed(x, y) != -1 ? ColorTranslator.FromHtml("0xFF000000") : ColorTranslator.FromHtml("0xFFFFFFFF"));
}
}
return bmap;
} /// <summary>
/// 扫描识别二维码
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Tick(object sender, EventArgs e)
{
//识别图片中的二维码 Image img = this.pictureBox1.Image; ;// Image.FromFile(this.openFileDialog1.FileName);
if (img == null)
return;
Bitmap bmap;
try
{
bmap = new Bitmap(img);
}
catch (System.IO.IOException ioe)
{
MessageBox.Show(ioe.ToString());
return;
}
if (bmap == null)
{
MessageBox.Show("Could not decode image");
return;
}
LuminanceSource source = new RGBLuminanceSource(bmap, bmap.Width, bmap.Height);
com.google.zxing.BinaryBitmap bitmap1 = new com.google.zxing.BinaryBitmap(new HybridBinarizer(source));
Result result;
try
{
result = new MultiFormatReader().decode(bitmap1);
}
catch (ReaderException re)
{
//MessageBox.Show(re.ToString());
return;
}
txtOutputQR.Text = result.Text;
this.timer1.Enabled = false;
this.cam.Stop();
this.pictureBox1.Image = null;
MessageBox.Show("图中二维码:" + result.Text);
} private void frmMain_Load(object sender, EventArgs e)
{
//初始化AForge内容
Use_Webcams = new FilterInfoCollection(FilterCategory.VideoInputDevice);
if (Use_Webcams.Count > 0)
{
//实例化对象
cam = new VideoCaptureDevice(Use_Webcams[0].MonikerString); //绑定事件
cam.NewFrame += new NewFrameEventHandler(Cam_NewFrame);
} } }
}

demo下载https://download.csdn.net/download/lw112190/84384040

C# 二维码生成、识别的更多相关文章

  1. Android 二维码 生成和识别(附Demo源码)

    今天讲一下目前移动领域很常用的技术——二维码.现在大街小巷.各大网站都有二维码的踪迹,不管是IOS. Android.WP都有相关支持的软件.之前我就想了解二维码是如何工作,最近因为工作需要使用相关技 ...

  2. Android 二维码 生成和识别(转)

    原博客地址 :http://www.cnblogs.com/weixing/archive/2013/08/28/3287120.html 还有几个写的也可以参考一下:http://www.itnos ...

  3. 玩转Android之二维码生成与识别

    二维码,我们也称作QRCode,QR表示quick response即快速响应,在很多App中我们都能见到二维码的身影,最常见的莫过于微信了.那么今天我们就来看看怎么样在我们自己的App中集成二维码的 ...

  4. 【转】Android 二维码 生成和识别(附Demo源码)--不错

    原文网址:http://www.cnblogs.com/mythou/p/3280023.html 今天讲一下目前移动领域很常用的技术——二维码.现在大街小巷.各大网站都有二维码的踪迹,不管是IOS. ...

  5. C# ZXing.Net生成二维码、识别二维码、生成带Logo的二维码(二)

    1.使用ZXint.Net生成带logo的二维码 /// <summary> /// 生成带Logo的二维码 /// </summary> /// <param name ...

  6. Python 实现二维码生成和识别

    今天突然想给自己自己做个头像,然后还是二维码的形式,这样只要扫一扫就可以访问我的主页.然后就开始自己的苦逼之路... 其实实现二维码java,c#,C++等都可以实现:由于自己正在学python,所以 ...

  7. python qrcode二维码生成与识别

    二维码 二维码生成 1.用法 https://github.com/lincolnloop/python-qrcode 2.使用 简单实用 import qrcode # 二维码内容 data = & ...

  8. [开源]C#二维码生成解析工具,可添加自定义Logo

    二维码又称 QR Code,QR 全称 Quick Response,是一个近几年来移动设备上超流行的一种编码方式,它比传统的 Bar Code 条形码能存更多的信息,也能表示更多的数据类型:比如:字 ...

  9. PHP二维码生成的方法(google APi,PHP类库,libqrencode等)

    原文地址: http://blog.csdn.net/liuxinmingcode/article/details/7910975 ================================== ...

  10. android 二维码生成+扫描

    android 二维码生成+扫描 1.在Android应用当中,很多时候都要用到二维码扫描,来避免让用户手动输入的麻烦. Google官方自己推出了一个二维码开源项目:ZXing库. 2.这里简单介绍 ...

随机推荐

  1. 51单片机封装库HML_FwLib_STC89/STC11

    HML_FwLib_STC89/11 项目地址 https://github.com/MCU-ZHISHAN-IoT/HML_FwLib_STC89 https://github.com/MCU-ZH ...

  2. 基于keras的双层LSTM网络和双向LSTM网络

    1 前言 基于keras的双层LSTM网络和双向LSTM网络中,都会用到 LSTM层,主要参数如下: LSTM(units,input_shape,return_sequences=False) un ...

  3. 利用javax.validation实现对bean属性校验

    1.使用场景介绍 controller层对传入的bean的属性进行非空.属性长度等合法性的校验. 传统的方式是我们要自己写if-else-去判断,比较麻烦. 2.实现效果 3.代码介绍 代码结构: p ...

  4. 《系列二》-- 7、后置处理器-PostProcessor

    目录 什么是后置处理器 spring 源码中已知的,顶级PostProcessor 其它 "后置处理器" 阅读之前要注意的东西:本文就是主打流水账式的源码阅读,主导的是一个参考,主 ...

  5. lib,dll的位置

    在添加第三方库的时候需要注意放置的路径,注意区分x86和x64的文件夹路径以及VS的版本,不要放错了 lib的位置:(需要先将lib放到该路径下,不然会说找不到.lib)C:\Program File ...

  6. Simulink模型指标分析与模型重构的最佳实践 - 软件模型质量保证不可忽视的一环

    在基于模型的开发中,优质的模型架构是生成优质代码的必要前提.静态模型分析对于模型的质量保证有着至关重要的作用,同时建模规范已在业内有着广泛而成熟的应用.然而建模规范并非模型设计原则合规性的唯一考量标准 ...

  7. CXP协议的传输层介绍 8b/10b编码

    8b/10b编码与K码 upconnection 和downconnection均使用8b/10b编码,因此我们先简单回顾一下8b/10b吧 8B/10B编码被广泛应用到高速串行总线,如IEEE139 ...

  8. 内存管理(二)之别小看了Tagged-Pointer关键时刻起到大作用

    本文主要研究Tagged Pointer技术,针对该技术需要解决的问题.以及在实际应用中的价值做一些简单的探讨. 如果你想要更进一步,去挖掘Tagged Pointer是如何实现的,可以参考Frida ...

  9. 【Azure Redis 缓存】Lettuce 连接到Azure Redis服务,出现15分钟Timeout问题

    问题描述 在Java应用中,使用 Lettuce 作为客户端SDK与Azure Redis 服务连接,当遇见连接断开后,长达15分钟才会重连.导致应用在长达15分的时间,持续报错Timeout 问题解 ...

  10. 解决celery与django结合后,分别启动celery和django的进程同时调用定时任务的问题

    django中引入celery后发现在代码中写如下这样的定时任务,启动celery和django的工程后,他们都会调用这个定时任务导致,任务有的时候会冲突出现奇怪的问题.如何解决请继续看. sched ...