Windows, Linux 및 macOS용 .NET 6 바코드 및 QR 코드 SDK를 빌드하는 방법
.NET 6 SDK 설치
Dynamsoft 바코드 리더
.NET 6 바코드 및 QR 코드 SDK 개발 및 구축 단계
새 라이브러리 프로젝트를 만듭니다.
dotnet new classlib -o BarcodeQRCodeSDK
- Windows: `DynamsoftBarcodeReader.dll`, `vcomp110.dll`
- Linux: `libDynamsoftBarcodeReader.so`
- macOS: `libDynamsoftBarcodeReader.dylib`    
Class1.cs를 BarcodeQRCodeReader.cs로 이름을 바꿉니다. P/Invoke은 C/C++ 및 .NET을 연결하는 데 사용되는 기술입니다.
BarcodeQRCodeReader.cs 파일에서 DllImport를 사용하여 관리되지 않는 공유 라이브러리(예: *.dll , *.so , *.dylib )를 로드하고 기본 구성 요소와 통신하기 위한 일부 관리 메서드를 정의합니다.[DllImport("DynamsoftBarcodeReader")]
static extern IntPtr DBR_CreateInstance();
[DllImport("DynamsoftBarcodeReader")]
static extern void DBR_DestroyInstance(IntPtr hBarcode);
[DllImport("DynamsoftBarcodeReader")]
static extern int DBR_InitLicense(string license, [Out] byte[] errorMsg, int errorMsgSize);
[DllImport("DynamsoftBarcodeReader")]
static extern int DBR_DecodeFile(IntPtr hBarcode, string filename, string template);
[DllImport("DynamsoftBarcodeReader")]
static extern int DBR_FreeTextResults(ref IntPtr pTextResultArray);
[DllImport("DynamsoftBarcodeReader")]
static extern void DBR_GetAllTextResults(IntPtr hBarcode, ref IntPtr pTextResultArray);
[DllImport("DynamsoftBarcodeReader")]
static extern int DBR_DecodeBuffer(IntPtr hBarcode, IntPtr pBufferBytes, int width, int height, int stride, ImagePixelFormat format, string template);
[DllImport("DynamsoftBarcodeReader")]
static extern int DBR_DecodeBase64String(IntPtr hBarcode, string base64string, string template);
또한 C#에서 몇 가지 기본 구조체를 정의해야 합니다.
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct PTextResult
{
    BarcodeFormat emBarcodeFormat;
    public string barcodeFormatString;
    BarcodeFormat_2 barcodeFormat_2;
    string barcodeFormatString_2;
    public string barcodeText;
    IntPtr barcodeBytes;
    int barcodeBytesLength;
    IntPtr localizationResult;
    IntPtr detailedResult;
    int resultsCount;
    IntPtr results;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 56)]
    char[] reserved;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct TextResultArray
{
    public int resultsCount;
    public IntPtr results;
}
관리되는 구조와 관리되지 않는 포인터 간의 메모리 작업은 약간 까다롭습니다. 데이터를 변환하려면 Marshal을 사용해야 합니다.
IntPtr pTextResultArray = IntPtr.Zero;
DBR_GetAllTextResults(hBarcode, ref pTextResultArray);
if (pTextResultArray != IntPtr.Zero)
{
    string[]? resultArray = null;
    TextResultArray? results = (TextResultArray?)Marshal.PtrToStructure(pTextResultArray, typeof(TextResultArray));
    if (results != null)
    {
        int count = results.Value.resultsCount;
        if (count > 0)
        {
            IntPtr[] barcodes = new IntPtr[count];
            Marshal.Copy(results.Value.results, barcodes, 0, count);
            resultArray = new string[count];
            for (int i = 0; i < count; i++)
            {
                PTextResult? result = (PTextResult?)Marshal.PtrToStructure(barcodes[i], typeof(PTextResult));
                if (result != null)
                {
                    resultArray[i] = result.Value.barcodeText;
                }
            }
        }
    }
    DBR_FreeTextResults(ref pTextResultArray);
    return resultArray;
}
관리되는 코드와 비관리되는 코드 사이의 통신 문제가 해결되면 일부 고급 C# 메서드를 정의할 수 있습니다.
public class BarcodeQRCodeReader
{
    private IntPtr hBarcode;
    private static string? licenseKey;
    public static void InitLicense(string license) {
        byte[] errorMsg = new byte[512];
        licenseKey = license;
        DBR_InitLicense(license, errorMsg, 512);
        Console.WriteLine(Encoding.ASCII.GetString(errorMsg) + "\n");
    }
    private BarcodeQRCodeReader()
    {
        hBarcode = DBR_CreateInstance();
    }
    public static BarcodeQRCodeReader Create()
    {
        if (licenseKey == null)
        {
            throw new Exception("Please call InitLicense first.");
        }
        return new BarcodeQRCodeReader();
    }
    ~BarcodeQRCodeReader()
    {
        if (hBarcode != IntPtr.Zero)
        {
            DBR_DestroyInstance(hBarcode);
            hBarcode = IntPtr.Zero;
        }
    }
    public void Destroy()
    {
        if (hBarcode != IntPtr.Zero)
        {
            DBR_DestroyInstance(hBarcode);
            hBarcode = IntPtr.Zero;
        }
    }
    public string[]? DecodeFile(string filename)
    {
        if (hBarcode == IntPtr.Zero) return null;
        int ret = DBR_DecodeFile(hBarcode, filename, "");
        return OutputResults();
    }
}
소스 코드를 빌드하여
*.dll 파일을 생성합니다.dotnet build --configuration Release
NuGet 패키지를 생성하고 게시하는 방법
*.nupkg 파일을 생성하는 가장 쉬운 방법은 <GeneratePackageOnBuild>true</GeneratePackageOnBuild> 파일에 *.csproj를 추가하는 것입니다.<PropertyGroup>
  ...
  <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
  ...
</PropertyGroup>
그러면 프로젝트를 빌드할 때
*.nupkg 파일이 자동으로 생성됩니다.패키지에는 일부 기본 라이브러리 파일이 포함되어 있습니다.
*.nupkg 파일에 올바르게 압축하기 위해 Runtime Identifier에 따라 PackagePath 파일에 해당하는 *.csproj를 설정합니다.<ItemGroup>
  <None CopyToOutputDirectory="Always" Include="DynamsoftBarcodeReader.dll" Pack="true" PackagePath="runtimes/win-x64/native/DynamsoftBarcodeReader.dll" />
  <None CopyToOutputDirectory="Always" Include="vcomp110.dll" Pack="true" PackagePath="runtimes/win-x64/native/vcomp110.dll" />
  <None CopyToOutputDirectory="Always" Include="libDynamsoftBarcodeReader.dylib" Pack="true" PackagePath="runtimes/osx-x64/native/libDynamsoftBarcodeReader.dylib" />
  <None CopyToOutputDirectory="Always" Include="libDynamsoftBarcodeReader.so" Pack="true" PackagePath="runtimes/linux-x64/native/libDynamsoftBarcodeReader.so" />
</ItemGroup>
*.nupkg 파일이 준비되면 dotnet command을 통해 NuGet 갤러리에 게시할 수 있습니다.dotnet nuget push *.nupkg -k <api-key> -s https://api.nuget.org/v3/index.json
또는 NuGet online page .

다음은 BarcodeQRCodeSDK의 마지막 페이지입니다.
https://www.nuget.org/packages/BarcodeQRCodeSDK/
.NET 라이브러리 프로젝트를 참조로 로컬에서 추가하는 방법
소스 코드의 경우
<ProjectReference> 파일에 *.csproj를 추가합니다.<ItemGroup>
    <ProjectReference Include="..\..\BarcodeQRCodeSDK.csproj" />
</ItemGroup>
생성된
*.nupkg 파일의 경우 패키지 디렉터리를 NuGet 소스 목록에 추가한 다음 dotnet add package를 통해 패키지를 설치합니다.dotnet nuget add source <package directory>
dotnet add package <package name>
간단한 .NET 6 명령줄 예제
새 .NET 콘솔 앱을 만듭니다.
dotnet new console -o Test
.NET 바코드 및 QR 코드 SDK를 설치합니다.
dotnet add package BarcodeQRCodeSDK
다음 코드를 사용하여 이미지 파일에서 바코드 및 QR 코드를 디코딩합니다.
using System;
using System.Runtime.InteropServices;
using Dynamsoft;
namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            BarcodeQRCodeReader.InitLicense("LICENSE-KEY");
            BarcodeQRCodeReader? reader = null;
            try {
                reader = BarcodeQRCodeReader.Create();
                Console.WriteLine("Please enter an image file: ");
                string? filename = Console.ReadLine();
                if (filename != null) {
                    string[]? results = reader.DecodeFile(filename);
                    if (results != null) {
                        foreach (string result in results) {
                            Console.WriteLine(result);
                        }
                    }
                    else {
                        Console.WriteLine("No barcode found.");
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Destroy();
                }
            }
        }
    }
}
Windows, Linux 또는 macOS에서 애플리케이션을 실행합니다.
dotnet run

소스 코드
https://github.com/yushulx/dotnet-barcode-qr-code-sdk
Reference
이 문제에 관하여(Windows, Linux 및 macOS용 .NET 6 바코드 및 QR 코드 SDK를 빌드하는 방법), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/yushulx/how-to-build-net-6-barcode-and-qr-code-sdk-for-windows-linux-macos-17gd텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)