-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from maslick/browser-module
publish browser, CJS and ESM versions in a single npm package
- Loading branch information
Showing
8 changed files
with
88 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
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}`}); | ||
|
||
// Initialize a glue API object (between JavaScript and C++ code) | ||
this.api = { | ||
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', []) | ||
}; | ||
|
||
// return the class | ||
return this; | ||
})(); | ||
} | ||
|
||
decode(imgData, width, height) { | ||
const buffer = this.api.createBuffer(width * height * 4); | ||
this.mod.HEAPU8.set(imgData, buffer); | ||
const results = []; | ||
if (this.api.triggerDecode(buffer, width, height) > 0) { | ||
const resultAddress = this.api.getScanResults(); | ||
results.push(this.mod.UTF8ToString(resultAddress)); | ||
this.api.deleteBuffer(resultAddress); | ||
} | ||
if (results.length > 0) return results[0]; | ||
else return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import CreateKoder from './zbar.js'; | ||
|
||
class Koder { | ||
get initialized() { | ||
return (async () => { | ||
// Load WASM file | ||
console.log("ESM"); | ||
this.mod = await CreateKoder(); | ||
|
||
// Initialize a glue API object (between JavaScript and C++ code) | ||
this.api = { | ||
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', []) | ||
}; | ||
|
||
// return the class | ||
return this; | ||
})(); | ||
} | ||
|
||
decode(imgData, width, height) { | ||
const buffer = this.api.createBuffer(width * height * 4); | ||
this.mod.HEAPU8.set(imgData, buffer); | ||
const results = []; | ||
if (this.api.triggerDecode(buffer, width, height) > 0) { | ||
const resultAddress = this.api.getScanResults(); | ||
results.push(this.mod.UTF8ToString(resultAddress)); | ||
this.api.deleteBuffer(resultAddress); | ||
} | ||
if (results.length > 0) return results[0]; | ||
else return null; | ||
} | ||
} | ||
|
||
export default Koder; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters