Saturday, December 25, 2021

WinForm QR Generator & Decoder App

First of all, we create a ASP.NET WinForm and use ZXing package by installing using NuGet package Manager. Basic idea behind this is given below. We can transform text data into image format. Bar code is all about this conversion. Text can be represented as byte array or as byte stream. We write some text in a TextBox control. We read this text and use a function (provided by XZing package) to generate Barcode. BarcodeWriter class has Format property which accepts an enum value. BarcodeWriter class has Write method to write the formatted text. Bitmap class is used to get bits of the data. Go through the code to better understand it.



The Solution Explorer shows the ZXing package after its installation.


The UI design of the form is as shown below.
The complete code is as follows.
using System;
using System.Windows.Forms;
using System.IO;

namespace BarcodeAppWF
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void WordsTextBox_TextChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(WordsTextBox.Text))
            {
                BarcodePictureBox.Image = null;
            }
        }

        private void DecodeBarcodeButton_Click(object sender, EventArgs e)
        {
            ZXing.BarcodeReader bcReader = new ZXing.BarcodeReader();
            MemoryStream memoryStream = new MemoryStream();
            BarcodePictureBox.Image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            //byte[] bytes = memoryStream.ToArray();
            
            ZXing.Result result= bcReader.Decode(new System.Drawing.Bitmap(memoryStream));
            MessageBox.Show(result.ToString(),"QRCode Decoded",MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void QRGeneratorButton_Click(object sender, EventArgs e)
        {
            ZXing.BarcodeWriter bcWriter = new ZXing.BarcodeWriter();
            bcWriter.Format = ZXing.BarcodeFormat.QR_CODE;
            System.Drawing.Bitmap bitmap= bcWriter.Write(WordsTextBox.Text);
            BarcodePictureBox.Image = bitmap;
        }
    }
}

INPUT DATA
OUTPUT
OUTPUT

No comments:

Post a Comment

Hot Topics