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. Js捕获异常的方法

    Js捕获异常的方法 JavaScript的异常主要使用try catch finally语句以及窗口对象window的onerror事件来捕获. try catch finally try catch ...

  2. db2伪表sysibm.sysdummy1

    $ db2 describe table sysibm.sysdummy1 Column                         Type      Type name             ...

  3. win32 - ListView_GetItemPosition的使用

    ListView_GetItemPosition : Gets the position of a list-view item 理论上获得桌面图标的正确方法是使用shell项,=> IFold ...

  4. virtualapp安装应用流程源码分析

    1. HomeActivity 为处理的入口 @Override protected void onActivityResult(int requestCode, int resultCode, In ...

  5. beego中数据库表创建

    package main import ( "fmt" "github.com/astaxie/beego/orm" _ "github.com/go ...

  6. 异常处理之raise A from B

    raise A from B 语句用于连锁chain异常 from 后面的B可以是: - 异常类 - 异常实例 - None 如果B是异常类或者异常实例,那么B会被设置为A的__cause__属性,表 ...

  7. 【LeetCode栈与队列#04】逆波兰表达式求值(仍然是经典的栈操作)

    逆波兰表达式求值 力扣题目链接(opens new window) 根据 逆波兰表示法,求表达式的值. 有效的运算符包括 + , - , * , / .每个运算对象可以是整数,也可以是另一个逆波兰表达 ...

  8. 用NavigationManager实现订单的列表和新增页面之间的导航

    1.在订单列表页面放置"新增"按钮 2.注入NavigationManager,按钮被点击后导航到新增订单的路径 3.测试一下,能工作了. 4.订单新增后,返回订单列表页面 5.测 ...

  9. 【Azure 环境】使用 az ad group create 时候遇见 Insufficient privileges to complete the operation

    问题描述 使用China Azure,通过Azure CLI 创建AAD组报错,提示权限不足 Insufficient privileges to complete the operation # 使 ...

  10. 【Azure 存储服务】关于对Azure Storage Account 的 Folder 权限管理和设定

    问题描述 在一个storage account下面有很多folder,需要对不同的folder设置不同的权限给到不同的用户来访问使用,怎么样设定比较合理? 问题解答 一:可以使用SAS共享访问签名进行 ...