The interleaved 2 of 5 barcode uses an scheme which alternates using bars and spaces to encode data with narrow and wide bars (or spaces). The encoding is further split up into odd and even encodings.

Interleaved 2 of 5 Barcode
The interleaved 2 of 5 barcode does not require a checksum digit, and can be any length.
The left and right guard bars are 1010 and 01101 respectively, and the digts are encoded alternatively with the odd and even encodings.
| Digit | Odd | Even |
| 0 | 1011001 | 0100110 |
| 1 | 1101011 | 0010100 |
| 2 | 1001011 | 0110100 |
| 3 | 1100101 | 0011010 |
| 4 | 1011011 | 0100100 |
| 5 | 1101101 | 0010010 |
| 6 | 1001101 | 0110010 |
| 7 | 1010011 | 0101100 |
| 8 | 1101001 | 0010110 |
| 9 | 1001001 | 0110110 |
Here is the code to encode the barcode. The full source code is available here
namespace BarcoderLib
{
public class BarcodeInter2of5
{
private string gLeftGuard = "1010";
private string gRightGuard = "01101";
private string[] gOdd = { "1011001", "1101011", "1001011", "1100101", "1011011", "1101101", "1001101", "1010011", "1101001", "1001001" };
private string[] gEven = { "0100110", "0010100", "0110100", "0011010", "0100100", "0010010", "0110010", "0101100", "0010110", "0110110" };
public Bitmap Encode(string message)
{
string encodedMessage;
Bitmap barcodeImage = new Bitmap(250, 100);
Graphics g = Graphics.FromImage(barcodeImage);
Validate(message);
encodedMessage = EncodeBarcode(message);
PrintBarcode(g, encodedMessage, message, 350, 100);
return barcodeImage;
}
private void Validate(string message)
{
Regex reNum = new Regex(@"^\d+$");
if (reNum.Match(message).Success == false)
{
throw new Exception("Encode string must be numeric");
}
}
private void PrintBarcode(Graphics g, string encodedMessage, string message, int width, int height)
{
SolidBrush whiteBrush = new SolidBrush(Color.White);
SolidBrush blackBrush = new SolidBrush(Color.Black);
Font textFont = new Font(FontFamily.GenericMonospace, 10, FontStyle.Regular);
g.FillRectangle(whiteBrush, 0, 0, width, height);
int xPos = 20;
int yTop = 10;
int barHeight = 50;
for (int i = 0; i < encodedMessage.Length; i++)
{
if (encodedMessage[i] == '1')
{
g.FillRectangle(blackBrush, xPos, yTop, 1, barHeight);
}
xPos += 1;
}
xPos = 20;
yTop += barHeight - 2;
for (int i = 0; i < message.Length; i++)
{
g.DrawString(message[i].ToString().Trim(), textFont, blackBrush, xPos, yTop);
xPos += 7;
}
}
private string EncodeBarcode(string message)
{
int i;
string encodedString = gLeftGuard;
for (i = 0; i < message.Length; i++)
{
if ((i % 2) == 0)
{
encodedString += gOdd[Convert.ToInt32(message[i].ToString())];
}
else
{
encodedString += gEven[Convert.ToInt32(message[i].ToString())];
}
}
encodedString += gRightGuard;
return encodedString;
}
}
}
Related posts:
- Barcodes in C#: Standard 2 of 5 The standard 2 of 5 barcode format encodes all of...
- Barcodes in C#: EAN-8 The EAN-8 barcode format is a compressed format of the...
- Barcodes in C#: UPC-A The UPC-A barcode format is very similar to the EAN-13...
- Barcodes in C#: UPC-2 UPC-2 is a simpler variation of the UPC-5 barcode designed...
- Barcodes in C#: MSI The MSI barcode, which also goes by the name of...
Related posts brought to you by Yet Another Related Posts Plugin.
Serge Meunier is a software developer living in Cape Town, South Africa. He loves programming, fencing, philosophy, feeding his internet addiction, and, of course, dogs.
Comments