Microsoft .NET 6 SDK empowers C# builders to construct cross-platform DotNet functions for Home windows, Linux, and macOS from one codebase. This text describes the steps to construct a .NET 6 barcode and QR code decoding library based mostly on Dynamsoft C/C++ Barcode SDK, in addition to how you can pack the library right into a NuGet package deal.
.NET 6 SDK Set up
Dynamsoft Barcode Reader
- Obtain C/C++ SDK v9.0.
- Get a sound license key for activating the SDK.
Steps to Develop and Construct .NET 6 Barcode and QR Code SDK
-
Create a brand new library undertaking:
dotnet new classlib -o BarcodeQRCodeSDK
-
Copy shared library information from the C/C++ SDK package deal to the undertaking root listing. For various platforms, the minimal required shared library information are:
- Home windows: `DynamsoftBarcodeReader.dll`, `vcomp110.dll`
- Linux: `libDynamsoftBarcodeReader.so`
- macOS: `libDynamsoftBarcodeReader.dylib`
- Rename
Class1.cs
toBarcodeQRCodeReader.cs
. -
P/Invoke is the know-how used for bridging C/C++ and .NET. Within the
BarcodeQRCodeReader.cs
file, we useDllImport
to load the unmanaged shared library (e.g.*.dll
,*.so
,*.dylib
) and outline some managed strategies to speak with the native part.[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 peak, int stride, ImagePixelFormat format, string template); [DllImport("DynamsoftBarcodeReader")] static extern int DBR_DecodeBase64String(IntPtr hBarcode, string base64string, string template);
-
As well as, we have to outline some native structs in C#:
[StructLayout(LayoutKind.Sequential, Pack = 1)] inside 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 outcomes; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 56)] char[] reserved; } [StructLayout(LayoutKind.Sequential, Pack = 1)] inside struct TextResultArray { public int resultsCount; public IntPtr outcomes; }
-
The reminiscence operation between the managed construction and the unmanaged pointer is slightly bit difficult. We have to use Marshal to transform knowledge:
IntPtr pTextResultArray = IntPtr.Zero; DBR_GetAllTextResults(hBarcode, ref pTextResultArray); if (pTextResultArray != IntPtr.Zero) { string[]? resultArray = null; TextResultArray? outcomes = (TextResultArray?)Marshal.PtrToStructure(pTextResultArray, typeof(TextResultArray)); if (outcomes != null) { int rely = outcomes.Worth.resultsCount; if (rely > 0) { IntPtr[] barcodes = new IntPtr[count]; Marshal.Copy(outcomes.Worth.outcomes, barcodes, 0, rely); resultArray = new string[count]; for (int i = 0; i < rely; i++) { PTextResult? outcome = (PTextResult?)Marshal.PtrToStructure(barcodes[i], typeof(PTextResult)); if (outcome != null) { resultArray[i] = outcome.Worth.barcodeText; } } } } DBR_FreeTextResults(ref pTextResultArray); return resultArray; }
-
As soon as the communication downside between managed and unmanaged code is solved, we are able to outline some high-level C# strategies:
public class BarcodeQRCodeReader { non-public IntPtr hBarcode; non-public 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"); } non-public BarcodeQRCodeReader() { hBarcode = DBR_CreateInstance(); } public static BarcodeQRCodeReader Create() { if (licenseKey == null) { throw new Exception("Please name 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(); } }
-
Construct the supply code to generate the
*.dll
file.dotnet construct --configuration Launch
Learn how to Generate and Publish NuGet Bundle
To generate the *.nupkg
file, the simplest method is so as to add <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
to the *.csproj
file:
<PropertyGroup>
...
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
...
</PropertyGroup>
Then the *.nupkg
file will likely be generated routinely if you construct the undertaking.
Our package deal incorporates some native library information. To pack them into the *.nupkg
file appropriately, we set the corresponding PackagePath
in *.csproj
file in keeping with the Runtime Identifier:
<ItemGroup>
<None CopyToOutputDirectory="At all times" Embrace="DynamsoftBarcodeReader.dll" Pack="true" PackagePath="runtimes/win-x64/native/DynamsoftBarcodeReader.dll" />
<None CopyToOutputDirectory="At all times" Embrace="vcomp110.dll" Pack="true" PackagePath="runtimes/win-x64/native/vcomp110.dll" />
<None CopyToOutputDirectory="At all times" Embrace="libDynamsoftBarcodeReader.dylib" Pack="true" PackagePath="runtimes/osx-x64/native/libDynamsoftBarcodeReader.dylib" />
<None CopyToOutputDirectory="At all times" Embrace="libDynamsoftBarcodeReader.so" Pack="true" PackagePath="runtimes/linux-x64/native/libDynamsoftBarcodeReader.so" />
</ItemGroup>
Because the *.nupkg
file is prepared, we are able to publish it to the NuGet Gallery both through the dotnet command:
dotnet nuget push *.nupkg -k <api-key> -s https://api.nuget.org/v3/index.json
or the NuGet online page.
Right here is the ultimate web page of BarcodeQRCodeSDK:
https://www.nuget.org/packages/BarcodeQRCodeSDK/
Learn how to Add a .NET Library Challenge as a Reference Domestically
For supply code, add <ProjectReference>
in *.csproj
file:
<ItemGroup>
<ProjectReference Embrace="....BarcodeQRCodeSDK.csproj" />
</ItemGroup>
For generated *.nupkg
file, add the package deal listing to NuGet supply listing after which set up the package deal through dotnet add package deal
:
dotnet nuget add supply <package deal listing>
dotnet add package deal <package deal title>
A Easy .NET 6 Command-line Instance
-
Create a brand new .NET console app:
dotnet new console -o Check
-
Set up the .NET Barcode and QR Code SDK:
dotnet add package deal BarcodeQRCodeSDK
-
Use the next code to decode barcode and QR code from a picture file:
utilizing System; utilizing System.Runtime.InteropServices; utilizing Dynamsoft; namespace Check { class Program { static void Principal(string[] args) { BarcodeQRCodeReader.InitLicense("LICENSE-KEY"); BarcodeQRCodeReader? reader = null; strive { reader = BarcodeQRCodeReader.Create(); Console.WriteLine("Please enter a picture file: "); string? filename = Console.ReadLine(); if (filename != null) { string[]? outcomes = reader.DecodeFile(filename); if (outcomes != null) { foreach (string outcome in outcomes) { Console.WriteLine(outcome); } } else { Console.WriteLine("No barcode discovered."); } } } catch (Exception e) { Console.WriteLine(e.Message); } lastly { if (reader != null) { reader.Destroy(); } } } } }
-
Run the applying in Home windows, Linux or macOS:
dotnet run