当前位置:网站首页>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);
}
}
}
}
}
边栏推荐
- Garbage collector driving everything -- G1
- How to encapsulate and call a library
- CMS 执行的七个阶段
- binder hwbinder vndbinder
- 如何封裝調用一個庫
- [notice of the Association] notice on holding summer special teacher training in the field of artificial intelligence and Internet of things
- 一种朴素的消失点计算方法
- OpenSSL client programming: SSL session failure caused by an obscure function
- Character interception triplets of data warehouse: substrb, substr, substring
- Teach you how to install Oracle 19C on Windows 10 (detailed picture and text with step on pit Guide)
猜你喜欢

深度学习和神经网络的介绍

国际数字经济学院、华南理工 | Unified BERT for Few-shot Natural Language Understanding(用于小样本自然语言理解的统一BERT)

Rxjs mergeMap 的使用场合

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

TIA博途_基于SCL语言制作模拟量输入输出全局库的具体方法

Function key input experiment based on stm32f103zet6 Library

OpenSSL client programming: SSL session failure caused by an obscure function

《第五项修炼》(The Fifth Discipline):学习型组织的艺术与实践
![[elt.zip] openharmony paper Club - memory compression for data intensive applications](/img/b3/ab915f0338174cba1a003edc262a5d.png)
[elt.zip] openharmony paper Club - memory compression for data intensive applications

Google Earth Engine(GEE)——ImageCollection (Error)遍历影像集合产生的错误
随机推荐
laravel框架中 定时任务的实现
【云驻共创】高校数字化差旅建设“解决之道”
華大單片機KEIL報錯_WEAK的解决方案
NVIDIA Clara-AGX-Developer-Kit installation
Is Guosen Securities a state-owned enterprise? Is it safe to open an account with Guosen Securities?
Photoshop-图层相关概念-LayerComp-Layers-移动旋转复制图层-复合图层
Row to column and column to row in MySQL
Differences between mongodb and MySQL
One week technical update express of substrate and Boca 20220425 - 20220501
Cucumber自动化测试框架使用
教你打印自己的日志 -- 如何自定义 log4j2 各组件
binder hwbinder vndbinder
Substrate及波卡一周技术更新速递 20220425 - 20220501
过关斩将,擒“指针”(下)
华大单片机KEIL添加ST-LINK解决方法
Solution of adding st-link to Huada MCU Keil
Stored procedures of PostgreSQL
明美新能源冲刺深交所:年应收账款超6亿 拟募资4.5亿
华大单片机KEIL报错_WEAK的解决方案
Comprehensively analyze the zero knowledge proof: resolve the expansion problem and redefine "privacy security"