-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0184e6e
commit f883c08
Showing
13 changed files
with
230 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# .prettierrc | ||
printWidth: 80 | ||
singleQuote: True |
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 |
---|---|---|
@@ -1,2 +1,5 @@ | ||
# zbar.wasm | ||
|
||
[](https://travis-ci.com/samsam2310/zbar.wasm) | ||
|
||
A wasm build for C/C++ Zbar bar code scan library. |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
.gitignore | ||
zbar.wasm.pre |
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 |
---|---|---|
@@ -1,38 +1,46 @@ | ||
import Module as ApiModule from 'data/zbar' | ||
import * as ApiModule from 'data/zbar'; | ||
|
||
// async function loadImage(src) { | ||
// // Load image | ||
// const imgBlob = await fetch(src).then(resp => resp.blob()); | ||
// const img = await createImageBitmap(imgBlob); | ||
// // Make canvas same size as image | ||
// const canvas = document.createElement('canvas'); | ||
// canvas.width = img.width; | ||
// canvas.height = img.height; | ||
// // Draw image onto canvas | ||
// const ctx = canvas.getContext('2d'); | ||
// ctx.drawImage(img, 0, 0); | ||
// return ctx.getImageData(0, 0, img.width, img.height); | ||
// } | ||
const utf8BufferToString = (buffer, addr) => { | ||
let end = addr; | ||
while (buffer[end]) { | ||
++end; | ||
} | ||
const str = new Uint8Array(buffer.slice(addr, end)); | ||
const encodedString = String.fromCharCode.apply(null, str); | ||
const decodedString = decodeURIComponent(escape(encodedString)); | ||
return decodedString; | ||
}; | ||
|
||
export default Scanner = mixin => { | ||
const mod = ApiModule(mixin); | ||
const api = { | ||
createBuffer: mod.cwrap('createBuffer', 'number', ['number']), | ||
deleteBuffer: mod.cwrap('deleteBuffer', '', ['number']), | ||
scanQrcode: mod.cwrap('scanQrcode', 'number', ['number', 'number', 'number']), | ||
getScanResults: mod.cwrap('getScanResults', '', []), | ||
}; | ||
const scanner = { | ||
scanQrcode: imgData => { | ||
const buf = api.createBuffer(image.width, image.height); | ||
mod.HEAP8.set(imgData.data, buf); | ||
api.scanQrcode(buf, image.width, image.height); | ||
api.deleteBuffer(buf); | ||
// TODO: return result | ||
api.getScanResults(); | ||
} | ||
}; | ||
return new Promise((resolv, reject) => { | ||
mod.then(resolv(scanner)); | ||
const Scanner = mixin => { | ||
const mod = ApiModule(mixin); | ||
const api = { | ||
createBuffer: mod.cwrap('createBuffer', 'number', ['number']), | ||
deleteBuffer: mod.cwrap('deleteBuffer', '', ['number']), | ||
scanQrcode: mod.cwrap('scanQrcode', 'number', [ | ||
'number', | ||
'number', | ||
'number' | ||
]), | ||
getScanResults: mod.cwrap('getScanResults', 'number', []) | ||
}; | ||
const scanner = { | ||
scanQrcode: (imgData, width, height) => { | ||
const buf = api.createBuffer(width * height * 4); | ||
mod.HEAP8.set(imgData, buf); | ||
const results = []; | ||
if (api.scanQrcode(buf, width, height)) { | ||
const res_addr = api.getScanResults(); | ||
results.push(utf8BufferToString(mod.HEAP8, res_addr)); | ||
api.deleteBuffer(res_addr); | ||
} | ||
return results; | ||
} | ||
}; | ||
return new Promise((resolv, reject) => { | ||
mod.then(() => { | ||
resolv(scanner); | ||
}); | ||
}); | ||
}; | ||
|
||
export default Scanner; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
* | ||
*/ | ||
!.gitignore |
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,56 @@ | ||
import Scanner from 'index'; | ||
import fs from 'fs'; | ||
import { createCanvas, loadImage } from 'canvas'; | ||
|
||
console.log(Scanner); | ||
|
||
const readFile = async path => { | ||
fs.open(path, 'r'); | ||
}; | ||
|
||
// const loadImage = async src => { | ||
// // Load image | ||
// const imgBlob = await fetch(src).then(resp => resp.blob()); | ||
// const img = await createImageBitmap(imgBlob); | ||
// // Make canvas same size as image | ||
// const canvas = document.createElement('canvas'); | ||
// canvas.width = img.width; | ||
// canvas.height = img.height; | ||
// // Draw image onto canvas | ||
// const ctx = canvas.getContext('2d'); | ||
// ctx.drawImage(img, 0, 0); | ||
// return ctx.getImageData(0, 0, img.width, img.height); | ||
// } | ||
|
||
const loadImageData = async path => { | ||
const img = await loadImage(path); | ||
const canvas = createCanvas(); | ||
canvas.width = img.width; | ||
canvas.height = img.height; | ||
const ctx = canvas.getContext('2d'); | ||
ctx.drawImage(img, 0, 0); | ||
return { | ||
data: canvas.toBuffer('raw'), | ||
height: img.height, | ||
width: img.width | ||
}; | ||
}; | ||
|
||
const scanImage = async (scanner, imagePath) => { | ||
const img = await loadImageData(imagePath); | ||
console.log(scanner.scanQrcode(img.data, img.width, img.height)); | ||
}; | ||
|
||
const main = async () => { | ||
try { | ||
const scanner = await Scanner({ locateFile: file => './data/' + file }); | ||
scanImage(scanner, './test/test.png'); | ||
scanImage(scanner, './test/test2.png'); | ||
scanImage(scanner, './test/test3.png'); | ||
} catch (e) { | ||
console.log(e); | ||
} finally { | ||
} | ||
}; | ||
|
||
main(); |
File renamed without changes
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,68 @@ | ||
const path = require('path'); | ||
const webpack = require('webpack'); | ||
|
||
const generateConfig = ( | ||
entry, | ||
outputName, | ||
includeFrom, | ||
resolveAlias, | ||
envNames | ||
) => ({ | ||
node: { | ||
__dirname: true | ||
}, | ||
target: 'node', | ||
watchOptions: { | ||
ignored: '/node_modules/', | ||
poll: true | ||
}, | ||
entry: [entry], | ||
output: { | ||
path: path.resolve(__dirname, 'test/build'), | ||
filename: outputName, | ||
}, | ||
resolve: { | ||
extensions: ['.js', '.jsx'], | ||
modules: [path.resolve(__dirname, 'node_modules')], | ||
alias: Object.keys(resolveAlias).reduce(function(previous, key) { | ||
previous[key] = path.resolve(__dirname, resolveAlias[key]); | ||
return previous; | ||
}, {}) | ||
}, | ||
devtool: 'source-map', | ||
plugins: [new webpack.EnvironmentPlugin(envNames)], | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.jsx?$/, | ||
include: path.resolve(__dirname, includeFrom), | ||
use: { | ||
loader: 'babel-loader', | ||
options: { | ||
presets: ['babel-preset-env'], | ||
plugins: [ | ||
require('babel-plugin-transform-runtime'), | ||
] | ||
} | ||
} | ||
}, | ||
{ | ||
test: /\.node$/, | ||
use: 'node-loader' | ||
} | ||
] | ||
} | ||
}); | ||
|
||
module.exports = [ | ||
generateConfig( | ||
'./test/test.js', | ||
'build.js', | ||
'test', | ||
{ | ||
data: './data', | ||
index: './', | ||
}, | ||
[] | ||
) | ||
]; |