VintaSoft Barcode .NET SDK Discussions
Questions, comments and suggestions concerning VintaSoft Barcode .NET SDK.
Board index < VintaSoft Barcode < VintaSoft Barcode .NET SDK Discussions

var items = new List<ValueItemBase>();
items.Add(NonDataFlags.CreateECICharacter(4));
items.Add(new BinaryValueItem(iso88592.GetBytes("Д…ЕјЕ›ЕєД™Д‡Е„ГіЕ‚Д„Е»ЕљЕ№ДД†ЕѓГ“ЕЃ")));
items.Add(NonDataFlags.CreateECICharacter(7));
items.Add(new BinaryValueItem(iso88595.GetBytes("АБВГДЕЖЗРЙКЛМНОП")));
items.Add(NonDataFlags.CreateECICharacter(28));
items.Add(new BinaryValueItem(big5.GetBytes("分切刈勻勾勿化匹午升卅卞厄友及反")));
dm.Settings.ValueItems = items.ToArray();
This way it works, but it's all encoded as Base256, which is not optimal, because it encodes 1 character on 1 byte. Text modes use 2 bytes for 3 characters, so it takes less space. We rather don't expect many special characters so it would be better to encode it as Text. According to Datamatrix specs it's possible to change default encoding with proper ECI codeword and then use normal ASCII/Text encoding to encode extended characters. Depending on the mode it will require a special codeword preceding the actual extended character, so each special character will actually take 2 codewords, but there should be only quite a few of them. The text we encode might be up to about 500 characters so it seems that we can have save some bytes by prefering Text over Base256 encodation of diacritics. This is especially important as our customers have strict requirements for the barcodes we generate and we cannot exceed specified sizes.public static void Test()
{
Encoding iso88591 = Encoding.GetEncoding(28591);
Encoding iso88595 = Encoding.GetEncoding(28595);
Encoding iso88592 = Encoding.GetEncoding(28592);
Encoding big5 = Encoding.GetEncoding(950);
// create the barcode value
List<ValueItemBase> items = new List<ValueItemBase>();
EncodeText(items, "Д…ЕјЕ›ЕєД™Д‡Е„ГіЕ‚Д„Е»ЕљЕ№ДД†ЕѓГ“ЕЃ", iso88592);
EncodeText(items, "АБВГДЕЖЗРЙКЛМНОП", iso88595);
EncodeText(items, "分切刈勻勾勿化匹午升卅卞厄友及反", big5);
EncodeText(items, "test!", iso88591);
// write the barcode
BarcodeWriter writer = new BarcodeWriter();
writer.Settings.Barcode = BarcodeType.DataMatrix;
writer.Settings.ValueItems = items.ToArray();
Bitmap bmp = writer.GetBarcodeAsBitmap();
// read the barcode
BarcodeReader reader = new BarcodeReader();
reader.Settings.ScanBarcodeTypes = BarcodeType.DataMatrix;
ValueItemBase[] readItems = reader.ReadBarcodes(bmp)[0].ValueItems;
// parse the barcode value
string value = ParseBarcodeValue(readItems);
// check the barcode value
if (value !=
"Д…ЕјЕ›ЕєД™Д‡Е„ГіЕ‚Д„Е»ЕљЕ№ДД†ЕѓГ“ЕЃ" +
"АБВГДЕЖЗРЙКЛМНОП" +
"分切刈勻勾勿化匹午升卅卞厄友及反" +
"test!")
throw new ApplicationException();
}
// Encodes text using specified encoding.
private static void EncodeText(List<ValueItemBase> items, string value, Encoding encoding)
{
int eciNumber = GetEciNumber(encoding);
// add ECI character
items.Add(NonDataFlags.CreateECICharacter(GetEciNumber(encoding)));
// string -> bytes
byte[] bytes = encoding.GetBytes(value);
// bytes -> text
char[] textChars = new char[bytes.Length];
for (int i = 0; i < textChars.Length; i++)
textChars[i] = (char)bytes[i];
// create and add text value item
items.Add(new TextValueItem(new string(textChars)));
}
// Parses the barcode value with ECI characters.
private static string ParseBarcodeValue(ValueItemBase[] items)
{
StringBuilder result = new StringBuilder();
// default encoding
Encoding currentEncoding = GetEncoding(3);
// for each value item
for (int i = 0; i < items.Length; i++)
{
if (items[i] is ECICharacterValueItem)
{
// change current encoding
currentEncoding = GetEncoding(((ECICharacterValueItem)items[i]).ECIAssignmentNumber);
continue;
}
// decode text value item
string text = ((TextValueItem)items[i]).Value;
byte[] bytes = new byte[text.Length];
for (int j = 0; j < text.Length; j++)
bytes[j] = (byte)text[j];
result.Append(currentEncoding.GetString(bytes));
}
return result.ToString();
}
// Gets the ECI number for specified encoding.
private static int GetEciNumber(Encoding encoding)
{
switch (encoding.CodePage)
{
case 28591:
return 3;
case 28592:
return 4;
case 28595:
return 7;
case 950:
return 28;
}
throw new NotImplementedException();
}
// Gets the Encoding for specified ECI number.
private static Encoding GetEncoding(int eciNumber)
{
switch (eciNumber)
{
case 3:
return Encoding.GetEncoding(28591);
case 4:
return Encoding.GetEncoding(28592);
case 7:
return Encoding.GetEncoding(28595);
case 28:
return Encoding.GetEncoding(950);
}
throw new NotImplementedException();
}
Best regards, Alexander