Stupid me I thought, that just writing some text with a barcode font, would make the scanner read it. Seems I was wrong.
So after reading some docs about code128 barcodes, I learned that:
- The barcode starts with (103, 104 or 105 dependent of the type)
- Then comes the string itself
- Then comes the modulo 103 of the calculated sum of each char in the string multiplied with its position
- Then 106 is appended
My code is:
public string Str = "MADS";
public string Barcode = null;
public void OnGet()
{
int start = 104;
int end = 106;
int calc = start;
Barcode = start.ToString();
for (var i = 0; i < Str.Length; i++)
{
calc += (Convert.ToChar(Str[i]) - 32) * (i + 1);
Barcode += Str[i];
}
double rem = calc % 103;
Barcode += Convert.ToChar((int)rem + 32).ToString() + end;
Console.WriteLine(Barcode);
}
I'm not sure how much to include in the Barcode string, for the scanner to read it?:
- MADS,
- 104MADS,
- 104MADS,106
Or am I getting it all wrong?
Me references are:
Especially "Link 1", because I've tested that result with the scanner, and it works. Sadly I can't get my output to look like that.
Conclusion
After reading the comments and answers I think the best way for me is to use an existing library.
I chose NetBarcode GitHub link because it's .Net Core compatible.
11010000100
which means a 2 width thick line, followed by a 1 width thick gap, then a 1 width thick line and so on. – Folketing