diff --git a/src/core/oned/EAN8Reader.ts b/src/core/oned/EAN8Reader.ts new file mode 100644 index 00000000..90d3ac30 --- /dev/null +++ b/src/core/oned/EAN8Reader.ts @@ -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'; + +/** + *

Implements decoding of the EAN-8 format.

+ * + * @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; + } +} diff --git a/src/core/oned/MultiFormatOneDReader.ts b/src/core/oned/MultiFormatOneDReader.ts index 4d9b3088..08c6905d 100644 --- a/src/core/oned/MultiFormatOneDReader.ts +++ b/src/core/oned/MultiFormatOneDReader.ts @@ -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) || diff --git a/src/core/oned/MultiFormatUPCEANReader.ts b/src/core/oned/MultiFormatUPCEANReader.ts index 493ce419..e4be3409 100644 --- a/src/core/oned/MultiFormatUPCEANReader.ts +++ b/src/core/oned/MultiFormatUPCEANReader.ts @@ -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'; /** @@ -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; diff --git a/src/test/core/oned/Ean8BlackBox1.spec.ts b/src/test/core/oned/Ean8BlackBox1.spec.ts new file mode 100644 index 00000000..703b5036 --- /dev/null +++ b/src/test/core/oned/Ean8BlackBox1.spec.ts @@ -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); + }); +}); diff --git a/src/test/resources/blackbox/ean8-1/1.png b/src/test/resources/blackbox/ean8-1/1.png new file mode 100644 index 00000000..14195c2e Binary files /dev/null and b/src/test/resources/blackbox/ean8-1/1.png differ diff --git a/src/test/resources/blackbox/ean8-1/1.txt b/src/test/resources/blackbox/ean8-1/1.txt new file mode 100644 index 00000000..cf23e896 --- /dev/null +++ b/src/test/resources/blackbox/ean8-1/1.txt @@ -0,0 +1 @@ +48512343 \ No newline at end of file diff --git a/src/test/resources/blackbox/ean8-1/2.png b/src/test/resources/blackbox/ean8-1/2.png new file mode 100644 index 00000000..d9d93bca Binary files /dev/null and b/src/test/resources/blackbox/ean8-1/2.png differ diff --git a/src/test/resources/blackbox/ean8-1/2.txt b/src/test/resources/blackbox/ean8-1/2.txt new file mode 100644 index 00000000..c1c11781 --- /dev/null +++ b/src/test/resources/blackbox/ean8-1/2.txt @@ -0,0 +1 @@ +12345670 \ No newline at end of file diff --git a/src/test/resources/blackbox/ean8-1/3.png b/src/test/resources/blackbox/ean8-1/3.png new file mode 100644 index 00000000..2e29b6bf Binary files /dev/null and b/src/test/resources/blackbox/ean8-1/3.png differ diff --git a/src/test/resources/blackbox/ean8-1/3.txt b/src/test/resources/blackbox/ean8-1/3.txt new file mode 100644 index 00000000..c1c11781 --- /dev/null +++ b/src/test/resources/blackbox/ean8-1/3.txt @@ -0,0 +1 @@ +12345670 \ No newline at end of file diff --git a/src/test/resources/blackbox/ean8-1/4.png b/src/test/resources/blackbox/ean8-1/4.png new file mode 100644 index 00000000..ab362700 Binary files /dev/null and b/src/test/resources/blackbox/ean8-1/4.png differ diff --git a/src/test/resources/blackbox/ean8-1/4.txt b/src/test/resources/blackbox/ean8-1/4.txt new file mode 100644 index 00000000..2ab0cf49 --- /dev/null +++ b/src/test/resources/blackbox/ean8-1/4.txt @@ -0,0 +1 @@ +67678983 \ No newline at end of file diff --git a/src/test/resources/blackbox/ean8-1/5.png b/src/test/resources/blackbox/ean8-1/5.png new file mode 100644 index 00000000..8dba6847 Binary files /dev/null and b/src/test/resources/blackbox/ean8-1/5.png differ diff --git a/src/test/resources/blackbox/ean8-1/5.txt b/src/test/resources/blackbox/ean8-1/5.txt new file mode 100644 index 00000000..4911bf2b --- /dev/null +++ b/src/test/resources/blackbox/ean8-1/5.txt @@ -0,0 +1 @@ +80674313 \ No newline at end of file diff --git a/src/test/resources/blackbox/ean8-1/6.png b/src/test/resources/blackbox/ean8-1/6.png new file mode 100644 index 00000000..87f88938 Binary files /dev/null and b/src/test/resources/blackbox/ean8-1/6.png differ diff --git a/src/test/resources/blackbox/ean8-1/6.txt b/src/test/resources/blackbox/ean8-1/6.txt new file mode 100644 index 00000000..d0ada6f4 --- /dev/null +++ b/src/test/resources/blackbox/ean8-1/6.txt @@ -0,0 +1 @@ +59001270 \ No newline at end of file diff --git a/src/test/resources/blackbox/ean8-1/7.png b/src/test/resources/blackbox/ean8-1/7.png new file mode 100644 index 00000000..1a99a312 Binary files /dev/null and b/src/test/resources/blackbox/ean8-1/7.png differ diff --git a/src/test/resources/blackbox/ean8-1/7.txt b/src/test/resources/blackbox/ean8-1/7.txt new file mode 100644 index 00000000..36db36bc --- /dev/null +++ b/src/test/resources/blackbox/ean8-1/7.txt @@ -0,0 +1 @@ +50487066 \ No newline at end of file diff --git a/src/test/resources/blackbox/ean8-1/8.png b/src/test/resources/blackbox/ean8-1/8.png new file mode 100644 index 00000000..faddfd7d Binary files /dev/null and b/src/test/resources/blackbox/ean8-1/8.png differ diff --git a/src/test/resources/blackbox/ean8-1/8.txt b/src/test/resources/blackbox/ean8-1/8.txt new file mode 100644 index 00000000..a6c30695 --- /dev/null +++ b/src/test/resources/blackbox/ean8-1/8.txt @@ -0,0 +1 @@ +55123457 \ No newline at end of file