当前位置:网站首页>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);
}
}
}
}
}
边栏推荐
- Record a list object traversal and determine the size of the float type
- 大厂常用软件测试面试题三(附答案)
- CDH集群之YARN性能调优
- Day8 - cloud information project introduction and creation
- 单元测试界的高富帅,Pytest框架,手把手教学,以后测试报告就这么做~
- Day 7 of "learning to go concurrent programming in 7 days" go language concurrent programming atomic atomic actual operation includes ABA problem
- 爬虫笔记(3)-selenium和requests
- Remote invocation of microservices
- 01 golang environment construction
- How to do function test well? Are you sure you don't want to know?
猜你喜欢

average-population-of-each-continent

average-population-of-each-continent

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

渗透学习-sql注入过程中遇到的问题-针对sort=left(version(),1)的解释-对order by后接字符串的理解

Test birds with an annual salary of 50w+ are using this: JMeter script development -- extension function

Simulink method for exporting FMU model files

Common problems encountered by burp Suite

Go from introduction to practice -- definition and implementation of behavior (notes)

Interval DP of Changyou dynamic programming

BAT测试专家对web测试和APP测试的总结
随机推荐
Gao fushuai in the unit testing industry, pytest framework, hands-on teaching, will do this in the future test reports~
It smells good. Since I used Charles, Fiddler has been completely uninstalled by me
Codeforces Round #719 (Div. 3)
Management system itclub (medium)
Hash table - sum of arrays
Go from introduction to actual combat - execute only once (note)
Simulink method for exporting FMU model files
Flask application case
Conversion between flat array and JSON tree
MONTHS_BETWEEN函数使用
管理系統-ITclub(下)
Exclusive interview with millions of annual salary. What should developers do if they don't fix bugs?
Deep learning has a new pit! The University of Sydney proposed a new cross modal task, using text to guide image matting
爬虫笔记(3)-selenium和requests
Analysis of stone merging
年薪50W+的测试大鸟都在用这个:Jmeter 脚本开发之——扩展函数
Contest 2050 and Codeforces Round #718 (Div. 1 + Div. 2)
【mysql实战】查询语句实战演示
Start the start php
Bean paste green protects your eyes