Click here to Skip to main content
14,160,515 members
Click here to Skip to main content
Add your own
alternative version

Stats

96.2K views
42 bookmarked
Posted 29 Jun 2015
Licenced CPOL

Basic with QR Code using Zxing Library

, 10 Feb 2018
Rate this:
Please Sign up or sign in to vote.
Encoded, decoded your QR code using Zxing library

Introduction

In this tip, I'll cover a simple method to do with a QR code inside a standard control. 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

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)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Sanpath Sunggad
Thailand Thailand
I love advanced technology and like to know a lot about all kinds of tech

Education
Kasetsart University
Master of Science Program in Computer Science | 2016-Current

Kasetsart University
Bachelor of Science Program in Computer Science | 2011-2015

Languages
Thai | Native
English | Good

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionDYNAMIC QR CODE Pin
Member 1373770119-Jun-18 16:13
memberMember 1373770119-Jun-18 16:13 
QuestionDoes Zxing library use native code in C or C++? Pin
Member 852948522-Mar-18 3:23
memberMember 852948522-Mar-18 3:23 
QuestionARABIC QR CODE Pin
Member 1054250021-Mar-18 0:53
memberMember 1054250021-Mar-18 0:53 
QuestionARABIC QR CODE Pin
Member 1054250021-Mar-18 0:50
memberMember 1054250021-Mar-18 0:50 
QuestionPDF417 Pin
tungtranthanh7-Mar-18 16:10
membertungtranthanh7-Mar-18 16:10 
Questionของผมเป็น C# 2.0 ใช้ตัวแปร var ไม่ได้ ต้องทำวิธีไหน แทนครับ Pin
Member 1367926718-Feb-18 21:28
memberMember 1367926718-Feb-18 21:28 
AnswerRe: ของผมเป็น C# 2.0 ใช้ตัวแปร var ไม่ได้ ต้องทำวิธีไหน แทนครับ Pin
Sanpath Sunggad24-Feb-18 18:20
memberSanpath Sunggad24-Feb-18 18:20 
QuestionNice Article, some suggestions... Pin
AngelBlueSky9-Feb-18 8:59
memberAngelBlueSky9-Feb-18 8:59 
PraiseRe: Nice Article, some suggestions... Pin
Sanpath Sunggad10-Feb-18 4:47
memberSanpath Sunggad10-Feb-18 4:47 
GeneralRe: Nice Article, some suggestions... Pin
AngelBlueSky11-Feb-18 23:37
memberAngelBlueSky11-Feb-18 23:37 
Questionis any declration need for 'options' in formload() function ? Pin
Member 1365370731-Jan-18 1:17
memberMember 1365370731-Jan-18 1:17 
SuggestionRe: is any declration need for 'options' in formload() function ? Pin
Sanpath Sunggad3-Feb-18 7:21
memberSanpath Sunggad3-Feb-18 7:21 
AnswerRe: is any declration need for 'options' in formload() function ? Pin
Sanpath Sunggad4-Feb-18 23:14
memberSanpath Sunggad4-Feb-18 23:14 
GeneralRe: is any declration need for 'options' in formload() function ? Pin
Member 136537078-Feb-18 18:02
memberMember 136537078-Feb-18 18:02 
QuestionQR Code Pin
saticin27-Jan-18 1:19
membersaticin27-Jan-18 1:19 
PraiseRe: QR Code Pin
Sanpath Sunggad3-Feb-18 7:46
memberSanpath Sunggad3-Feb-18 7:46 
QuestionReading Barcode from Camera Pin
tarunbhardwaj28-Aug-17 23:25
membertarunbhardwaj28-Aug-17 23:25 
SuggestionRe: Reading Barcode from Camera Pin
Sanpath Sunggad3-Feb-18 7:42
memberSanpath Sunggad3-Feb-18 7:42 
QuestionIS it possible to create vcard? Pin
jfshimul9-May-17 9:07
professionaljfshimul9-May-17 9:07 
AnswerRe: IS it possible to create vcard? Pin
Sanpath Sunggad24-May-17 16:21
memberSanpath Sunggad24-May-17 16:21 
QuestionGreat job Pin
akulovac27-Apr-17 22:02
memberakulovac27-Apr-17 22:02 
PraiseRe: Great job Pin
Sanpath Sunggad24-May-17 16:11
memberSanpath Sunggad24-May-17 16:11 
PraiseMy Vote of 5 Pin
prckmi6-Apr-17 19:32
memberprckmi6-Apr-17 19:32 
PraiseRe: My Vote of 5 Pin
Sanpath Sunggad24-May-17 16:08
memberSanpath Sunggad24-May-17 16:08 
QuestionQR Code Pin
Member 1048524213-Mar-17 1:30
memberMember 1048524213-Mar-17 1:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.190518.1 | Last Updated 10 Feb 2018
Article Copyright 2015 by Sanpath Sunggad
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid