当前位置:网站首页>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);
}
}
}
}
}
边栏推荐
- 【建议收藏】ABAP随笔-EXCEL-4-批量导入-推荐
- The Fifth Discipline: the art and practice of learning organization
- [elt.zip] openharmony paper Club - memory compression for data intensive applications
- 如何封裝調用一個庫
- Cloud native database: the outlet of the database, you can also take off
- New Zhongda chongci scientific and Technological Innovation Board: annual revenue of 284million and proposed fund-raising of 557million
- 基于STM32F103ZET6库函数跑马灯实验
- Vscode suggests that you enable gopls. What exactly is it?
- Erreur Keil de Huada Single Chip Computer La solution de Weak
- openssl客户端编程:一个不起眼的函数导致的SSL会话失败问题
猜你喜欢

全面解析零知识证明:消解扩容难题 重新定义「隐私安全」

Keras深度学习实战(12)——面部特征点检测

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

实战回忆录:从Webshell开始突破边界

External interrupt experiment based on stm32f103zet6 library function

Introduction to deep learning and neural networks

电脑安全证书错误怎么处理比较好

新中大冲刺科创板:年营收2.84亿 拟募资5.57亿

Core dynamic Lianke rushes to the scientific innovation board: with an annual revenue of 170million yuan, Beifang Electronics Institute and Zhongcheng venture capital are shareholders

Erreur Keil de Huada Single Chip Computer La solution de Weak
随机推荐
Market status and development prospect forecast of global handheld ventilator industry in 2022
Market status and development prospect forecast of global active quality control air sampler industry in 2022
Labelimg usage guide
如何封装调用一个库
PCB线路板蛇形布线要注意哪些问题?
华大单片机KEIL报错_WEAK的解决方案
Current market situation and development prospect forecast of the global ductless heating, ventilation and air conditioning system industry in 2022
Vs code runs "yarn run dev" and reports "yarn": the file XXX cannot be loaded
openssl客户端编程:一个不起眼的函数导致的SSL会话失败问题
Usage of rxjs mergemap
maxwell 报错(连接为mysql 8.x)解决方法
Cdga | what is the core of digital transformation in the transportation industry?
TIA博途_基于SCL语言制作模拟量输入输出全局库的具体方法
网络传输是怎么工作的 -- 详解 OSI 模型
别焦虑了,这才是中国各行业的工资真相
Function key input experiment based on stm32f103zet6 Library
Vscode suggests that you enable gopls. What exactly is it?
GIS遥感R语言学习看这里
Comment encapsuler un appel à une bibliothèque
Cucumber自动化测试框架使用