当前位置:网站首页>C# 二维码生成、识别,去除白边、任意颜色
C# 二维码生成、识别,去除白边、任意颜色
2022-06-27 17:35:00 【天天代码码天天】

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);
}
}
}
}
}
边栏推荐
- VS code 运行yarn run dev 报yarn : 无法加载文件XXX的问题
- Solution to Maxwell error (MySQL 8.x connection)
- Teach you how to install Oracle 19C on Windows 10 (detailed picture and text with step on pit Guide)
- Row to column and column to row in MySQL
- Jinyuan's high-end IPO was terminated: it was planned to raise 750million Rushan assets and Liyang industrial investment were shareholders
- Informatics Orsay all in one 1335: [example 2-4] connected block
- 让单测变得如此简单 -- spock 框架初体验
- MySQL读取Binlog日志常见错误和解决方法
- 【云驻共创】高校数字化差旅建设“解决之道”
- 一对一关系
猜你喜欢

在线文本按行批量反转工具

Error reported by Huada MCU Keil_ Weak's solution

华大单片机KEIL报错_WEAK的解决方案

International School of Digital Economics, South China Institute of technology 𞓜 unified Bert for few shot natural language understanding

什么是SSR/SSG/ISR?如何在AWS上托管它们?

Bit. Store: long bear market, stable stacking products may become the main theme

The Fifth Discipline: the art and practice of learning organization

2022年第一季度消费金融APP用户洞察——总数达4479万人

Redis 原理 - String

数据分析师太火?月入3W?用数据告诉你这个行业的真实情况
随机推荐
工作流自动化 低代码是关键
过关斩将,擒“指针”(下)
Labelimg usage guide
NVIDIA Clara-AGX-Developer-Kit installation
9.OpenFeign服务接口调用
maxwell 报错(连接为mysql 8.x)解决方法
What is ICMP? What is the relationship between Ping and ICMP?
如何实现IM即时通讯“消息”列表卡顿优化
Four years of College for an ordinary graduate
校园书籍资源共享平台
Market status and development prospect forecast of the global infusion needle less connector industry in 2022
labelimg使用指南
Buzzer experiment based on stm32f103zet6 library function
Minmei new energy rushes to Shenzhen Stock Exchange: the annual accounts receivable exceeds 600million and the proposed fund-raising is 450million
使用logrotate对宝塔的网站日志进行自动切割
The IPO of Yuchen Airlines was terminated: Guozheng was proposed to raise 500million yuan as the major shareholder
What is ssr/ssg/isr? How do I host them on AWS?
華大單片機KEIL報錯_WEAK的解决方案
惊呆!原来 markdown 的画图功能如此强大!
Jinyuan's high-end IPO was terminated: it was planned to raise 750million Rushan assets and Liyang industrial investment were shareholders