Riconoscimento dei codici a barre nelle immagini in Linux utilizzando un'applicazione .NET
Categoria del blog: Barcode ; .NET ; Linux
15.12.2023
dotnet new console --framework net8.0
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>ConsoleApp1</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="SkiaSharp" Version="2.88.6" />
<PackageReference Include="SkiaSharp.NativeAssets.Linux" Version="2.88.6" />
<PackageReference Include="Vintasoft.Barcode" Version="14.2.0.3" />
<PackageReference Include="Vintasoft.Barcode.SkiaSharp" Version="14.2.0.3" />
<PackageReference Include="Vintasoft.Shared" Version="4.1.0.3" />
</ItemGroup>
<ItemGroup>
<Content Include="AllSupportedBarcodes.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>
namespace Recognize_Barcodes_In_Image
{
class Program
{
static void Main(string[] args)
{
// register the evaluation license for VintaSoft Barcode .NET SDK
Vintasoft.Barcode.BarcodeGlobalSettings.Register("%EVAL_LIC_USER_NAME%", "%EVAL_LIC_USER_EMAIL%", "%EVAL_LIC_DATE%", "%EVAL_LIC_REG_CODE%");
// Vintasoft.Barcode.SkiaSharp.dll allows to load bitmap from BMP, DNG, GIF, HEIF, JPEG, PNG, WEBP files using image codecs from SkiaSharp library
Vintasoft.Barcode.SkiaSharpAssembly.Init();
// recognize barcodes from image file
RecognizeBarcodesFromImage("AllSupportedBarcodes.png");
}
/// <summary>
/// Recognizes barcodes from an image file.
/// </summary>
/// <param name="filename">A path to an image file.</param>
public static void RecognizeBarcodesFromImage(string filename)
{
// create barcode reader
using (Vintasoft.Barcode.BarcodeReader reader = new Vintasoft.Barcode.BarcodeReader())
{
// specify that reader must search for Code39, Code39Extended, Code128, SSCC18 and DataMatrix barcodes
reader.Settings.ScanBarcodeTypes =
Vintasoft.Barcode.BarcodeType.Code39 |
Vintasoft.Barcode.BarcodeType.Code128 |
Vintasoft.Barcode.BarcodeType.DataMatrix;
reader.Settings.ScanBarcodeSubsets.Add(Vintasoft.Barcode.SymbologySubsets.BarcodeSymbologySubsets.Code39Extended);
reader.Settings.ScanBarcodeSubsets.Add(Vintasoft.Barcode.SymbologySubsets.BarcodeSymbologySubsets.SSCC18);
// specify that reader must search for horizontal and vertical barcodes only
reader.Settings.ScanDirection = Vintasoft.Barcode.ScanDirection.Horizontal | Vintasoft.Barcode.ScanDirection.Vertical;
// use Automatic Recognition
reader.Settings.AutomaticRecognition = true;
// read barcodes from image file
Vintasoft.Barcode.IBarcodeInfo[] infos = reader.ReadBarcodes(filename);
// if barcodes are not detected
if (infos.Length == 0)
{
Console.WriteLine("No barcodes found.");
}
// if barcodes are detected
else
{
// get information about extracted barcodes
Console.WriteLine(string.Format("{0} barcodes found:", infos.Length));
Console.WriteLine();
for (int i = 0; i < infos.Length; i++)
{
Vintasoft.Barcode.IBarcodeInfo info = infos[i];
Console.WriteLine(string.Format("[{0}:{1}]", i + 1, info.BarcodeType));
Console.WriteLine(string.Format("Value: {0}", info.Value));
Console.WriteLine(string.Format("Region: {0}", info.Region));
Console.WriteLine();
}
}
}
}
}
}
dotnet build Recognize_Barcodes_In_Image.csproj
dotnet ./Recognize_Barcodes_In_Image.dll