Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply to various encoding #252

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
node_modules/
dist/
package-lock.json

.idea/
yarn.lock
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ let opts = {

// Only applies to continuous mode. The period, in rendered frames, between scans. A lower scan period
// increases CPU usage but makes scan response faster. Default 1 (i.e. analyze every frame).
scanPeriod: 1
scanPeriod: 1,

// Whether to infer the encoding of the scanned QR code.
// Available encodings are as follows: UTF32, UTF16, UTF16BE, UTF16LE, BINARY, ASCII, JIS, UTF8, EUCJP, SJIS, UNICODE.
// Default false.
inferEncoding: false
};
```

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"babel-polyfill": "^6.9.1",
"fsm-as-promised": "^0.13.0",
"visibilityjs": "^1.2.3",
"webrtc-adapter": "^1.4.0"
"webrtc-adapter": "^1.4.0",
"encoding-japanese": "^1.0.30"
}
}
31 changes: 26 additions & 5 deletions src/scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ const EventEmitter = require('events');
const ZXing = require('./zxing')();
const Visibility = require('visibilityjs');
const StateMachine = require('fsm-as-promised');
const Encoding = require('encoding-japanese');

class ScanProvider {
constructor(emitter, analyzer, captureImage, scanPeriod, refractoryPeriod) {
constructor(emitter, analyzer, captureImage, scanPeriod, refractoryPeriod, inferEncoding) {
this.scanPeriod = scanPeriod;
this.captureImage = captureImage;
this.refractoryPeriod = refractoryPeriod;
this.inferEncoding = inferEncoding;
this._emitter = emitter;
this._frameCount = 0;
this._analyzer = analyzer;
Expand Down Expand Up @@ -83,7 +85,7 @@ class ScanProvider {
}

class Analyzer {
constructor(video) {
constructor(video, inferEncoding) {
this.video = video;

this.imageBuffer = null;
Expand All @@ -98,7 +100,17 @@ class Analyzer {

this.decodeCallback = ZXing.Runtime.addFunction(function (ptr, len, resultIndex, resultCount) {
let result = new Uint8Array(ZXing.HEAPU8.buffer, ptr, len);
let str = String.fromCharCode.apply(null, result);
let str;
if (inferEncoding) {
let detected = Encoding.detect(result);
str = Encoding.convert(result, {
from: detected,
to: 'UNICODE',
type: 'string',
});
} else {
str = String.fromCharCode.apply(null, result);
}
if (resultIndex === 0) {
window.zxDecodeResult = '';
}
Expand Down Expand Up @@ -163,15 +175,16 @@ class Scanner extends EventEmitter {
this.video = this._configureVideo(opts);
this.mirror = (opts.mirror !== false);
this.backgroundScan = (opts.backgroundScan !== false);
let inferEncoding = opts.inferEncoding || false;
this._continuous = (opts.continuous !== false);
this._analyzer = new Analyzer(this.video);
this._analyzer = new Analyzer(this.video, inferEncoding);
this._camera = null;

let captureImage = opts.captureImage || false;
let scanPeriod = opts.scanPeriod || 1;
let refractoryPeriod = opts.refractoryPeriod || (5 * 1000);

this._scanner = new ScanProvider(this, this._analyzer, captureImage, scanPeriod, refractoryPeriod);
this._scanner = new ScanProvider(this, this._analyzer, captureImage, scanPeriod, refractoryPeriod, inferEncoding);
this._fsm = this._createStateMachine();

Visibility.change((e, state) => {
Expand Down Expand Up @@ -244,6 +257,14 @@ class Scanner extends EventEmitter {
return this._scanner.refractoryPeriod;
}

set inferEncoding(encoding) {
this._scanner.inferEncoding = encoding;
}

get inferEncoding() {
return this._scanner.inferEncoding;
}

set continuous(continuous) {
this._continuous = continuous;

Expand Down