macOS で .NET アプリケーションを使用してバーコードを生成します

ブログ カテゴリ: バーコード.NETmacOS

2023/12/25

この記事では、macOS でコンソール .NET アプリケーションを作成し、バーコードを生成する方法について説明します。バーコードの生成には、VintaSoft Barcode .NET SDK を使用します。

タスクを完了する手順は次のとおりです。
  1. macOS デスクトップを開きます。

  2. .NETアプリケーションのファイルを保存するフォルダを作成します。現在のユーザーのデスクトップに「Generate_Barcode」フォルダを作成し、そのフォルダに移動します。


  3. コンソール ターミナルを開きます。


  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.macOS" 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. macOS で評価版を使用するためのコードは、documentation に記載されている方法で取得し、取得したコードを "Program.cs" ファイルの C# コードに挿入してください。


  8. ターミナルを開き、次のコマンドを使用して .NET プロジェクトをコンパイルします。
    dotnet build Generate_Barcode.csproj
    



    ターミナルを閉じます。

  9. "bin/Debug/net8.0/" フォルダーに移動します。


  10. ターミナルを開き、次のコマンドを使用して .NET アプリケーションを実行します。
    dotnet ./Generate_Barcode.dll
    



    ターミナルを閉じて、生成された PNG ファイルと SVG ファイルを確認します。