Linux에서 .NET 애플리케이션을 사용하여 바코드를 생성합니다..NET 애플리케이션

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

2023/12/22

이 문서에서는 Ubuntu에서 콘솔 .NET 애플리케이션을 만들고 바코드를 생성하는 방법을 설명합니다. 바코드 생성에는 VintaSoft Barcode .NET SDK가 사용됩니다.

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

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


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


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



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

  5. 텍스트 편집기에서 "Generate_Barcode.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>
    
    </Project>
    



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

  6. "Program.cs" 파일을 열고 코드를 다음과 같은 C# 코드로 변경하십시오.
    namespace Generate_Barcode
    {
        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();
    
                // generate Code128 barcode as a raster image and save to a PNG file
                GenerateBarcodeAsRasterImage(Vintasoft.Barcode.BarcodeType.Code128, "VintaSoft Barcode .NET SDK", "rasterBarcodeImage.png");
    
                // generate QR barcode as vector image and save to a SVG file
                GenerateBarcodeAsVectorImage(Vintasoft.Barcode.BarcodeType.QR, "VintaSoft Barcode .NET SDK", "vectorBarcode.svg");
            }
    
            /// <summary>
            /// Generates barcode as raster image and saves to an image file.
            /// </summary>
            /// <param name="barcodeType">Barcode type.</param>
            /// <param name="barcodeValue">Barcode value.</param>
            /// <param name="pngFilename">The filename, where barcode image must be saved.</param>
            public static void GenerateBarcodeAsRasterImage(Vintasoft.Barcode.BarcodeType barcodeType, string barcodeValue, string pngFilename)
            {
                // create the barcode writer
                using (Vintasoft.Barcode.BarcodeWriter barcodeWriter = new Vintasoft.Barcode.BarcodeWriter())
                {
                    // set barcode writer settings
                    barcodeWriter.Settings.Barcode = barcodeType;
                    barcodeWriter.Settings.Value = barcodeValue;
    
                    // generate barcode as raster image and save to a PNG file
                    barcodeWriter.SaveBarcodeAsImage(pngFilename);
                }
            }
    
            /// <summary>
            /// Generates barcode in vector form and saves to a SVG file.
            /// </summary>
            /// <param name="barcodeType">Barcode type.</param>
            /// <param name="barcodeValue">Barcode value.</param>
            /// <param name="svgFilename">The filename, where SVG file must be saved.</param>
            public static void GenerateBarcodeAsVectorImage(Vintasoft.Barcode.BarcodeType barcodeType, string barcodeValue, string svgFilename)
            {
                // create the barcode writer
                using (Vintasoft.Barcode.BarcodeWriter barcodeWriter = new Vintasoft.Barcode.BarcodeWriter())
                {
                    // set barcode writer settings
                    barcodeWriter.Settings.Barcode = barcodeType;
                    barcodeWriter.Settings.Value = barcodeValue;
    
                    // generate SVG file
                    string svgFile = barcodeWriter.GetBarcodeAsSvgFile();
    
                    // save SVG file
                    System.IO.File.WriteAllText(svgFilename, svgFile);
                }
            }
    
        }
    }
    



    애플리케이션 코드는 Code128 바코드를 래스터 이미지로, QR 바코드를 벡터 형식으로 생성하며, 생성된 바코드는 파일로 저장됩니다.

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


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



    터미널을 닫으십시오.

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


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



    터미널을 닫고 생성된 PNG 및 SVG 파일을 확인하세요.