Simple EAN - 13 barcode generator

Here is a very simple EAN - 13 barcode generator written in C#.

using System;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using Karna.Barcode.Properties;
using System.Globalization;

namespace Karna.Barcode
{
    public class Ean13
    {
        private string barcodeId;
        private Image image;

        public Ean13(Image image)
        {
            this.image = image;
        }

        public string BarcodeId
        {
            get { return barcodeId; }
            set
            {
                if (value == null)
                    return;

                if (value.Length != 13)
                {
                    throw new ArgumentException("Wrong data length");
                }

                barcodeId = value;
                string s = null;


                string[,] encTable = new string[3, 10];
                int[,] parityToEnc = new int[10, 8];

                parityToEnc[1, 2] = 0;
                parityToEnc[1, 3] = 0;
                parityToEnc[1, 4] = 1;
                parityToEnc[1, 5] = 0;
                parityToEnc[1, 6] = 1;
                parityToEnc[1, 7] = 1;
                parityToEnc[2, 2] = 0;
                parityToEnc[2, 3] = 0;
                parityToEnc[2, 4] = 1;
                parityToEnc[2, 5] = 1;
                parityToEnc[2, 6] = 0;
                parityToEnc[2, 7] = 1;
                parityToEnc[3, 2] = 0;
                parityToEnc[3, 3] = 0;
                parityToEnc[3, 4] = 1;
                parityToEnc[3, 5] = 1;
                parityToEnc[3, 6] = 1;
                parityToEnc[3, 7] = 0;
                parityToEnc[4, 2] = 0;
                parityToEnc[4, 3] = 1;
                parityToEnc[4, 4] = 0;
                parityToEnc[4, 5] = 0;
                parityToEnc[4, 6] = 1;
                parityToEnc[4, 7] = 1;
                parityToEnc[5, 2] = 0;
                parityToEnc[5, 3] = 1;
                parityToEnc[5, 4] = 1;
                parityToEnc[5, 5] = 0;
                parityToEnc[5, 6] = 0;
                parityToEnc[5, 7] = 1;
                parityToEnc[6, 2] = 0;
                parityToEnc[6, 3] = 1;
                parityToEnc[6, 4] = 1;
                parityToEnc[6, 5] = 1;
                parityToEnc[6, 6] = 0;
                parityToEnc[6, 7] = 0;
                parityToEnc[7, 2] = 0;
                parityToEnc[7, 3] = 1;
                parityToEnc[7, 4] = 0;
                parityToEnc[7, 5] = 1;
                parityToEnc[7, 6] = 0;
                parityToEnc[7, 7] = 1;
                parityToEnc[8, 2] = 0;
                parityToEnc[8, 3] = 1;
                parityToEnc[8, 4] = 0;
                parityToEnc[8, 5] = 1;
                parityToEnc[8, 6] = 1;
                parityToEnc[8, 7] = 0;
                parityToEnc[9, 2] = 0;
                parityToEnc[9, 3] = 1;
                parityToEnc[9, 4] = 1;
                parityToEnc[9, 5] = 0;
                parityToEnc[9, 6] = 1;
                parityToEnc[9, 7] = 0;

                encTable[0, 0] = "0001101";
                encTable[0, 1] = "0011001";
                encTable[0, 2] = "0010011";
                encTable[0, 3] = "0111101";
                encTable[0, 4] = "0100011";
                encTable[0, 5] = "0110001";
                encTable[0, 6] = "0101111";
                encTable[0, 7] = "0111011";
                encTable[0, 8] = "0110111";
                encTable[0, 9] = "0001011";
                encTable[1, 0] = "0100111";
                encTable[1, 1] = "0110011";
                encTable[1, 2] = "0011011";
                encTable[1, 3] = "0100001";
                encTable[1, 4] = "0011101";
                encTable[1, 5] = "0111001";
                encTable[1, 6] = "0000101";
                encTable[1, 7] = "0010001";
                encTable[1, 8] = "0001001";
                encTable[1, 9] = "0010111";
                encTable[2, 0] = "1110010";
                encTable[2, 1] = "1100110";
                encTable[2, 2] = "1101100";
                encTable[2, 3] = "1000010";
                encTable[2, 4] = "1011100";
                encTable[2, 5] = "1001110";
                encTable[2, 6] = "1010000";
                encTable[2, 7] = "1000100";
                encTable[2, 8] = "1001000";
                encTable[2, 9] = "1110100";

                string lastString = "";

                lastString += "101";

                StringBuilder sb = new StringBuilder();

                for (int i = 2; i <= 7; ++i)
                {
                    string firstDigit_s = value[0].ToString();
                    int firstDigit_i = int.Parse(firstDigit_s, CultureInfo.InvariantCulture);

                    string curDigit_s = value[i-1].ToString();
                    int curDigit_i = int.Parse(curDigit_s, CultureInfo.InvariantCulture);
                    sb.Append(encTable[parityToEnc[firstDigit_i, i], curDigit_i]);
                }
                lastString += sb.ToString();

                lastString += "01010";

                StringBuilder sb2 = new StringBuilder();
                for (int i = 8; i <= 13; ++i)
                {
                    string curDigit_s = value[i-1].ToString();
                    int curDigit_i = int.Parse(curDigit_s, CultureInfo.InvariantCulture);
                    sb2.Append(encTable[2, curDigit_i]);
                }

                lastString += sb2.ToString();
                lastString += "101";

                Bitmap bitmap = new Bitmap(210, 146, PixelFormat.Format32bppArgb);
                Graphics canvas = Graphics.FromImage(bitmap);
                canvas.Clear(Color.White);

                for (int i = 1; i <= 190; i = i + 2)
                {
                    if (lastString[i / 2] == '1')
                    {
                        if (i <= 6 || (i >= 90 && i <= 100) || i >= 184)
                        {
                            canvas.DrawLine(Pens.Black, new Point(i + 14, 10), new Point(i + 14, 136));
                            canvas.DrawLine(Pens.Black, new Point(i + 15, 10), new Point(i + 15, 136));
                        }
                        else
                        {
                            canvas.DrawLine(Pens.Black, new Point(i + 14, 10), new Point(i + 14, 123));

                            canvas.DrawLine(Pens.Black, new Point(i + 15, 10), new Point(i + 15, 123));
                        }
                    }
                }

                canvas.SmoothingMode = SmoothingMode.HighQuality;
                canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
                canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
                canvas.CompositingQuality = CompositingQuality.HighQuality;

                Font font = new Font("Arial", 11);

                canvas.DrawString(value[0].ToString(), font, Brushes.Black, 4, 116);
                s = value[1] + " " + value[2] + " " + value[3] + " " + value[4] + 
                " " + value[5] + " " + value[6];
                canvas.DrawString(s, font, Brushes.Black, 30, 123);
                s = value[7] + " " + value[8] + " " + value[9] + " " + value[10] + 
               " " + value[11] + " " + value[12];
                canvas.DrawString(s, font, Brushes.Black, 121, 123);

                font.Dispose();
                canvas.Dispose();


                if (image != null)
                {
                    Graphics g = Graphics.FromImage(image);
                    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    g.SmoothingMode = SmoothingMode.HighQuality;
                    g.DrawImage(bitmap, 0, 0, image.Width, image.Height);
                    g.Dispose();
                }

                bitmap.Dispose();

            }
        }
    }
}

Comments

Popular posts from this blog

Quricol - QR code generator library

Quricol 2.0 - QR Code generator

EIDNative Library 2.0 released