Skip to content

Commit

Permalink
Merge pull request #83 from maslick/return-qr-code-type
Browse files Browse the repository at this point in the history
Return qr code type
  • Loading branch information
maslick committed Sep 24, 2023
2 parents 4957e73 + f44ab98 commit fadf6e5
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 19 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,26 @@ docker build -t maslick/emscripten-zbar-sdk -f docker/Dockerfile docker

### Build WASM artifacts
```shell
# React app
# Linux, Mac Intel
docker run \
-e INPUT_FILE=zbar/qr.cpp \
-e OUTPUT_FILE=zbar \
-e OUTPUT_DIR=public/wasm \
-v $(pwd):/app \
maslick/emscripten-zbar-sdk make -B

# Mac M1/M2
docker run \
--platform linux/amd64 \
-e INPUT_FILE=zbar/qr.cpp \
-e OUTPUT_FILE=zbar \
-e OUTPUT_DIR=public/wasm \
-v $(pwd):/app \
maslick/emscripten-zbar-sdk make -B
```

### Clean the build artifacts (if necessary):
```shell
# React app
OUTPUT_DIR=public/wasm OUTPUT_FILE=zbar make clean
```

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "koder-react",
"version": "1.10.0",
"version": "1.11.0",
"homepage": "./",
"private": true,
"description": "QR/bar code scanner for the Browser",
Expand Down
10 changes: 7 additions & 3 deletions public/wasm/koder.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ class Koder {
initialize(config) {
return (async () => {
// Load WASM file
console.log("Browser");
config ||= {};
const directory = config.wasmDirectory || "./wasm";
this.mod = await CreateKoder({locateFile: file => `${directory}/${file}`});
Expand All @@ -12,7 +11,8 @@ class Koder {
createBuffer: this.mod.cwrap('createBuffer', 'number', ['number']),
deleteBuffer: this.mod.cwrap('deleteBuffer', '', ['number']),
triggerDecode: this.mod.cwrap('triggerDecode', 'number', ['number', 'number', 'number']),
getScanResults: this.mod.cwrap('getScanResults', 'number', [])
getScanResults: this.mod.cwrap('getScanResults', 'number', []),
getResultType: this.mod.cwrap('getResultType', 'number', []),
};

// return the class
Expand All @@ -26,7 +26,11 @@ class Koder {
const results = [];
if (this.api.triggerDecode(buffer, width, height) > 0) {
const resultAddress = this.api.getScanResults();
results.push(this.mod.UTF8ToString(resultAddress));
const resultType = this.api.getResultType();
results.push({
code: this.mod.UTF8ToString(resultAddress),
type: this.mod.UTF8ToString(resultType)
});
this.api.deleteBuffer(resultAddress);
}
if (results.length > 0) return results[0];
Expand Down
8 changes: 4 additions & 4 deletions public/wasm/zbar.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified public/wasm/zbar.wasm
Binary file not shown.
4 changes: 2 additions & 2 deletions public/wasmWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ importScripts("wasm/koder.js");
const scanResult = koder.decode(data, this.width, this.height);
const t1 = new Date().getTime();
if (scanResult) {
console.log(`Scanned in ${t1-t0} ms`);
postMessage({
data: scanResult,
data: scanResult.code,
type: scanResult.type,
ms: t1-t0
});
}
Expand Down
4 changes: 1 addition & 3 deletions src/components/scan.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import "../css/scan.css";

const BTN_TXT = {
START: "START",
STOP: "STOP",
AGAIN: "START AGAIN"
STOP: "STOP"
};

const CANVAS_SIZE = {
Expand Down Expand Up @@ -145,7 +144,6 @@ export default function Scan({
setBtnText(BTN_TXT.START);
await video.pause();
if (video.srcObject) {
console.log("stopping video...")
video.srcObject.getVideoTracks().forEach(track => track.stop());
video.srcObject = null;
}
Expand Down
22 changes: 18 additions & 4 deletions zbar/qr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,30 @@ extern "C" {
}
if (symb_p == image->symbol_end())
return NULL;
std::cout << "decoded " << symb_p->get_type_name() << std::endl;
std::cout << symb_p->get_data() << std::endl;
std::string data = symb_p->get_data();
char* str = (char*)malloc(data.size() + 1);
strcpy(str, data.c_str());
++ symb_p;
return str;
}

/**
* Returns the type of the QR code
*
* @return char* containing the type of QR code
*/
EXPORT const char* getResultType() {
if (!image) {
std::cerr << "Call triggerDecode first to get scan result\n";
return NULL;
}
if (symb_p == image->symbol_end())
return NULL;
std::string type = symb_p->get_type_name();
char* str = (char*)malloc(type.size() + 1);
strcpy(str, type.c_str());
++ symb_p;
return str;
}
}

int main(int argc, char** argv) {
Expand All @@ -89,5 +104,4 @@ int main(int argc, char** argv) {
scanner.set_config(zbar::ZBAR_CODE128, zbar::ZBAR_CFG_ENABLE, 1);
scanner.set_config(zbar::ZBAR_CODABAR, zbar::ZBAR_CFG_ENABLE, 1);
scanner.set_config(zbar::ZBAR_DATABAR, zbar::ZBAR_CFG_ENABLE, 1);
std::cout << "QR/Barcode scanner initialized" << std::endl;
}

0 comments on commit fadf6e5

Please sign in to comment.