当前位置:网站首页>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);
}
}
}
}
}
边栏推荐
- VMware virtual machine PE startup
- 大厂常用软件测试面试题三(附答案)
- 登录凭证(cookie+session和Token令牌)
- Open source technology exchange - Introduction to Chengying, a one-stop fully automated operation and maintenance manager
- Bean paste green protects your eyes
- Solution to the error of VMware tool plug-in installed in Windows 8.1 system
- MONTHS_ Between function use
- Codeforces Round #721 (Div. 2)
- Contest 2050 and Codeforces Round #718 (Div. 1 + Div. 2)
- 清华大学教授:软件测试已经走入一个误区——“非代码不可”
猜你喜欢

管理系统-ITclub(中)

Professor of Tsinghua University: software testing has gone into a misunderstanding - "code is necessary"

Management system itclub (Part 1)

Open source technology exchange - Introduction to Chengying, a one-stop fully automated operation and maintenance manager

Solution to the error of VMware tool plug-in installed in Windows 8.1 system

Fill in the blank of rich text test

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

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

Codeforces Round #723 (Div. 2)

Windwos 8.1系统安装vmware tool插件报错的解决方法
随机推荐
[LeetCode]30. Concatenate substrings of all words
如何做好功能测试?你确定不想知道吗?
Solve the problem that the virtual machine cannot be connected locally
Bean paste green protects your eyes
6G显卡显存不足出现CUDA Error:out of memory解决办法
Matlab finds the position of a row or column in the matrix
MONTHS_BETWEEN函数使用
gomock mockgen : unknown embedded interface
爬虫笔记(3)-selenium和requests
Yolov6: the fast and accurate target detection framework is open source
01 golang environment construction
[LeetCode]161. Edit distance of 1
Simulink method for exporting FMU model files
Deep learning has a new pit! The University of Sydney proposed a new cross modal task, using text to guide image matting
解决本地连接不上虚拟机的问题
Beijing University of Posts and Telecommunications - multi-agent deep reinforcement learning for cost and delay sensitive virtual network function placement and routing
Do280openshift access control -- Security Policy and chapter experiment
百万年薪独家专访,开发人员不修复bug怎么办?
crontab定时任务常用命令
[MySQL practice] query statement demonstration