Skip to content

Commit

Permalink
Merge pull request #138 from mpustelnik/feature/ean8-support
Browse files Browse the repository at this point in the history
Ported EAN8 reader
  • Loading branch information
odahcam authored Feb 22, 2019
2 parents 88901f8 + a0b020b commit c5ebcc3
Show file tree
Hide file tree
Showing 20 changed files with 127 additions and 3 deletions.
71 changes: 71 additions & 0 deletions src/core/oned/EAN8Reader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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.
*/

import BarcodeFormat from '../BarcodeFormat';
import BitArray from '../common/BitArray';

import UPCEANReader from './UPCEANReader';

/**
* <p>Implements decoding of the EAN-8 format.</p>
*
* @author Sean Owen
*/
export default class EAN8Reader extends UPCEANReader {
private decodeMiddleCounters: number[];

public constructor() {
super();
this.decodeMiddleCounters = [0, 0, 0, 0];
}

public decodeMiddle(row: BitArray, startRange: number[], resultString: string) {
let counters = this.decodeMiddleCounters;
counters[0] = 0;
counters[1] = 0;
counters[2] = 0;
counters[3] = 0;
let end = row.getSize();
let rowOffset = startRange[1];

for (let x = 0; x < 4 && rowOffset < end; x++) {
let bestMatch = UPCEANReader.decodeDigit(row, counters, rowOffset, UPCEANReader.L_PATTERNS);
resultString += String.fromCharCode(('0'.charCodeAt(0) + bestMatch));

for (let counter of counters) {
rowOffset += counter;
}
}

let middleRange = UPCEANReader.findGuardPattern(row, rowOffset, true, UPCEANReader.MIDDLE_PATTERN, new Array(UPCEANReader.MIDDLE_PATTERN.length).fill(0));
rowOffset = middleRange[1];

for (let x = 0; x < 4 && rowOffset < end; x++) {
let bestMatch = UPCEANReader.decodeDigit(row, counters, rowOffset, UPCEANReader.L_PATTERNS);
resultString += String.fromCharCode(('0'.charCodeAt(0) + bestMatch));

for (let counter of counters) {
rowOffset += counter;
}
}

return {rowOffset, resultString};
}

public getBarcodeFormat(): BarcodeFormat {
return BarcodeFormat.EAN_8;
}
}
3 changes: 2 additions & 1 deletion src/core/oned/MultiFormatOneDReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ export default class MultiFormatOneDReader extends OneDReader {
const useCode39CheckDigit = hints && hints.get(DecodeHintType.ASSUME_CODE_39_CHECK_DIGIT) !== undefined;

if (possibleFormats) {
if (possibleFormats.includes(BarcodeFormat.EAN_13)) {
if (possibleFormats.includes(BarcodeFormat.EAN_13) ||
possibleFormats.includes(BarcodeFormat.EAN_8)) {
this.readers.push(new MultiFormatUPCEANReader(hints));
}
// if (possibleFormats.includes(BarcodeFormat.EAN_13) ||
Expand Down
10 changes: 8 additions & 2 deletions src/core/oned/MultiFormatUPCEANReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import Result from '../Result';
import OneDReader from './OneDReader';
import UPCEANReader from './UPCEANReader';
import EAN13Reader from './EAN13Reader';
import EAN8Reader from './EAN8Reader';
import NotFoundException from '../NotFoundException';

/**
Expand All @@ -42,12 +43,17 @@ export default class MultiFormatUPCEANReader extends OneDReader {
if (possibleFormats.indexOf(BarcodeFormat.EAN_13) > -1) {
readers.push(new EAN13Reader());
}
// todo add UPC_A, EAN_8, UPC_E

if (possibleFormats.indexOf(BarcodeFormat.EAN_8) > -1) {
readers.push(new EAN8Reader());
}
// todo add UPC_A, UPC_E
}

if (readers.length === 0) {
readers.push(new EAN13Reader());
// todo add UPC_A, EAN_8, UPC_E
readers.push(new EAN8Reader());
// todo add UPC_A, UPC_E
}

this.readers = readers;
Expand Down
38 changes: 38 additions & 0 deletions src/test/core/oned/Ean8BlackBox1.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.
*/

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

/**
* @author Sean Owen
*/
class Ean8BlackBox1Spec extends AbstractBlackBoxSpec {

public constructor() {
super('src/test/resources/blackbox/ean8-1', new MultiFormatReader(), BarcodeFormat.EAN_8);
this.addTest(8, 8, 0.0);
this.addTest(8, 8, 180.0);
}
}

describe('Ean8BlackBox1Spec.1', () => {
it('testBlackBox', done => {
const test = new Ean8BlackBox1Spec();
return test.testBlackBox(done);
});
});
Binary file added src/test/resources/blackbox/ean8-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/ean8-1/1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
48512343
Binary file added src/test/resources/blackbox/ean8-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/ean8-1/2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12345670
Binary file added src/test/resources/blackbox/ean8-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/ean8-1/3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12345670
Binary file added src/test/resources/blackbox/ean8-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/ean8-1/4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
67678983
Binary file added src/test/resources/blackbox/ean8-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/ean8-1/5.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
80674313
Binary file added src/test/resources/blackbox/ean8-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/ean8-1/6.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
59001270
Binary file added src/test/resources/blackbox/ean8-1/7.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/ean8-1/7.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
50487066
Binary file added src/test/resources/blackbox/ean8-1/8.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/ean8-1/8.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
55123457

0 comments on commit c5ebcc3

Please sign in to comment.