Генерация и распознавание штрих-кодов DotCode в .NET
Категория: Штрих-коды; .NET
Август 13, 2020


/// <summary>
/// Сгенерировать штрих-код DotCode в виде растрового изображения.
/// </summary>
public void GenerateDotCodeBarcodeAsRasterImage()
{
// создать генератор штрих-кодов
Vintasoft.Barcode.BarcodeWriter barcodeWriter = new Vintasoft.Barcode.BarcodeWriter();
// установить настройки генератора штрих-кодов
barcodeWriter.Settings.Barcode = Vintasoft.Barcode.BarcodeType.DotCode;
barcodeWriter.Settings.Value = "abc012345def";
// получить штрих-код как растровое изображение
using (System.Drawing.Image image = barcodeWriter.GetBarcodeAsBitmap())
{
// сохранить полученное изображение в файл
image.Save("DotCodeBarcode.png");
}
}
/// <summary>
/// Сгенерировать штрих-код DotCode в векторной форме.
/// </summary>
public System.Drawing.Drawing2D.GraphicsPath GenerateDotCodeBarcodeAsGraphicsPath()
{
// создать генератор штрих-кодов
Vintasoft.Barcode.BarcodeWriter barcodeWriter = new Vintasoft.Barcode.BarcodeWriter();
// установить настройки генератора штрих-кодов
barcodeWriter.Settings.Barcode = Vintasoft.Barcode.BarcodeType.DotCode;
barcodeWriter.Settings.Value = "abc012345def";
// вернуть штрих-код как объект GraphicsPath
return barcodeWriter.GetBarcodeAsGraphicsPath();
}
/// <summary>
/// Сгенерировать штрих-код DotCode в виде SVG изображения.
/// </summary>
public string GenerateDotCodeBarcodeAsSvgImage()
{
// создать генератор штрих-кодов
Vintasoft.Barcode.BarcodeWriter barcodeWriter = new Vintasoft.Barcode.BarcodeWriter();
// установить настройки генератора штрих-кодов
barcodeWriter.Settings.Barcode = Vintasoft.Barcode.BarcodeType.DotCode;
barcodeWriter.Settings.Value = "012345abcde";
// вернуть штрих-код как строку с данными SVG изображения
return barcodeWriter.GetBarcodeAsSvgFile();
}
/// <summary>
/// Распознать штрих-код DotCode в изображении.
/// </summary>
public void RecognizeDotCodeBarcode()
{
// создать распознаватель штрих-кодов
using (Vintasoft.Barcode.BarcodeReader reader = new Vintasoft.Barcode.BarcodeReader())
{
// указать, что распознаватель должен искать штрих-коды DotCode
reader.Settings.ScanBarcodeTypes = Vintasoft.Barcode.BarcodeType.DotCode;
// ScanInterval должен быть меньше размера точки штрих-кода DotCode, в пикселях
reader.Settings.ScanInterval = 3;
// распознать штрих-коды в файле изображения
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();
}
}
}
}