.NET 애플리케이션을 사용하여 Linux 이미지에서 바코드 인식

블로그 카테고리: 바코드.NET리눅스

2023/12/15

이 문서에서는 콘솔 .NET 애플리케이션을 만들고 Ubuntu에서 이미지의 바코드를 인식하는 방법을 설명합니다. 이미지에서 바코드를 인식하기 위해 VintaSoft Barcode .NET SDK가 사용됩니다.

다음은 작업을 완료하는 단계입니다.
  1. 우분투 데스크톱을 엽니다.

  2. .NET 애플리케이션 파일을 저장할 폴더를 생성합니다. 현재 사용자의 바탕 화면에 "Recognize_Barcodes_In_Image" 폴더를 생성하고 해당 폴더로 이동합니다.


  3. 콘솔 명령 터미널을 엽니다. 컨텍스트 메뉴에서 "터미널에서 열기"를 선택하거나 Ctrl+Alt+T 키 조합을 눌러 열 수 있습니다.


  4. 터미널에서 새 콘솔 .NET 애플리케이션 프로젝트를 생성하는 명령을 실행합니다.
    dotnet new console --framework net8.0
    



    생성된 프로젝트에는 "Recognize_Barcodes_In_Image.csproj" 프로젝트 파일과 애플리케이션의 C# 코드가 포함된 "Program.cs" 파일이 있습니다. 터미널을 닫으세요.

  5. 텍스트 편집기에서 "Recognize_Barcodes_In_Image.csproj" 프로젝트 파일을 열고 파일 내용을 다음과 같이 변경하세요.
    <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>
    



    변경된 프로젝트는 VintaSoft Barcode .NET SDK(Vintasoft.Shared.dll, Vintasoft.Barcode.dll, Vintasoft.Barcode.SkiaSharp.dll)의 NuGet 패키지를 참조합니다.

  6. "Program.cs" 파일을 열고 코드를 다음과 같은 C# 코드로 변경하십시오.
    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();
                        }
                    }
                }
            }
    
        }
    }
    



    애플리케이션 코드는 이미지에서 바코드를 인식하고 결과를 콘솔에 출력합니다.

  7. 문서에 설명된 방법을 사용하여 Linux에서 평가판을 사용하는 코드를 받으세요. 얻은 코드를 "Program.cs" 파일의 C# 코드에 삽입합니다.


  8. "AllSupportedBarcodes.png" 파일을 프로젝트 폴더에 복사하세요.


    "AllSupportedBarcodes.png" 파일 대신 바코드가 포함된 다른 파일을 사용할 수도 있습니다.

  9. 터미널을 열고 다음 명령어를 사용하여 .NET 프로젝트를 컴파일하십시오.
    dotnet build Recognize_Barcodes_In_Image.csproj
    



    터미널을 닫으십시오.

  10. "bin/Debug/net8.0/" 폴더로 이동하세요.


  11. 터미널을 열고 다음 명령어를 사용하여 .NET 애플리케이션을 실행합니다.
    dotnet ./Recognize_Barcodes_In_Image.dll
    



    바코드 인식 결과를 확인하고 터미널을 닫으세요.