当前位置:网站首页>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);
}
}
}
}
}
边栏推荐
- The karsonzhang/fastadmin addons provided by the system reports an error
- It smells good. Since I used Charles, Fiddler has been completely uninstalled by me
- Bean paste green protects your eyes
- [MySQL practice] query statement demonstration
- Deep learning has a new pit! The University of Sydney proposed a new cross modal task, using text to guide image matting
- Which method is called for OSS upload
- Secret script of test case design without leakage -- module test
- Windwos 8.1系统安装vmware tool插件报错的解决方法
- Common problems encountered by burp Suite
- Software test automation test -- interface test from entry to proficiency, learn a little every day
猜你喜欢

结构化机器学习项目(二)- 机器学习策略(2)

关于davwa的SQL注入时报错:Illegal mix of collations for operation ‘UNION‘原因剖析与验证

Luogu p5706 redistributing fertilizer and house water

使用Jmeter进行性能测试的这套步骤,涨薪2次,升职一次
![[MySQL] database function clearance Tutorial Part 2 (window function topic)](/img/03/2b37e63d0d482d5020b7421ac974cb.jpg)
[MySQL] database function clearance Tutorial Part 2 (window function topic)

爬虫笔记(1)- urllib

It smells good. Since I used Charles, Fiddler has been completely uninstalled by me

The create database of gbase 8A takes a long time to query and is suspected to be stuck

Dialogue with Qiao Xinyu: the user is the product manager of Wei brand, and zero anxiety defines luxury

Management system itclub (Part 2)
随机推荐
不外泄的测试用例设计秘籍--模块测试
百万年薪独家专访,开发人员不修复bug怎么办?
Stm32cubeide1.9.0\stm32cubemx 6.5 f429igt6 plus lan8720a, configure eth+lwip
The problem of minimum modification cost in two-dimensional array [conversion question + shortest path] (dijkstra+01bfs)
It smells good. Since I used Charles, Fiddler has been completely uninstalled by me
爬虫笔记(1)- urllib
大厂常用软件测试面试题三(附答案)
QT large file generation MD5 check code
[MySQL practice] query statement demonstration
How many ways does selenium upload files? I don't believe you have me all!
MONTHS_BETWEEN函数使用
BAT测试专家对web测试和APP测试的总结
Codeforces Round #719 (Div. 3)
[LeetCode]186. Flip word II in string
渗透学习-靶场篇-pikachu靶场详细攻略(持续更新中-目前只更新sql注入部分)
Test birds with an annual salary of 50w+ are using this: JMeter script development -- extension function
爬虫笔记(3)-selenium和requests
二维数组中修改代价最小问题【转换题意+最短路径】(Dijkstra+01BFS)
xpath
How to open an account for agricultural futures? How much is the handling charge for opening an account for futures? Who can give you a preferential handling charge?