当前位置:网站首页>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);
}
}
}
}
}
边栏推荐
- Flask application case
- Software defect management - a must for testers
- Interval DP of Changyou dynamic programming
- Conversation Qiao Xinyu: l'utilisateur est le gestionnaire de produits Wei Brand, zéro anxiété définit le luxe
- 01 golang environment construction
- 爬虫笔记(1)- urllib
- Yarn中RMApp、RMAppAttempt、RMContainer和RMNode状态机及其状态转移
- Selenium上传文件有多少种方式?不信你有我全!
- xpath
- [LeetCode]186. Flip word II in string
猜你喜欢

使用sqlite3语句后出现省略号 ... 的解决方法

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

Secret script of test case design without leakage -- module test

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

Read write separation master-slave replication of MySQL

Management system itclub (Part 2)

Experience sharing of meituan 20K Software Test Engineers

Interval DP of Changyou dynamic programming

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

Transformation from student to engineer
随机推荐
Software defect management - a must for testers
Golang uses regularity to match substring functions
Summary of gbase 8A database user password security related parameters
大厂常用软件测试面试题三(附答案)
二维数组中修改代价最小问题【转换题意+最短路径】(Dijkstra+01BFS)
Example of using gbase 8A OLAP function group by grouping sets
Matlab finds the position of a row or column in the matrix
AQS SOS AQS with me
Gbase 8A OLAP analysis function cume_ Example of dist
\w和[A-Za-z0-9_],\d和[0-9]等价吗?
mysql 大于 小于 等于符号的表示方法
[LeetCode]515. Find the maximum value in each tree row
如何做好功能测试?你确定不想知道吗?
[LeetCode]513. Find the value in the lower left corner of the tree
Day8 - cloud information project introduction and creation
Luogu p5706 redistributing fertilizer and house water
MONTHS_BETWEEN函数使用
How many ways does selenium upload files? I don't believe you have me all!
Transformation from student to engineer
對話喬心昱:用戶是魏牌的產品經理,零焦慮定義豪華