Programming     Travel Logs     Life Is Good     Surfing Online     About Me
The most accountable people have singular, public, and risky brands: Oprah, Trump, Kanye, Elon.
-Naval Ravikant
2018-05-06 21:49:30

Copy this link when reproducing:
http://www.casperlee.com/en/y/blog/73

/Images/20170909/01.jpg

/Images/20170909/02.jpg

/Images/20170909/03.jpg

/Images/20170909/04.jpg

/Images/20170909/05.jpg

/Images/20170909/06.jpg

1. Download the library "zxing.dll" from https://zxingnet.codeplex.com/

    Here is what I've got: "ZXing.Net.0.16.0.0.zip".

2. Unzip "ZXing.Net.0.16.0.0.zip" to the computer.

3. Run Visual Studio 2015.

4. Select "File -> New -> Project..." from the main menu.

/Images/20170909/101.jpg

5. Select "Windows Forms Application", type in the project name and solution name, click "OK" to create an application.

6. Change Form1.cs to MainForm.cs, and change the title of the form to "QR Code Generator".

7. Create a folder named "Lib" in the project root folder, and copy the library "zxing.dll" of proper version (net4.6 in my case) into the "Lib" folder.

/Images/20170909/102.jpg

8. Right click on the project, select "Add -> Reference..." from the popped menu.

/Images/20170909/103.jpg

/Images/20170909/104.jpg

9. Click "Browse..." and locate the "zxing.dll" in the "Lib" folder that we have just created.

/Images/20170909/105.jpg

10. Click "OK", the reference of the library "zxing.dll" will be added to this project.

11. Design the main form as the following image shows:

/Images/20170909/106.jpg

12. Here is the source code of the form:

using System;
using System.Drawing;
using System.Windows.Forms;
using ZXing;
using ZXing.Common;
using ZXing.QrCode;
using ZXing.Rendering;

namespace com.casperlee.examples.QRCodeGenerator
{
public partial class MainForm : Form
{
private static QRCodeWriter writer = new QRCodeWriter();
private static QRCodeReader reader = new QRCodeReader();

public MainForm()
{
InitializeComponent();
}

private void btnEncode_Click(object sender, EventArgs e)
{
BitMatrix matrix = writer.encode(tbContent.Text, BarcodeFormat.QR_CODE, 256, 256);

QrCodeEncodingOptions options = new QrCodeEncodingOptions();

BitmapRenderer r = new BitmapRenderer();
Bitmap b = r.Render(matrix, BarcodeFormat.QR_CODE, tbContent.Text, options);
pbQRCode.Image = b;
}

private void btnExport_Click(object sender, EventArgs e)
{
if (pbQRCode.Image == null)
{
return;
}

using (SaveFileDialog sfd = new SaveFileDialog())
{
sfd.Filter = "*.jpg|*.jpg";
if (sfd.ShowDialog(this) == DialogResult.OK)
{
pbQRCode.Image.Save(sfd.FileName);
}
}
}

private void btnImport_Click(object sender, EventArgs e)
{
using (OpenFileDialog ofd = new OpenFileDialog())
{
ofd.Filter = "*.jpg|*.jpg";
if (ofd.ShowDialog(this) == DialogResult.OK)
{
Image b = Image.FromFile(ofd.FileName);
pbQRCode.Image = b;
}
}
}

private void btnDecode_Click(object sender, EventArgs e)
{
if (pbQRCode.Image == null)
{
return;
}

Bitmap b = new Bitmap(pbQRCode.Image, pbQRCode.Width, pbQRCode.Height);
LuminanceSource source = new BitmapLuminanceSource(b);
Result result = reader.decode(new BinaryBitmap(new HybridBinarizer(source)));
tbContent.Text = result.Text;
}
}
}

13. Done!