当前位置:网站首页>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);
}
}
}
}
}
边栏推荐
- 對話喬心昱:用戶是魏牌的產品經理,零焦慮定義豪華
- Is flush stock trading software reliable?? Is it safe?
- 读写分离-Mysql的主从复制
- Gbase 8A method for reducing the impact on the system by controlling resource usage through concurrency during node replacement of V8 version
- Codeforces Round #717 (Div. 2)
- CUDA error:out of memory caused by insufficient video memory of 6G graphics card
- Professor of Tsinghua University: software testing has gone into a misunderstanding - "code is necessary"
- Day8 - cloud information project introduction and creation
- 百万年薪独家专访,开发人员不修复bug怎么办?
- Go from introduction to practice -- shared memory concurrency mechanism (notes)
猜你喜欢

Do280openshift access control -- Security Policy and chapter experiment

7 jours d'apprentissage de la programmation simultanée go 7 jours de programmation simultanée go Language Atomic Atomic Atomic actual Operation contains ABA Problems

读写分离-Mysql的主从复制

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

Dialogue with Qiao Xinyu: the user is the product manager of Wei brand, and zero anxiety defines luxury
How to design an elegant caching function

《7天学会Go并发编程》第7天 go语言并发编程Atomic原子实战操作含ABA问题

《7天学会Go并发编程》第六天 go语言Sync.cond的应用和实现 go实现多线程联合执行

Management system itclub (Part 2)

Système de gestion - itclub (II)
随机推荐
Experience sharing of meituan 20K Software Test Engineers
Système de gestion - itclub (II)
软件缺陷管理——测试人员必会
Yarn中RMApp、RMAppAttempt、RMContainer和RMNode状态机及其状态转移
扁平数组和JSON树的转换
MONTHS_BETWEEN函数使用
Fill in the blank of rich text test
Record a list object traversal and determine the size of the float type
How to prioritize the contents in the queue every second
Go from introduction to practice - error mechanism (note)
[MySQL practice] query statement demonstration
Management system itclub (Part 1)
Contest 2050 and Codeforces Round #718 (Div. 1 + Div. 2)
Gbase 8A OLAP analysis function cume_ Example of dist
Day8 - cloud information project introduction and creation
关于davwa的SQL注入时报错:Illegal mix of collations for operation ‘UNION‘原因剖析与验证
爬虫笔记(3)-selenium和requests
Is it safe to open a stock account through the account opening link given by the CICC securities manager? I want to open an account
百万年薪独家专访,开发人员不修复bug怎么办?
Where can I set the slides on the front page of CMS applet?