A Razor Class Library, equipped with the Dynamsoft JavaScript Barcode SDK, enables the creation of web-based barcode reader and scanner applications entirely in C#.
https://yushulx.me/Razor-Barcode-Library/
dotnet-blazor-barcode-scanner.mp4
-
Import and initialize the Razor Barcode Library.
@using RazorBarcodeLibrary protected override async Task OnInitializedAsync() { barcodeJsInterop = new BarcodeJsInterop(JSRuntime); await barcodeJsInterop.LoadJS(); }
-
Set the license key and load the wasm module.
await barcodeJsInterop.SetLicense('LICENSE-KEY'); await barcodeJsInterop.LoadWasm();
-
Create a barcode reader instance.
BarcodeReader reader = await barcodeJsInterop.CreateBarcodeReader();
-
Read barcodes from a base64 image source.
List<BarcodeResult> results = await reader.DecodeBase64(imageSrc); string text = ""; foreach (BarcodeResult result in results) { text += "format: " + result.Format + ", "; text += "text: " + result.Text + "<br>"; }
-
Create a barcode scanner instance and set the callback function.
@implements BarcodeScanner.ICallback BarcodeReader reader = await barcodeJsInterop.CreateBarcodeScanner(); await scanner.RegisterCallback(this); public async Task OnCallback(List<BarcodeResult> results) {}
-
Open the camera and start scanning.
<div id="videoContainer"></div> await scanner.SetVideoElement("videoContainer"); List<Camera> cameras = await scanner.GetCameras(); await scanner.OpenCamera(cameras[0]);
Represents a camera device with its device ID and label.
Represents the result of a barcode scan, including the decoded text, format, and positional details.
Provides JavaScript interop functionalities for barcode operations.
Task LoadJS()
: Loads and initializes the JavaScript module.Task SetLicense(string license)
: Sets the license key for the barcode functionality.Task LoadWasm()
: Loads the WebAssembly for barcode processing.Task<BarcodeReader> CreateBarcodeReader()
: Creates a new BarcodeReader instance.Task<BarcodeScanner> CreateBarcodeScanner()
: Creates a new BarcodeScanner instance.Task DrawCanvas(string id, int sourceWidth, int sourceHeight, List<BarcodeResult> results)
: Draws the barcode results on a specified canvas.Task ClearCanvas(string id)
: Clears the specified canvas element.
Provides functionalities to decode barcodes from various sources such as Base64 strings and canvas objects.
Task<List<BarcodeResult>> DecodeBase64(string base64)
: Asynchronously decodes a barcode from a Base64 encoded string.Task<List<BarcodeResult>> DecodeCanvas(IJSObjectReference canvas)
: Asynchronously decodes a barcode from a canvas object.Task<string> GetParameters()
: Asynchronously retrieves the current parameters of the barcode reader.Task<int> SetParameters(string parameters)
: Asynchronously sets the parameters for the barcode reader.
Provides functionalities for barcode scanning using a camera.
Task SetVideoElement(string videoId)
: Sets a div element as the video container.Task OpenCamera(Camera camera)
: Opens the camera for barcode scanning.Task CloseCamera()
: Closes the current camera.Task<List<Camera>> GetCameras()
: Gets a list of available cameras.Task RegisterCallback(ICallback callback)
: Registers a callback for handling barcode scan results.