65.9K
CodeProject is changing. Read more.
Home

Basic with QR Code using Zxing Library

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.55/5 (24 votes)

Jun 29, 2015

CPOL

2 min read

viewsIcon

228838

Encoded, decoded your QR code using Zxing library

Introduction

For reference, I will use ZXing.Net library from zxingnet.codeplex.com.

First, you will need to download the ZXing.Net library from zxingnet.codeplex.com. Extract the contents of the file you have downloaded and reference the library that fits your needs in your project.

Download link: ZXing.Net.0.14.0.0.zip (Released: Apr 7, 2014)

The Zxing.Net project now migrated to https://github.com/micjahn/ZXing.Net (Reference: Feb 3,2018)

First please add this references to your Project References before using Zxing.Net libraries (Reference: Feb 3,2018)

Background

About ZXing.Net
A library which supports decoding and generating of barcodes (like QR Code, PDF 417, EAN, UPC, Aztec, Data Matrix, Codabar) within images.

Using the Code

source code ทั้งหมดที่ใช้ในบทความนี้ครับ : )

https://github.com/lazycat18/Workshop-Basic-with-QR-Code-using-Zxing-Library

Design for this tip looks like this... : )

Step 1: Using Zxing Library

using Zxing library from your reference.

using ZXing.Common;
using ZXing;
using ZXing.QrCode;

Step 2: Coding in your Form Load

For Width or Height, you can change it to any value you want. :)

Note: Please write this first:

QrCodeEncodingOptions options = new QrCodeEncodingOptions();

options =  new QrCodeEncodingOptions
{
    DisableECI = true,
    CharacterSet = "UTF-8",
    Width = 250,
    Height = 250,
};
var writer = new BarcodeWriter();
writer.Format = BarcodeFormat.QR_CODE;
writer.Options = options; 

//update : Feb 3,2018

Step 3: Create Generate QR Code Button

Generate your text from textBox1 to QR Code format and show this result in pictureBox1:

if (String.IsNullOrWhiteSpace(textBox1.Text)||String.IsNullOrEmpty(textBox1.Text)){
          pictureBox1.Image = null;
          MessageBox.Show("Text not found", "Oops!",MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else{
          var qr = new ZXing.BarcodeWriter();
          qr.Options = options;
          qr.Format = ZXing.BarcodeFormat.QR_CODE;
          var result = new Bitmap(qr.Write(textBox1.Text.Trim()));
          pictureBox1.Image = result;
          textBox1.Clear();
}
//update : Feb 3,2018

Step 4: Create Decode QR Code Button

Decoded your QR Code from pictureBox1 to plain text and show this result in textBox1:

try{
    Bitmap bitmap = new Bitmap(pictureBox1.Image);
    BarcodeReader reader = new BarcodeReader { AutoRotate = true, TryInverted = true };
    Result result = reader.Decode(bitmap);
    string decoded = result.ToString().Trim();
    textBox1.Text = decoded;
}catch (Exception){
    MessageBox.Show("Image not found", "Oops!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
//update : Feb 3,2018

Step 5: Create Browse a Local Image Button

Find where image files are stored on your computer.

OpenFileDialog open = new OpenFileDialog();
      if (open.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            var qr = new ZXing.BarcodeWriter();
            qr.Options = options;
            qr.Format = ZXing.BarcodeFormat.QR_CODE;
            pictureBox1.ImageLocation = open.FileName;
}
       
//update : Feb 3,2018

Step 6: Create Download Button

Save your QR code with file format type like this *.png, *. jpg, *.bmp, *.gif.

if(pictureBox1.Image == null)
            {
                MessageBox.Show("Image not found", "Oops!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                SaveFileDialog save = new SaveFileDialog();
                save.CreatePrompt = true;
                save.OverwritePrompt = true;
                save.FileName = "QR";
                save.Filter = "PNG|*.png|JPEG|*.jpg|BMP|*.bmp|GIF|*.gif";
                if (save.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    pictureBox1.Image.Save(save.FileName);
                    save.InitialDirectory = Environment.GetFolderPath
                                (Environment.SpecialFolder.Desktop);
                }
            }
//update : Feb 3,2018

Points of Interest

  • Encoding text from Thai language and decoding this. Now show the correct result!! You can try this with your QR code reader.

History

  • 29/06/2015: First release for www.codeproject.com
  • 03/02/2018: Second update for several years. This update comes with new Zxing.Net Libraries using NuGet Package (easy to use)