Skip to content

Commit

Permalink
Updated document comments
Browse files Browse the repository at this point in the history
  • Loading branch information
yushulx committed Nov 17, 2023
1 parent 5364c8c commit 51cb127
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 7 deletions.
66 changes: 63 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Razor Barcode Library
A Razor Class Library, equipped with the [Dynamsoft JavaScript Barcode SDK](https://www.npmjs.com/package/dynamsoft-javascript-barcode), enables the creation of web-based barcode reader applications entirely in C#.
A Razor Class Library, equipped with the [Dynamsoft JavaScript Barcode SDK](https://www.npmjs.com/package/dynamsoft-javascript-barcode), enables the creation of web-based barcode reader and scanner applications entirely in C#.

## Online Demo
[https://yushulx.me/Razor-Barcode-Library/](https://yushulx.me/Razor-Barcode-Library/)

## Demo Video
https://github.com/yushulx/Razor-Barcode-Library/assets/2202306/ac3c333f-7895-420a-94ee-6debe805a8b7


Expand All @@ -12,7 +13,7 @@ https://github.com/yushulx/Razor-Barcode-Library/assets/2202306/ac3c333f-7895-42
- [Dynamsoft JavaScript Barcode License](https://www.dynamsoft.com/customer/license/trialLicense?product=dbr&utm_source=github&utm_campaign=razor-barcode-library)


## Usage
## Quick Usage
- Import and initialize the Razor Barcode Library.

```csharp
Expand Down Expand Up @@ -49,7 +50,66 @@ https://github.com/yushulx/Razor-Barcode-Library/assets/2202306/ac3c333f-7895-42
}
```

- Create a barcode scanner instance and set the callback function.

```csharp
@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.

```csharp
<div id="videoContainer"></div>

await scanner.SetVideoElement("videoContainer");
List<Camera> cameras = await scanner.GetCameras();
await scanner.OpenCamera(cameras[0]);
```

## API

### Camera Class
Represents a camera device with its device ID and label.

### BarcodeResult Class
Represents the result of a barcode scan, including the decoded text, format, and positional details.

### BarcodeJsInterop Class

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.

### BarcodeReader Class

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.

### BarcodeScanner Class

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.

## Example
- [Blazor WebAssembly](https://github.com/yushulx/Razor-Barcode-Library/tree/main/example)

![razor barcode reader](https://user-images.githubusercontent.com/2202306/282373638-b2cf96db-8466-4c3a-a802-4fd39cec42cd.png)
![razor barcode reader](https://user-images.githubusercontent.com/2202306/282373638-b2cf96db-8466-4c3a-a802-4fd39cec42cd.png)
49 changes: 49 additions & 0 deletions RazorBarcodeLibrary/BarcodeJsInterop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,66 @@

namespace RazorBarcodeLibrary
{
/// <summary>
/// Provides JavaScript interop functionalities for barcode operations.
/// </summary>
public class BarcodeJsInterop : IAsyncDisposable
{
// Holds a task that resolves to a JavaScript module reference.
private readonly Lazy<Task<IJSObjectReference>> moduleTask;

/// <summary>
/// Initializes a new instance of the BarcodeJsInterop class.
/// </summary>
/// <param name="jsRuntime">The JS runtime to use for invoking JavaScript functions.</param>
public BarcodeJsInterop(IJSRuntime jsRuntime)
{
moduleTask = new(() => jsRuntime.InvokeAsync<IJSObjectReference>(
"import", "./_content/RazorBarcodeLibrary/barcodeJsInterop.js").AsTask());
}

/// <summary>
/// Loads and initializes the JavaScript module.
/// </summary>
public async Task LoadJS()
{
var module = await moduleTask.Value;
await module.InvokeAsync<object>("init");
}

/// <summary>
/// Sets the license key for the barcode functionality.
/// </summary>
/// <param name="license">The license key.</param>
public async Task SetLicense(string license)
{
var module = await moduleTask.Value;
await module.InvokeVoidAsync("setLicense", license);
}

/// <summary>
/// Loads the WebAssembly for barcode processing.
/// </summary>
public async Task LoadWasm()
{
var module = await moduleTask.Value;
await module.InvokeVoidAsync("loadWasm");
}

/// <summary>
/// Gets the version of the barcode library.
/// </summary>
/// <returns>A task that represents the asynchronous operation. The task result contains the version string.</returns>
public async ValueTask<string> GetVersion()
{
var module = await moduleTask.Value;
return await module.InvokeAsync<string>("getVersion");
}

/// <summary>
/// Creates a new BarcodeReader instance.
/// </summary>
/// <returns>A task that represents the asynchronous operation. The task result is a new BarcodeReader instance.</returns>
public async Task<BarcodeReader> CreateBarcodeReader()
{
var module = await moduleTask.Value;
Expand All @@ -44,6 +70,10 @@ public async Task<BarcodeReader> CreateBarcodeReader()
return reader;
}

/// <summary>
/// Creates a new BarcodeScanner instance.
/// </summary>
/// <returns>A task that represents the asynchronous operation. The task result is a new BarcodeScanner instance.</returns>
public async Task<BarcodeScanner> CreateBarcodeScanner()
{
var module = await moduleTask.Value;
Expand All @@ -52,25 +82,44 @@ public async Task<BarcodeScanner> CreateBarcodeScanner()
return scanner;
}

/// <summary>
/// Converts a Base64 string to a canvas element.
/// </summary>
/// <param name="base64">The Base64 encoded string.</param>
/// <returns>A task that represents the asynchronous operation. The task result is a reference to the canvas element.</returns>
public async Task<IJSObjectReference> Base64ToCanvas(string base64)
{
var module = await moduleTask.Value;
var canvas = await module.InvokeAsync<IJSObjectReference>("decodeBase64Image", base64);
return canvas;
}

/// <summary>
/// Draws the barcode results on a specified canvas.
/// </summary>
/// <param name="id">The ID of the canvas element.</param>
/// <param name="sourceWidth">The width of the source image.</param>
/// <param name="sourceHeight">The height of the source image.</param>
/// <param name="results">The list of barcode results to draw.</param>
public async Task DrawCanvas(string id, int sourceWidth, int sourceHeight, List<BarcodeResult> results)
{
var module = await moduleTask.Value;
await module.InvokeVoidAsync("drawCanvas", id, sourceWidth, sourceHeight, results);
}

/// <summary>
/// Clears the specified canvas element.
/// </summary>
/// <param name="id">The ID of the canvas element to clear.</param>
public async Task ClearCanvas(string id)
{
var module = await moduleTask.Value;
await module.InvokeVoidAsync("clearCanvas", id);
}

/// <summary>
/// Releases unmanaged resources asynchronously.
/// </summary>
public async ValueTask DisposeAsync()
{
if (moduleTask.IsValueCreated)
Expand Down
29 changes: 29 additions & 0 deletions RazorBarcodeLibrary/BarcodeReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,34 @@

namespace RazorBarcodeLibrary
{
/// <summary>
/// Provides functionalities to decode barcodes from various sources and manage barcode reader settings.
/// </summary>
public class BarcodeReader
{
// Fields to hold JavaScript object references.
private IJSObjectReference _module;
private IJSObjectReference _jsObjectReference;

// Public properties to store source dimensions.
public int SourceWidth, SourceHeight;

/// <summary>
/// Initializes a new instance of the BarcodeReader class.
/// </summary>
/// <param name="module">A reference to the JavaScript module.</param>
/// <param name="reader">A reference to the JavaScript object for barcode reading.</param>
public BarcodeReader(IJSObjectReference module, IJSObjectReference reader)
{
_module = module;
_jsObjectReference = reader;
}

/// <summary>
/// Asynchronously decodes a barcode from a Base64 encoded string.
/// </summary>
/// <param name="base64">The Base64 encoded string containing the barcode image.</param>
/// <returns>A task that represents the asynchronous decode operation. The task result contains a list of BarcodeResult objects.</returns>
public async Task<List<BarcodeResult>> DecodeBase64(string base64)
{
JsonElement? result = await _jsObjectReference.InvokeAsync<JsonElement>("decode", base64);
Expand All @@ -24,6 +39,11 @@ public async Task<List<BarcodeResult>> DecodeBase64(string base64)
return BarcodeResult.WrapResult(result);
}

/// <summary>
/// Asynchronously decodes a barcode from a canvas object.
/// </summary>
/// <param name="canvas">A reference to the JavaScript object representing the canvas with the barcode image.</param>
/// <returns>A task that represents the asynchronous decode operation. The task result contains a list of BarcodeResult objects.</returns>
public async Task<List<BarcodeResult>> DecodeCanvas(IJSObjectReference canvas)
{
JsonElement? result = await _jsObjectReference.InvokeAsync<JsonElement>("decode", canvas);
Expand All @@ -32,13 +52,22 @@ public async Task<List<BarcodeResult>> DecodeCanvas(IJSObjectReference canvas)
return BarcodeResult.WrapResult(result);
}

/// <summary>
/// Asynchronously retrieves the current parameters of the barcode reader.
/// </summary>
/// <returns>A task that represents the asynchronous operation. The task result is a string representation of the current barcode reader settings.</returns>
public async Task<string> GetParameters()
{
if (_jsObjectReference == null) { return ""; }

return await _jsObjectReference.InvokeAsync<string>("outputRuntimeSettingsToString");
}

/// <summary>
/// Asynchronously sets the parameters for the barcode reader.
/// </summary>
/// <param name="parameters">A string representation of the settings to apply to the barcode reader.</param>
/// <returns>A task that represents the asynchronous operation. The task result is an integer indicating the status (0 for success, -1 for failure).</returns>
public async Task<int> SetParameters(string parameters)
{
if (_jsObjectReference == null) { return -1; }
Expand Down
28 changes: 27 additions & 1 deletion RazorBarcodeLibrary/BarcodeResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,27 @@

namespace RazorBarcodeLibrary
{
/// <summary>
/// Represents the result of a barcode scan, including the decoded text, format, and positional details.
/// </summary>
public class BarcodeResult
{
/// <summary>
/// Gets or sets the decoded text of the barcode.
/// </summary>
public string Text { get; set; } = string.Empty;

/// <summary>
/// Gets or sets the format of the decoded barcode.
/// </summary>
public string Format { get; set; } = string.Empty;

/// <summary>
/// Gets or sets the full information string of the barcode result.
/// </summary>
public string FullInfo { get; set; } = string.Empty;

// Properties for the coordinates of the barcode corners
public int X1 { get; set; }
public int Y1 { get; set; }
public int X2 { get; set; }
Expand All @@ -18,12 +32,19 @@ public class BarcodeResult
public int X4 { get; set; }
public int Y4 { get; set; }


/// <summary>
/// Returns a string that represents the current barcode result.
/// </summary>
/// <returns>A string representation of the barcode result.</returns>
public override string ToString()
{
return $"Text: {Text}, Format: {Format}, X1: {X1}, Y1: {Y1}, X2: {X2}, Y2: {Y2}, X3: {X3}, Y3: {Y3}, X4: {X4}, Y4: {Y4}";
}

/// <summary>
/// Converts the barcode result into a JSON formatted dictionary.
/// </summary>
/// <returns>A dictionary containing the barcode result data in JSON format.</returns>
public Dictionary<string, object> ToJson()
{
var jsonDict = new Dictionary<string, object>();
Expand All @@ -40,6 +61,11 @@ public override string ToString()
return jsonDict;
}

/// <summary>
/// Static method to wrap JSON results into a list of BarcodeResult objects.
/// </summary>
/// <param name="result">The JSON element containing the barcode scan results.</param>
/// <returns>A list of BarcodeResult objects representing the scan results.</returns>
public static List<BarcodeResult> WrapResult(JsonElement? result)
{
List<BarcodeResult> results = new List<BarcodeResult>();
Expand Down
Loading

0 comments on commit 51cb127

Please sign in to comment.