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] Support for CODABAR code (#194) #195

Merged
merged 10 commits into from
Apr 19, 2023
11 changes: 11 additions & 0 deletions src/core/common/BitArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,4 +386,15 @@ export default class BitArray /*implements Cloneable*/ {
return new BitArray(this.size, this.bits.slice());
}

/**
* converts to boolean array.
*/
public toArray(): Array<boolean> {
let result = [];
for (let i = 0, size = this.size; i < size; i++) {
result.push(this.get(i));
}
return result;
}

}
133 changes: 133 additions & 0 deletions src/core/oned/CodaBarReader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* Copyright 2008 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*namespace com.google.zxing.oned {*/

import BarcodeFormat from '../BarcodeFormat';
import BitArray from '../common/BitArray';
import DecodeHintType from '../DecodeHintType';
import NotFoundException from '../NotFoundException';
import OneDReader from './OneDReader';
import Result from '../Result';
import ResultPoint from '../ResultPoint';

/**
* <p>Decodes CodaBar barcodes. </p>
*
* @author Evan @dodobelieve
* @see CodaBarReader
*/
export default class CodaBarReader extends OneDReader {

private readonly CODA_BAR_CHAR_SET = {
nnnnnww: '0',
nnnnwwn: '1',
nnnwnnw: '2',
wwnnnnn: '3',
nnwnnwn: '4',
wnnnnwn: '5',
nwnnnnw: '6',
nwnnwnn: '7',
nwwnnnn: '8',
wnnwnnn: '9',
nnnwwnn: '-',
nnwwnnn: '$',
wnnnwnw: ':',
wnwnnnw: '/',
wnwnwnn: '.',
nnwwwww: '+',
nnwwnwn: 'A',
nnnwnww: 'B',
nwnwnnw: 'C',
nnnwwwn: 'D'
};

public decodeRow(rowNumber: number, row: BitArray, hints?: Map<DecodeHintType, any>): Result {
let validRowData = this.getValidRowData(row);
if (!validRowData) throw new NotFoundException();

let retStr = this.codaBarDecodeRow(validRowData.row);
if (!retStr) throw new NotFoundException();
return new Result(
retStr,
null,
0,
[new ResultPoint(validRowData.left, rowNumber), new ResultPoint(validRowData.right, rowNumber)],
BarcodeFormat.CODABAR,
new Date().getTime());
}

/**
* converts bit array to valid data array(lengths of black bits and white bits)
* @param row bit array to convert
*/
private getValidRowData(row: BitArray): any {
let booleanArr = row.toArray();
let startIndex = booleanArr.indexOf(true);
if (startIndex === -1) return null;
let lastIndex = booleanArr.lastIndexOf(true);
if (lastIndex <= startIndex) return null;
booleanArr = booleanArr.slice(startIndex, lastIndex + 1);

let result = [];
let lastBit = booleanArr[0];
let bitLength: number = 1;
for (let i = 1; i < booleanArr.length; i++) {
if (booleanArr[i] === lastBit) {
bitLength++;
} else {
lastBit = booleanArr[i];
result.push(bitLength);
bitLength = 1;
}
}
result.push(bitLength);
// CodaBar code data valid
if (result.length < 23 && (result.length + 1) % 8 !== 0)
return null;
return { row: result, left: startIndex, right: lastIndex };
}

/**
* decode codabar code
* @param row row to cecode
*/
private codaBarDecodeRow(row: Array<number>): string {
const code = [];
const barThreshold = Math.ceil(
row.reduce((pre, item) => (pre + item) / 2, 0)
);
// Read one encoded character at a time.
while (row.length > 0) {
const seg = row.splice(0, 8).splice(0, 7);
const key = seg.map(len => (len < barThreshold ? 'n' : 'w')).join('');
if (this.CODA_BAR_CHAR_SET[key] === undefined) return null;
code.push(this.CODA_BAR_CHAR_SET[key]);
}
let strCode = code.join('');
if (this.validCodaBarString(strCode)) return strCode;
return null;
}

/**
* check if the string is a CodaBar string
* @param src string to determine
*/
private validCodaBarString(src: string): boolean {
let reg = /^[A-D].{1,}[A-D]$/;
return reg.test(src);
}
}
8 changes: 5 additions & 3 deletions src/core/oned/MultiFormatOneDReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import Code93Reader from './Code93Reader';
import ITFReader from './ITFReader';
import MultiFormatUPCEANReader from './MultiFormatUPCEANReader';
import OneDReader from './OneDReader';
import Result from '../Result';
import CodaBarReader from './CodaBarReader';
import RSSExpandedReader from './rss/expanded/RSSExpandedReader';
import RSS14Reader from './rss/RSS14Reader';

Expand Down Expand Up @@ -62,9 +64,9 @@ export default class MultiFormatOneDReader extends OneDReader {
if (possibleFormats.includes(BarcodeFormat.ITF)) {
this.readers.push(new ITFReader());
}
// if (possibleFormats.includes(BarcodeFormat.CODABAR)) {
// this.readers.push(new CodaBarReader());
// }
if (possibleFormats.includes(BarcodeFormat.CODABAR)) {
this.readers.push(new CodaBarReader());
}
if (possibleFormats.includes(BarcodeFormat.RSS_14)) {
this.readers.push(new RSS14Reader());
}
Expand Down
16 changes: 16 additions & 0 deletions src/test/core/common/BitArray.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,22 @@ describe('BitArray', () => {
assert.strictEqual(a.hashCode(), b.hashCode());
});


it('testToArray', () => {
const array = new BitArray(20);
for (let i = 0; i < 10; i++) {
array.set(i);
}
let booleanArr = array.toArray();
for (let i = 0; i < 20; i++) {
if (i < 10) {
assert.strictEqual(booleanArr[i], true);
} else {
assert.strictEqual(booleanArr[i], false);
}
}
});

function reverseOriginal(oldBits: Int32Array, size: number): Int32Array {
const newBits = new Int32Array(oldBits.length);
for (let i = 0; i < size; i++) {
Expand Down
40 changes: 40 additions & 0 deletions src/test/core/oned/CodaBarBlackBox1.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

/*
* Copyright 2008 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// package com.google.zxing.oned;

import BarcodeFormat from '../../../core/BarcodeFormat';
import MultiFormatReader from '../../../core/MultiFormatReader';
import AbstractBlackBoxSpec from '../common/AbstractBlackBox';

/**
* @author Evan @dodobelieve
*/
class CodaBarBlackBox1Spec extends AbstractBlackBoxSpec {
public constructor() {
super('src/test/resources/blackbox/codabar-1', new MultiFormatReader(), BarcodeFormat.CODABAR);
this.addTest(6, 6, 0.0);
this.addTest(6, 6, 180.0);
}
}

describe('CodaBarBlackBox.1', () => {
it('testBlackBox', done => {
const test = new CodaBarBlackBox1Spec();
return test.testBlackBox(done);
});
});
Binary file added src/test/resources/blackbox/codabar-1/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/test/resources/blackbox/codabar-1/1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A4172B
Binary file added src/test/resources/blackbox/codabar-1/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/test/resources/blackbox/codabar-1/2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
C0316D
Binary file added src/test/resources/blackbox/codabar-1/3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/test/resources/blackbox/codabar-1/3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A137286C
Binary file added src/test/resources/blackbox/codabar-1/4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/test/resources/blackbox/codabar-1/4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
B133882D
Binary file added src/test/resources/blackbox/codabar-1/5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/test/resources/blackbox/codabar-1/5.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A26557467D
Binary file added src/test/resources/blackbox/codabar-1/6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/test/resources/blackbox/codabar-1/6.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
D20190815A