Генерация и распознавание штрих-кодов Han Xin Code в .NET
Категория: Штрих-коды; .NET
4 мая 2020


/// <summary>
/// Сгенерировать штрих-код Han Xin Code в виде растрового изображения.
/// </summary>
public void GenerateHanXinCodeBarcodeAsRasterImage()
{
// создать генератор штрих-кодов
Vintasoft.Barcode.BarcodeWriter barcodeWriter = new Vintasoft.Barcode.BarcodeWriter();
// установить настройки генератора штрих-кодов
barcodeWriter.Settings.Barcode = Vintasoft.Barcode.BarcodeType.HanXinCode;
barcodeWriter.Settings.Value = "012345abcde";
// получить штрих-код как растровое изображение
using (System.Drawing.Image image = barcodeWriter.GetBarcodeAsBitmap())
{
// сохранить полученное изображение в файл
image.Save("HanXinCodeBarcode.png");
}
}
/// <summary>
/// Сгенерировать штрих-код Han Xin Code в векторной форме.
/// </summary>
public System.Drawing.Drawing2D.GraphicsPath GenerateHanXinCodeBarcodeAsGraphicsPath()
{
// создать генератор штрих-кодов
Vintasoft.Barcode.BarcodeWriter barcodeWriter = new Vintasoft.Barcode.BarcodeWriter();
// установить настройки генератора штрих-кодов
barcodeWriter.Settings.Barcode = Vintasoft.Barcode.BarcodeType.HanXinCode;
barcodeWriter.Settings.Value = "012345abcde";
// вернуть штрих-код как объект GraphicsPath
return barcodeWriter.GetBarcodeAsGraphicsPath();
}
/// <summary>
/// Сгенерировать штрих-код Han Xin Code в виде SVG изображения.
/// </summary>
public string GenerateHanXinCodeBarcodeAsSvgImage()
{
// создать генератор штрих-кодов
Vintasoft.Barcode.BarcodeWriter barcodeWriter = new Vintasoft.Barcode.BarcodeWriter();
// установить настройки генератора штрих-кодов
barcodeWriter.Settings.Barcode = Vintasoft.Barcode.BarcodeType.HanXinCode;
barcodeWriter.Settings.Value = "012345abcde";
// вернуть штрих-код как строку с данными SVG изображения
return barcodeWriter.GetBarcodeAsSvgFile();
}
/// <summary>
/// Распознать штрих-код Han Xin Code в изображении.
/// </summary>
public void RecognizeHanXinCodeBarcode()
{
// создать распознаватель штрих-кодов
using (Vintasoft.Barcode.BarcodeReader reader = new Vintasoft.Barcode.BarcodeReader())
{
// указать, что распознаватель должен искать штрих-коды Han Xin Code
reader.Settings.ScanBarcodeTypes = Vintasoft.Barcode.BarcodeType.HanXinCode;
// указать, что распознаватель должен искать только горизонтальные и вертикальные штрих-коды
reader.Settings.ScanDirection = Vintasoft.Barcode.ScanDirection.Horizontal | Vintasoft.Barcode.ScanDirection.Vertical;
// распознать штрих-коды в файле изображения
Vintasoft.Barcode.IBarcodeInfo[] barcodeInfos = reader.ReadBarcodes("HanXinCodeBarcode.png");
// если штрих-коды не найдены
if (barcodeInfos.Length == 0)
{
Console.WriteLine("Штрих-коды не найдены.");
}
// если штрих-коды найдены
else
{
// получить информацию о найденных штрих-кодах
Console.WriteLine(string.Format("{0} barcode(s) found:", barcodeInfos.Length));
Console.WriteLine();
for (int i = 0; i < barcodeInfos.Length; i++)
{
Vintasoft.Barcode.IBarcodeInfo barcodeInfo = barcodeInfos[i];
Console.WriteLine(string.Format("[{0}:{1}]", i + 1, barcodeInfo.BarcodeType));
Console.WriteLine(string.Format("Значение: {0}", barcodeInfo.Value));
Console.WriteLine(string.Format("Регион: {0}", barcodeInfo.Region));
Console.WriteLine();
}
}
}
}
/// <summary>
/// Распознать и протестировать качество печати штрих-кода Han Xin Code в изображении.
/// </summary>
public void ReadBarcodesAndTestBarcodePrintQuality()
{
// загрузить изображение штрих-кода из файла
using (System.Drawing.Image barcodeImage = System.Drawing.Image.FromFile("HanXinCodeBarcode.png"))
{
// создать распознаватель штрих-кодов
using (Vintasoft.Barcode.BarcodeReader reader = new Vintasoft.Barcode.BarcodeReader())
{
// указать, что распознаватель должен искать штрих-коды Han Xin Code
reader.Settings.ScanBarcodeTypes = Vintasoft.Barcode.BarcodeType.HanXinCode;
// указать, что распознаватель должен собирать информацию для теста качества печати штрих-кода
reader.Settings.CollectTestInformation = true;
// распознать штрих-коды в изображении
Vintasoft.Barcode.IBarcodeInfo[] barcodeInfos = reader.ReadBarcodes(barcodeImage);
// для каждого найденного штрих-кода
for (int i = 0; i < barcodeInfos.Length; i++)
{
Vintasoft.Barcode.IBarcodeInfo barcodeInfo = barcodeInfos[i];
// напечатать информацию о найденном штрих-коде
Console.WriteLine(string.Format("[{0}:{1}]", i + 1, barcodeInfo.BarcodeType));
Console.WriteLine(string.Format("Value: {0}", barcodeInfo.Value));
Console.WriteLine(string.Format("Region: {0}", barcodeInfo.Region));
// выполнить тестирование качества печати штрих-кода в соответствии с ISO 15415
Vintasoft.Barcode.QualityTests.ISO15415QualityTest test = new Vintasoft.Barcode.QualityTests.ISO15415QualityTest();
test.CalculateGrades((Vintasoft.Barcode.BarcodeInfo.BarcodeInfo2D)barcodeInfo, barcodeImage);
// напечатать результаты тестирования качества печати штрих-кода
Console.WriteLine("ISO15415 print quality test:");
Console.WriteLine(string.Format("- Decode : {0}", GradeToString(test.DecodeGrade)));
Console.WriteLine(string.Format("- Unused error correction : {0} ({1}%)", GradeToString(test.UnusedErrorCorrectionGrade), test.UnusedErrorCorrection));
Console.WriteLine(string.Format("- Symbol contrast : {0} ({1}%)", GradeToString(test.SymbolContrastGrade), test.SymbolContrast));
Console.WriteLine(string.Format("- Axial nonuniformity : {0} ({1})", GradeToString(test.AxialNonuniformityGrade), test.AxialNonuniformity));
Console.WriteLine(string.Format("- Grid nonuniformity : {0} ({1} cell)", GradeToString(test.GridNonuniformityGrade), test.GridNonuniformity));
Console.WriteLine(string.Format("- Modulation : {0}", GradeToString(test.ModulationGrade)));
Console.WriteLine(string.Format("- Reflectance margin : {0}", GradeToString(test.ReflectanceMarginGrade)));
Console.WriteLine(string.Format("- Fixed pattern damage : {0}", GradeToString(test.FixedPatternDamageGrade)));
Console.WriteLine(string.Format("- Distortion angle (informative) : {0}°", test.DistortionAngle));
Console.WriteLine();
}
}
}
}
/// <summary>
/// Конвертирует оценку качества ISO15415 в строковое значение.
/// </summary>
static string GradeToString(Vintasoft.Barcode.QualityTests.ISO15415QualityGrade grade)
{
int gradeAsInt = (int)grade;
return string.Format("{0}({1})", gradeAsInt, grade);
}
