C# 二维码生成、识别

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# 二维码生成、识别的更多相关文章
- Android 二维码 生成和识别(附Demo源码)
今天讲一下目前移动领域很常用的技术——二维码.现在大街小巷.各大网站都有二维码的踪迹,不管是IOS. Android.WP都有相关支持的软件.之前我就想了解二维码是如何工作,最近因为工作需要使用相关技 ...
- Android 二维码 生成和识别(转)
原博客地址 :http://www.cnblogs.com/weixing/archive/2013/08/28/3287120.html 还有几个写的也可以参考一下:http://www.itnos ...
- 玩转Android之二维码生成与识别
二维码,我们也称作QRCode,QR表示quick response即快速响应,在很多App中我们都能见到二维码的身影,最常见的莫过于微信了.那么今天我们就来看看怎么样在我们自己的App中集成二维码的 ...
- 【转】Android 二维码 生成和识别(附Demo源码)--不错
原文网址:http://www.cnblogs.com/mythou/p/3280023.html 今天讲一下目前移动领域很常用的技术——二维码.现在大街小巷.各大网站都有二维码的踪迹,不管是IOS. ...
- C# ZXing.Net生成二维码、识别二维码、生成带Logo的二维码(二)
1.使用ZXint.Net生成带logo的二维码 /// <summary> /// 生成带Logo的二维码 /// </summary> /// <param name ...
- Python 实现二维码生成和识别
今天突然想给自己自己做个头像,然后还是二维码的形式,这样只要扫一扫就可以访问我的主页.然后就开始自己的苦逼之路... 其实实现二维码java,c#,C++等都可以实现:由于自己正在学python,所以 ...
- python qrcode二维码生成与识别
二维码 二维码生成 1.用法 https://github.com/lincolnloop/python-qrcode 2.使用 简单实用 import qrcode # 二维码内容 data = & ...
- [开源]C#二维码生成解析工具,可添加自定义Logo
二维码又称 QR Code,QR 全称 Quick Response,是一个近几年来移动设备上超流行的一种编码方式,它比传统的 Bar Code 条形码能存更多的信息,也能表示更多的数据类型:比如:字 ...
- PHP二维码生成的方法(google APi,PHP类库,libqrencode等)
原文地址: http://blog.csdn.net/liuxinmingcode/article/details/7910975 ================================== ...
- android 二维码生成+扫描
android 二维码生成+扫描 1.在Android应用当中,很多时候都要用到二维码扫描,来避免让用户手动输入的麻烦. Google官方自己推出了一个二维码开源项目:ZXing库. 2.这里简单介绍 ...
随机推荐
- STM32F401CCU6与MFRC522接线及读取示例
硬件准备 stm32f401ccu6最小开发板 rfid-rc522开发板 usb2ttl转接, 可以用pl2303, ch340, CP2102, FT232 Mifare 1K卡, UID长度4字 ...
- 优雅地使用pt-archiver进行数据归档(转)
一.引言 最近由于业务需求,需要将公有云RDS(业务库)的大表数据归档至私有云MySQL(历史库),以缩减公有云RDS的体积和成本. 那么问题来了,数据归档的方式有n种,选择哪种呢?经过一番折腾,发现 ...
- ORA-22828 输入样式或替换参数超过了32k大小限制
今天调试程序报以下错误: ORA-22828: input pattern or replacement parameters exceed 32K size limit 22828. 00000 - ...
- 一秒出图?SDXL-Turbo实时AI绘画整合包下载
SDXL Turbo是一种快速生成的AI构图模型,它基于一种称为对抗性扩散蒸馏的新训练方法,该方法允许在1到4个步骤中以高图像质量对大规模基础图像扩散模型进行采样,并将其与对抗性损失相结合,以确保即使 ...
- Linux驱动开发笔记(二):ubuntu系统从源码编译安装gcc7.3.0编译器
前言 编译ubuntu驱动之前,发现使用的gcc是7.3.0,而使用apt管理和下载的都无法直接或间接安装gcc7.3.0,于是只能从源码安装gcc7.3.0编译器. GCC 概述 GCC ...
- mac更新系统后,提示xcrun的错误问题
pycharm运行代码终端报错: xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), ...
- 参数替换xargs
由于很多命令不支持管道|来传递参数,xargs用于产生某个命令的参数,xargs可以读入stdin的数据,并且以空格符或回车符将stdin的数据分隔为参数 示例: 创建10个用户 echo user{ ...
- Ubuntu如何卸载mysql
首先在终端中查看MySQL的依赖项:dpkg --list|grep mysql 卸载: sudo apt-get remove mysql-common 卸载:sudo apt-get autore ...
- ECMA Script Module(ES module)知识点
1.每个 ES Module 都是运行在单独的私有作用,ESM 自动采用严格模式,忽略use strict <script type="module">console. ...
- 【Azure 存储服务】存储在Azure Storage Table中的数据,如何按照条件进行删除呢?
问题描述 如何按条件删除 Storage Table 中的数据,如果Table中有大量的条记录需要删除,Java代码如何按条件删除 Table中的数据(Entity)? (通过Azure Storag ...