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 ZXing; namespace QRCode
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
} private void btnQRCode_Click(object sender, EventArgs e)
{
string content = txtInputForQR.Text; Dictionary<EncodeHintType, Object> hints = new Dictionary<EncodeHintType, object>();
hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H);
hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); //编码
MultiFormatWriter writer = new MultiFormatWriter(); ZXing.Common.BitMatrix bm = writer.encode(content, ZXing.BarcodeFormat.QR_CODE, 300, 300, hints);
BarcodeWriter barcodeWriter = new BarcodeWriter();
//barcodeWriter.Renderer = new ZXing.Rendering.BitmapRenderer
//{
// Background = Color.FromArgb(242, 241, 250),
// Foreground = Color.FromArgb(46, 51, 57)
//}; barcodeWriter.Renderer = new ZXing.Rendering.BitmapRenderer
{
Background = Color.White,
Foreground = Color.Green
}; System.Drawing.Bitmap bmp = RemoveWhiteMargin(bm, barcodeWriter.Write(bm));//去除边 pictureBox1.Image = bmp;
} private static Bitmap RemoveWhiteMargin(ZXing.Common.BitMatrix bitMatrix, Bitmap bitmap)
{
//获取参数
int[] rec = bitMatrix.getEnclosingRectangle();
int left = rec[0];
int top = rec[1];
int width = rec[2];
int height = rec[3];
Bitmap newImg = new Bitmap(width, height);
Graphics g = Graphics.FromImage(newImg);
//截取
g.DrawImage(bitmap, 0, 0, new Rectangle(left, top, newImg.Width, newImg.Height), GraphicsUnit.Pixel);
return newImg;
} private void btnQRDeCode_Click(object sender, EventArgs e)
{ OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "图像文件(*.jpg;*.jpeg;*.gif;*.bmp;*.png)|*.jpg;*.jpeg;*.gif;*.bmp;*.png"; if (openFileDialog1.ShowDialog() != DialogResult.OK)
{
return;
}
Image img = null;
try
{
img = Image.FromFile(openFileDialog1.FileName);
pictureBox1.Image = img;
}
catch (Exception ex)
{
MessageBox.Show("文件格式不正确" + ex.Message);
}
BarcodeReader reader = new BarcodeReader();
reader.Options.CharacterSet = "UTF-8";
Result result = null;
try
{
result = reader.Decode(new Bitmap(img));
}
catch (Exception ex)
{ txtInputForQR.Text = "识别异常" + ex.Message;
} if (result != null)
{
txtOutputForQR.Text = result.Text;
}
else
{
txtOutputForQR.Text = "未识别出信息!";
}
} private void btnSave_Click(object sender, EventArgs e)
{
SaveFileDialog sFD = new SaveFileDialog();
sFD.Filter = "保存图片(*.png) |*.png|所有文件(*.*) |*.*";
sFD.DefaultExt = "*.png|*.png";
sFD.AddExtension = true;
if (sFD.ShowDialog() == DialogResult.OK)
{
if (sFD.FileName != "")
{
Bitmap bitmap = (Bitmap)pictureBox1.Image;
bitmap.Save(sFD.FileName, System.Drawing.Imaging.ImageFormat.Png);
} }
}
}
}

Demo下载

C# 二维码生成、识别,去除白边、任意颜色的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  8. C# - VS2019调用ZXing.NET实现条码、二维码和带有Logo的二维码生成

    前言 C# WinFrm程序调用ZXing.NET实现条码.二维码和带有Logo的二维码生成. ZXing.NET导入 GitHub开源库 ZXing.NET开源库githib下载地址:https:/ ...

  9. QRCode二维码生成方案及其在带LOGO型二维码中的应用(1)

    原文:QRCode二维码生成方案及其在带LOGO型二维码中的应用(1) 提要:很多公司为商业宣传之需,常将企业LOGO加入二维码中,但如果LOGO遮挡区域足够地大,二维码就变得无法识别.那么,有没有一 ...

  10. JAVA实现二维码生成加背景图

    pom.xml依赖 <!-- 二维码生成 -->         <!-- https://mvnrepository.com/artifact/com.google.zxing/c ...

随机推荐

  1. QT C++工程CI环境笔记

    开发环境 Ubuntu18.04 or Ubuntu20.04 Qt Creator 4.6.x (Based on Qt 5.11.x) APT list: apt-transport-https ...

  2. 初探富文本之文档diff算法

    初探富文本之文档diff算法 当我们实现在线文档的系统时,通常需要考虑到文档的版本控制与审核能力,并且这是这是整个文档管理流程中的重要环节,那么在这个环节中通常就需要文档的diff能力,这样我们就可以 ...

  3. ORACLE ROLLUP和CUBE介绍

    http://blog.csdn.net/wanghai__/article/details/4817920 ------------------ ROLLUP,是GROUP BY子句的一种扩展,可以 ...

  4. 解决etcd集群空间占用问题 mvcc: database space exceeded

    一.查看状态 ./etcdctl --write-out=table endpoint status 可以看到空间占用(DB SIZE).本次是清理过了.原本达到了2G(为默认上限) 二.查看当前版本 ...

  5. mysql-数据类型,类型约束,联合唯一约束,表与表之间的关系,存储引擎---day36

    # ### char varchar(补充) char 字符长度 255个 varchar 字符长度 21845个 # ### part1 数据类型 -时间 date YYYY-MM-DD 年月日(结 ...

  6. ASP.NET 上传文件导入Excel

    前言 本文对应的场景是导入Excel数据,Excel对应的字段都配置在xml文件中.截图如下: 代码实战 工具类 实体类:XMLReadModel.cs public class XMLReadMod ...

  7. 【Azure 应用服务】如何从App Service for Linux 的环境中下载Container中非Home目录下的文件呢?

    问题描述 在App Service for Linux的环境中,我们能通过SSH进入到Container的环境中,并且可以通过在kudu站点的URL后面添加 /newui 打开一个适用于Linux环境 ...

  8. Volatile关键字原理

    转载请标明:https://www.cnblogs.com/tangZH/p/15113505.html 一.如果一个变量被volatile关键字修饰,那么所有线程都是可见的.所谓可见就是,当一条线程 ...

  9. CentOS 设置系统时间与网络时间同步

    CentOS 设置系统时间与网络时间同步 一.Linux的时间分为(两种) System Clock(系统时间) 指当前Linux Kernel中的时间 Real Time Clock (硬件时间,简 ...

  10. 开源短信项目 platform-sms 发布了新版本 0.5.0

    Github : https://github.com/makemyownlife/platform-sms 短信服务 platform-sms 0.5.0 发布 ,新的版本做了非常多的优化和改进. ...