当前位置:网站首页>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);
}
}
}
}
}
边栏推荐
- MONTHS_BETWEEN函数使用
- Typescript learning
- Selenium上传文件有多少种方式?不信你有我全!
- 使用Jmeter进行性能测试的这套步骤,涨薪2次,升职一次
- regular expression
- 《7天学会Go并发编程》第7天 go语言并发编程Atomic原子实战操作含ABA问题
- Windwos 8.1系统安装vmware tool插件报错的解决方法
- Solution to the error of VMware tool plug-in installed in Windows 8.1 system
- gomock mockgen : unknown embedded interface
- 关于davwa的SQL注入时报错:Illegal mix of collations for operation ‘UNION‘原因剖析与验证
猜你喜欢

BAT测试专家对web测试和APP测试的总结

Exclusive interview with millions of annual salary. What should developers do if they don't fix bugs?

Management system itclub (Part 2)

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

Codeforces Round #717 (Div. 2)

使用Jmeter进行性能测试的这套步骤,涨薪2次,升职一次

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

CUDA error:out of memory caused by insufficient video memory of 6G graphics card

登录凭证(cookie+session和Token令牌)

Interval DP of Changyou dynamic programming
随机推荐
The "business and Application Security Development Forum" held by the ICT Institute was re recognized for the security capability of Tianyi cloud
MONTHS_ Between function use
Stm32f107+lan8720a use stm32subemx to configure network connection +tcp master-slave +udp app
Simulink method for exporting FMU model files
How many ways does selenium upload files? I don't believe you have me all!
清华大学教授:软件测试已经走入一个误区——“非代码不可”
Experience sharing of meituan 20K Software Test Engineers
同花顺炒股软件可靠吗??安全嘛?
信通院举办“业务与应用安全发展论坛” 天翼云安全能力再获认可
软件测试自动化测试之——接口测试从入门到精通,每天学习一点点
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
美团20k软件测试工程师的经验分享
Solution to the error of VMware tool plug-in installed in Windows 8.1 system
YOLOv6:又快又准的目标检测框架开源啦
Figure countdownlatch and cyclicbarrier based on AQS queue
Common problems encountered by burp Suite
Go from introduction to actual combat - execute only once (note)
[LeetCode]515. Find the maximum value in each tree row
Gbase 8A OLAP analysis function cume_ Example of dist
读写分离-Mysql的主从复制