当前位置:网站首页>C # QR code generation and recognition, removing white edges and any color
C # QR code generation and recognition, removing white edges and any color
2022-06-27 22:30:00 【Tiantian code Tiantian】
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"); // code
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));// Remove edges
pictureBox1.Image = bmp;
}
private static Bitmap RemoveWhiteMargin(ZXing.Common.BitMatrix bitMatrix, Bitmap bitmap)
{
// To obtain parameters
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);
// Intercept
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 = " image file (*.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(" The file format is not correct " + 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 = " Identify anomalies " + ex.Message;
}
if (result != null)
{
txtOutputForQR.Text = result.Text;
}
else
{
txtOutputForQR.Text = " Unrecognized information !";
}
}
private void btnSave_Click(object sender, EventArgs e)
{
SaveFileDialog sFD = new SaveFileDialog();
sFD.Filter = " Save the picture (*.png) |*.png| All the files (*.*) |*.*";
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);
}
}
}
}
}
边栏推荐
- 使用sqlite3语句后出现省略号 ... 的解决方法
- 结构化机器学习项目(二)- 机器学习策略(2)
- Where can I set the slides on the front page of CMS applet?
- 使用Fiddler模拟弱网测试(2G/3G)
- Login credentials (cookie+session and token token)
- \W and [a-za-z0-9_], \Are D and [0-9] equivalent?
- xpath
- Fill in the blank of rich text test
- Solve the problem that the virtual machine cannot be connected locally
- Matlab finds the position of a row or column in the matrix
猜你喜欢
Figure countdownlatch and cyclicbarrier based on AQS queue
Login credentials (cookie+session and token token)
Codeforces Round #717 (Div. 2)
《7天学会Go并发编程》第六天 go语言Sync.cond的应用和实现 go实现多线程联合执行
The problem of minimum modification cost in two-dimensional array [conversion question + shortest path] (dijkstra+01bfs)
Secret script of test case design without leakage -- module test
使用Jmeter进行性能测试的这套步骤,涨薪2次,升职一次
\w和[A-Za-z0-9_],\d和[0-9]等价吗?
解决本地连接不上虚拟机的问题
對話喬心昱:用戶是魏牌的產品經理,零焦慮定義豪華
随机推荐
Secret script of test case design without leakage -- module test
6G显卡显存不足出现CUDA Error:out of memory解决办法
Contest 2050 and Codeforces Round #718 (Div. 1 + Div. 2)
PCIe knowledge point -008: structure of PCIe switch
扁平数组和JSON树的转换
从学生到工程师的蜕变之路
Dynamic refresh mapper
Codeforces Round #719 (Div. 3)
average-population-of-each-continent
A method of go accessing gbase 8A database
Login credentials (cookie+session and token token)
Gbase 8A OLAP analysis function cume_ Example of dist
北京邮电大学|用于成本和延迟敏感的虚拟网络功能放置和路由的多智能体深度强化学习
Yarn中RMApp、RMAppAttempt、RMContainer和RMNode状态机及其状态转移
Analysis of stone merging
Golang uses regularity to match substring functions
关于davwa的SQL注入时报错:Illegal mix of collations for operation ‘UNION‘原因剖析与验证
Figure countdownlatch and cyclicbarrier based on AQS queue
[MySQL practice] query statement demonstration
Acwing weekly contest 57- digital operation - (thinking + decomposition of prime factor)