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

Feature/inverted datamatrix #1

Open
wants to merge 3 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
8 changes: 6 additions & 2 deletions docs/examples/qr-camera/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ <h1 class="title">Scan QR Code from Video Camera</h1>

</main>

<script type="text/javascript" src="https://unpkg.com/@zxing/library@latest"></script>
<script type="text/javascript" src="../../../dist/umd/index.min.js"></script>
<script type="text/javascript">
function decodeOnce(codeReader, selectedDeviceId) {
codeReader.decodeFromInputVideoDevice(selectedDeviceId, 'video').then((result) => {
Expand Down Expand Up @@ -115,9 +115,13 @@ <h1 class="title">Scan QR Code from Video Camera</h1>

window.addEventListener('load', function () {
let selectedDeviceId;
const codeReader = new ZXing.BrowserQRCodeReader()
const codeReader = new ZXing.BrowserMultiFormatReader()
console.log('ZXing code reader initialized')

const hints = new Map();
hints.set(ZXing.DecodeHintType.POSSIBLE_FORMATS, [ZXing.BarcodeFormat.DATA_MATRIX]);
codeReader.setHints = hints;

codeReader.getVideoInputDevices()
.then((videoInputDevices) => {
const sourceSelect = document.getElementById('sourceSelect')
Expand Down
2 changes: 2 additions & 0 deletions src/core/MultiFormatReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export default class MultiFormatReader implements Reader {
}
if (formats.includes(BarcodeFormat.DATA_MATRIX)) {
readers.push(new DataMatrixReader());
readers.push(new DataMatrixReader(true));
}
if (formats.includes(BarcodeFormat.AZTEC)) {
readers.push(new AztecReader());
Expand All @@ -153,6 +154,7 @@ export default class MultiFormatReader implements Reader {

readers.push(new QRCodeReader());
readers.push(new DataMatrixReader());
readers.push(new DataMatrixReader(true));
readers.push(new AztecReader());
readers.push(new PDF417Reader());
// readers.push(new MaxiCodeReader())
Expand Down
4 changes: 4 additions & 0 deletions src/core/common/BitMatrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -519,4 +519,8 @@ export default class BitMatrix /*implements Cloneable*/ {
return new BitMatrix(this.width, this.height, this.rowSize, this.bits.slice());
}

public inverted(): BitMatrix {
return new BitMatrix(this.width, this.height, this.rowSize, this.bits.map(byte => ~byte));
}

}
17 changes: 15 additions & 2 deletions src/core/datamatrix/DataMatrixReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ import Detector from './detector/Detector';
*/
export default class DataMatrixReader implements Reader {

private isInverted: Boolean = false;

public constructor(isInverted?: Boolean) {
if (isInverted != null && isInverted) {
// We use inverted code
this.isInverted = true;
}
}

private static NO_POINTS: ResultPoint[] = [];

private decoder: Decoder = new Decoder();
Expand All @@ -59,11 +68,15 @@ export default class DataMatrixReader implements Reader {
let points: ResultPoint[];

if (hints != null && hints.has(DecodeHintType.PURE_BARCODE)) {
const bits = DataMatrixReader.extractPureBits(image.getBlackMatrix());
const bits = this.isInverted ?
DataMatrixReader.extractPureBits(image.getBlackMatrix().inverted()) :
DataMatrixReader.extractPureBits(image.getBlackMatrix());
decoderResult = this.decoder.decode(bits);
points = DataMatrixReader.NO_POINTS;
} else {
const detectorResult = new Detector(image.getBlackMatrix()).detect();
const detectorResult = this.isInverted ?
new Detector(image.getBlackMatrix().inverted()).detect() :
new Detector(image.getBlackMatrix()).detect();
decoderResult = this.decoder.decode(detectorResult.getBits());
points = detectorResult.getPoints();
}
Expand Down