📜 ⬆️ ⬇️

Bar coding: software implementation in C #

Greetings It's about creating a program that can encode information into a barcode. We consider two options: EAN13 and Code 128.

To begin with, let's see what the bar code provides and start with the EAN 13 format. Externally, the bar code consists of black and white stripes. Each of the strips according to the standard has a width of 0.33mm. And now let's look a little deeper. According to the standard, the information in the barcode is converted by the algorithm into a sequence of zeros and ones. The black bar corresponds to "1" and white, respectively, "0". Thus we get a bar code. What is described above is true for both formats, and now we come to the specifics of formats. EAN 13 code can actually have 12 characters of information (usually it is divided into a country code, a plant code, etc., but no one bothers you to create a bar code with any numbers). The 13 digit in the barcode (and he EAN 13 has them exactly 13) is a checksum. It is used to check the correctness of decoding, and depending on the appropriate coding table is selected. That's all according to the EAN 13 format. Detailed information on the coding algorithm and on the calculation of the check digit is here:

So, what we need to do for the software bar code generation (both formats):
  1. Configure the graphical interface so that 1 unit corresponds to 0.33 mm
  2. Convert code to binary sequence (from "0" and "1")
  3. Display the sequence in such a way that each unit is a rectangle with a width of 0.33 mm. I note that you can change the size of the stroke, but the standard says 0.33 mm.

')
Creating a program, I did this:
I created a form with fields for selecting the format, entering the code, adjusting the width of the stroke and the button for generating the code and saving the image to the clipboard. It turned out like this: Design
I checked with Abbyy FineReader 10 Professional:
Check
Each of the barcode formats I designed in the form of a separate class, which from the code forms a binary sequence. By the way, the EAN 13 format seemed to me a little harder to implement due to the fact that the table is changing there depending on the code. as a result, the code itself was drawn like this:
Bitmap bmp; private void button1_Click(object sender, EventArgs e) { Graphics g = panel1.CreateGraphics(); g.PageUnit = GraphicsUnit.Millimeter; if (comboBox1.Text == "EAN 13") { g.PageScale = scale; //g.DrawRectangle(new Pen(new SolidBrush(Color.Black)), 1, 1, 501, 1010); EAN13 ean = new EAN13(textBox1.Text); int cnt = ean.output.Length; panel1.Width = cnt ; float x = g.DpiX; int s1 =Convert.ToInt32( cnt * scale)+1; int s2 = Convert.ToInt32(s1 / 25.4); int res =(int) x * s2; panel1.Width = res; Draw(ref g, ean.output); } if (comboBox1.Text == "Code 128") { g.PageScale = scale; Code128B code = new Code128B(textBox1.Text); int cnt = code.output.Length; float x = g.DpiX; int s1 =Convert.ToInt32( cnt * scale)+1; int s2 = Convert.ToInt32(s1 / 25.4); int res =(int) x * s2; panel1.Width = res; Draw(ref g, code.output); bmp=new Bitmap(panel1.Width,panel1.Height); Graphics h=Graphics.FromImage(bmp); Draw(ref h, code.output); } } 

here the float scale is the width of the strip, specified from the form by means of DataBinding.

A little more talk about the code Code128. Here the coding is simply according to the table, but the structure has a zone of beginning, end, movement. By moving, I mean the transition from a separate code table to another (see Code128A, Code 128B, Code 128C)

This code, unlike the previous one, has an arbitrary length and can consist of all ASCII from 1 to 128. There is a lot of information about the algorithm, I took it from here: Source

( www.barcodeman.com/info/c128.php3 ).

Well, that's all, the main thing said. Finally I will attach my code, which I wrote to convert the information into a binary representation of the code. The code may not work correctly, it may not work optimally. I do not give any guarantee.

The code is here: pastebin.com/xsGVe9tG

Source: https://habr.com/ru/post/127799/


All Articles