Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Keillion committed Sep 19, 2024
1 parent 779a407 commit 32c7ba7
Show file tree
Hide file tree
Showing 13 changed files with 860 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
dist/*
!dist/easy-barcode-scanner.js
node_modules
.npmrc
package-lock.json
24 changes: 24 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <https://unlicense.org>
154 changes: 153 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,153 @@
# easy-barcode-scanner
# Easy Barcode Scanner

The Easy Barcode Scanner is a lightweight, user-friendly wrapper for the Dynamsoft Barcode Reader SDK. It simplifies the barcode scanning process, making it easier to integrate into your web applications with minimal effort.

**Features**
* Supports video-based barcode scanning
* Handles multiple barcodes with ease
* Simple integration with just a few lines of code

## Out-of-the-box Scanning

The simplest way to use Easy Barcode Scanner requires only one line code to create a video decoding web application.

```html
<script src="https://cdn.jsdelivr.net/npm/dynamsoft-barcode-reader-bundle@10.2.1000/dist/dbr.bundle.js"></script>
<script src="https://cdn.jsdelivr.net/gh/Dynamsoft/easy-barcode-scanner@10.2.1007/dist/easy-barcode-scanner.js"
data-license=""></script>
<script>
EasyBarcodeScanner.scan().then(txt=>alert(txt)).catch(ex=>alert(ex.message || ex));
</script>
```
[Source Code >>](https://github.com/Dynamsoft/easy-barcode-scanner/blob/main/index.html) | [Run in github.io >>](https://Dynamsoft.github.io/easy-barcode-scanner/index.html)

![Out-of-the-box Scanning](./out-off-box-scan.png)

## Create Your Own Scanner for Further Control

You can also create your own scanner instance to have more control over the entire workflow. For more details on the encapsulated functionality, refer to `src/index.ts`, and feel free to modify it based on your specific needs.

```html
<div id="camera-view-container" style="height:90vh"></div>
<button id="btn-scan">scan</button>
<script src="https://cdn.jsdelivr.net/npm/dynamsoft-barcode-reader-bundle@10.2.1000/dist/dbr.bundle.js"></script>
<script src="https://cdn.jsdelivr.net/gh/Dynamsoft/easy-barcode-scanner@10.2.1007/dist/easy-barcode-scanner.js"
data-license=""></script>
<script>
let pScanner, scanner;
document.getElementById('btn-scan').addEventListener('click', async()=>{
try{
scanner = await (pScanner || (pScanner = EasyBarcodeScanner.createInstance()));
// Optional. Insert the UI into the specified element.
// Otherwise the UI will be inserted into `document.body`.
document.querySelector("#camera-view-container").append(scanner.getUIElement());
scanner.onUniqueRead = (txt) => { console.log(txt); };
await scanner.open();
}catch(ex){
// If camera doesn't exist or is occupied, the camera may fail to open.
// So it's better to use `try-catch`.
let errMsg = ex.message || ex;
console.error(errMsg);
alert(errMsg);
}
});
</script>
```

## How to use it in frameworks

To integrate Easy Barcode Scanner into your framework, follow these steps:

1. Install the necessary package:

```sh
npm i dynamsoft-barcode-reader-bundle@10.2.1000 -E
```

2. Copy the `src/index.ts` file from the library into your project. Rename it as needed, for example: `[your-path]/easy-barcode-reader.ts.`

**Example 1: Simple Out-of-the-box Scan**
For a simpler implementation, this example shows how to scan with a single function:

```ts
import EasyBarcodeScanner from '[your-path]/easy-barcode-reader';

EasyBarcodeScanner.license = ""; // Add your license key here

async scan(){
try{
alert(await EasyBarcodeScanner.scan());
}catch(ex){
let errMsg = ex.message || ex;
console.error(errMsg);
alert(errMsg);
}
}
```

**Example 2: Setting Up a Scanner**
This example demonstrates how to create a scanner instance and handle barcode readings efficiently:

```tsx
import EasyBarcodeScanner from '[your-path]/easy-barcode-reader';

EasyBarcodeScanner.license = ""; // Add your license key here

let pScanner = null;
let scanner = null;

async mount(){
try{
scanner = await (pScanner || (pScanner = EasyBarcodeScanner.createInstance()));
cameraViewContainer.append(scanner.getUIElement()); // Optional.
scanner.onUniqueRead = (txt) => { console.log(txt); };
await scanner.open();
}catch(ex){
let errMsg = ex.message || ex;
console.error(errMsg);
alert(errMsg);
}
}
beforeUnmount(){
// Clean up to free resources
try{ (await pScanner)?.dispose(); }catch(_){}
}

// usage example in a tsx/jsx component
<div ref={cameraViewContainer}></div>
```

* The `mount()` function initializes the scanner and listens for barcode readings.
* The `beforeUnmount()` function disposes of the scanner instance to prevent memory leaks.

## Customize the UI