More coverage for Code93/39 writers, and for multiple barcode detection

This commit is contained in:
Sean Owen 2016-09-11 13:44:13 +01:00
parent 0718d8a45f
commit 30dc420bab
17 changed files with 193 additions and 78 deletions

View file

@ -63,16 +63,20 @@ public abstract class AbstractBlackBoxTestCase extends Assert {
private final BarcodeFormat expectedFormat;
private final List<TestResult> testResults;
protected AbstractBlackBoxTestCase(String testBasePathSuffix,
Reader barcodeReader,
BarcodeFormat expectedFormat) {
public static Path buildTestBase(String testBasePathSuffix) {
// A little workaround to prevent aggravation in my IDE
Path testBase = Paths.get(testBasePathSuffix);
if (!Files.exists(testBase)) {
// try starting with 'core' since the test base is often given as the project root
testBase = Paths.get("core").resolve(testBasePathSuffix);
}
this.testBase = testBase;
return testBase;
}
protected AbstractBlackBoxTestCase(String testBasePathSuffix,
Reader barcodeReader,
BarcodeFormat expectedFormat) {
this.testBase = buildTestBase(testBasePathSuffix);
this.barcodeReader = barcodeReader;
this.expectedFormat = expectedFormat;
testResults = new ArrayList<>();

View file

@ -264,6 +264,15 @@ public final class BitMatrixTestCase extends Assert {
}
}
public static String matrixToString(BitMatrix result) {
assertEquals(1, result.getHeight());
StringBuilder builder = new StringBuilder(result.getWidth());
for (int i = 0; i < result.getWidth(); i++) {
builder.append(result.get(i, 0) ? '1' : '0');
}
return builder.toString();
}
private static void testXOR(BitMatrix dataMatrix, BitMatrix flipMatrix, BitMatrix expectedMatrix) {
BitMatrix matrix = dataMatrix.clone();
matrix.xor(flipMatrix);

View file

@ -0,0 +1,58 @@
/*
* Copyright 2016 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.multi;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.nio.file.Path;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.BufferedImageLuminanceSource;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Result;
import com.google.zxing.common.AbstractBlackBoxTestCase;
import com.google.zxing.common.HybridBinarizer;
import org.junit.Assert;
import org.junit.Test;
public final class MultiTestCase extends Assert {
@Test
public void testMulti() throws Exception {
// Very basic test for now
Path testBase = AbstractBlackBoxTestCase.buildTestBase("src/test/resources/blackbox/multi-1");
Path testImage = testBase.resolve("1.png");
BufferedImage image = ImageIO.read(testImage.toFile());
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
MultipleBarcodeReader reader = new GenericMultipleBarcodeReader(new MultiFormatReader());
Result[] results = reader.decodeMultiple(bitmap);
assertNotNull(results);
assertEquals(2, results.length);
assertEquals("031415926531", results[0].getText());
assertEquals(BarcodeFormat.UPC_A, results[0].getBarcodeFormat());
assertEquals("www.airtable.com/jobs", results[1].getText());
assertEquals(BarcodeFormat.QR_CODE, results[1].getBarcodeFormat());
}
}

View file

@ -18,9 +18,7 @@ package com.google.zxing.multi.qrcode;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
@ -31,6 +29,7 @@ import com.google.zxing.BufferedImageLuminanceSource;
import com.google.zxing.LuminanceSource;
import com.google.zxing.Result;
import com.google.zxing.ResultMetadataType;
import com.google.zxing.common.AbstractBlackBoxTestCase;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.multi.MultipleBarcodeReader;
import org.junit.Assert;
@ -41,10 +40,7 @@ public final class MultiQRCodeTestCase extends Assert {
@Test
public void testMultiQRCodes() throws Exception {
// Very basic test for now
Path testBase = Paths.get("src/test/resources/blackbox/multi-qrcode-1");
if (!Files.exists(testBase)) {
testBase = Paths.get("core").resolve(testBase);
}
Path testBase = AbstractBlackBoxTestCase.buildTestBase("src/test/resources/blackbox/multi-qrcode-1");
Path testImage = testBase.resolve("1.png");
BufferedImage image = ImageIO.read(testImage.toFile());

View file

@ -19,6 +19,7 @@ package com.google.zxing.oned;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.BitMatrixTestCase;
import org.junit.Assert;
import org.junit.Test;
@ -52,11 +53,7 @@ public final class CodaBarWriterTestCase extends Assert {
private static void doTest(String input, CharSequence expected) throws WriterException {
BitMatrix result = encode(input);
StringBuilder actual = new StringBuilder(result.getWidth());
for (int i = 0; i < result.getWidth(); i++) {
actual.append(result.get(i, 0) ? '1' : '0');
}
assertEquals(expected, actual.toString());
assertEquals(expected, BitMatrixTestCase.matrixToString(result));
}
private static BitMatrix encode(String input) throws WriterException {

View file

@ -16,6 +16,7 @@
package com.google.zxing.oned;
import com.google.zxing.common.BitMatrixTestCase;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
@ -56,7 +57,7 @@ public class Code128WriterTestCase extends Assert {
BitMatrix result = writer.encode(toEncode, BarcodeFormat.CODE_128, 0, 0);
String actual = matrixToString(result);
String actual = BitMatrixTestCase.matrixToString(result);
assertEquals(expected, actual);
}
@ -68,7 +69,7 @@ public class Code128WriterTestCase extends Assert {
BitMatrix result = writer.encode(toEncode, BarcodeFormat.CODE_128, 0, 0);
String actual = matrixToString(result);
String actual = BitMatrixTestCase.matrixToString(result);
assertEquals(expected, actual);
}
@ -80,7 +81,7 @@ public class Code128WriterTestCase extends Assert {
BitMatrix result = writer.encode(toEncode, BarcodeFormat.CODE_128, 0, 0);
String actual = matrixToString(result);
String actual = BitMatrixTestCase.matrixToString(result);
assertEquals(expected, actual);
}
@ -104,15 +105,9 @@ public class Code128WriterTestCase extends Assert {
BitMatrix result = writer.encode(toEncode, BarcodeFormat.CODE_128, 0, 0);
String actual = matrixToString(result);
String actual = BitMatrixTestCase.matrixToString(result);
assertEquals(expected, actual);
}
private static String matrixToString(BitMatrix result) {
StringBuilder builder = new StringBuilder(result.getWidth());
for (int i = 0; i < result.getWidth(); i++) {
builder.append(result.get(i, 0) ? '1' : '0');
}
return builder.toString();
}
}

View file

@ -0,0 +1,44 @@
/*
* Copyright 2016 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 com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.BitMatrixTestCase;
import org.junit.Assert;
import org.junit.Test;
public final class Code39WriterTestCase extends Assert {
@Test
public void testEncode() throws WriterException {
doTest("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
"000001001011011010110101001011010110100101101101101001010101011001011011010110010101" +
"011011001010101010011011011010100110101011010011010101011001101011010101001101011010" +
"100110110110101001010101101001101101011010010101101101001010101011001101101010110010" +
"101101011001010101101100101100101010110100110101011011001101010101001011010110110010" +
"110101010011011010101010011011010110100101011010110010101101101100101010101001101011" +
"01101001101010101100110101010100101101101101001011010101100101101010010110110100000");
}
private static void doTest(String input, CharSequence expected) throws WriterException {
BitMatrix result = new Code39Writer().encode(input, BarcodeFormat.CODE_39, 0, 0);
assertEquals(expected, BitMatrixTestCase.matrixToString(result));
}
}

View file

@ -0,0 +1,43 @@
/*
* Copyright 2016 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 com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.BitMatrixTestCase;
import org.junit.Assert;
import org.junit.Test;
public final class Code93WriterTestCase extends Assert {
@Test
public void testEncode() throws WriterException {
doTest("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
"000001010111101101010001101001001101000101100101001100100101100010101011010001011001" +
"001011000101001101001000110101010110001010011001010001101001011001000101101101101001" +
"101100101101011001101001101100101101100110101011011001011001101001101101001110101000" +
"101001010010001010001001010000101001010001001001001001000101010100001000100101000010" +
"10100111010101000010101011110100000");
}
private static void doTest(String input, CharSequence expected) throws WriterException {
BitMatrix result = new Code93Writer().encode(input, BarcodeFormat.CODE_93, 0, 0);
assertEquals(expected, BitMatrixTestCase.matrixToString(result));
}
}

View file

@ -19,6 +19,7 @@ package com.google.zxing.oned;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.BitMatrixTestCase;
import org.junit.Assert;
import org.junit.Test;
@ -29,11 +30,9 @@ public final class EAN13WriterTestCase extends Assert {
@Test
public void testEncode() throws WriterException {
CharSequence testStr = "00010100010110100111011001100100110111101001110101010110011011011001000010101110010011101000100101000";
String testStr = "00010100010110100111011001100100110111101001110101010110011011011001000010101110010011101000100101000";
BitMatrix result = new EAN13Writer().encode("5901234123457", BarcodeFormat.EAN_13, testStr.length(), 0);
for (int i = 0; i < testStr.length(); i++) {
assertEquals("Element " + i, testStr.charAt(i) == '1', result.get(i, 0));
}
assertEquals(testStr, BitMatrixTestCase.matrixToString(result));
}
}

View file

@ -19,6 +19,7 @@ package com.google.zxing.oned;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.BitMatrixTestCase;
import org.junit.Assert;
import org.junit.Test;
@ -29,11 +30,9 @@ public final class EAN8WriterTestCase extends Assert {
@Test
public void testEncode() throws WriterException {
CharSequence testStr = "0001010001011010111101111010110111010101001110111001010001001011100101000";
String testStr = "0001010001011010111101111010110111010101001110111001010001001011100101000";
BitMatrix result = new EAN8Writer().encode("96385074", BarcodeFormat.EAN_8, testStr.length(), 0);
for (int i = 0; i < testStr.length(); i++) {
assertEquals("Element " + i, testStr.charAt(i) == '1', result.get(i, 0));
}
assertEquals(testStr, BitMatrixTestCase.matrixToString(result));
}
}

View file

@ -20,6 +20,7 @@ import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.BitMatrixTestCase;
import org.junit.Assert;
import org.junit.Test;
@ -30,20 +31,16 @@ public final class UPCAWriterTestCase extends Assert {
@Test
public void testEncode() throws WriterException {
CharSequence testStr = "00010101000110110111011000100010110101111011110101010111001011101001001110110011011011001011100101000";
String testStr = "00010101000110110111011000100010110101111011110101010111001011101001001110110011011011001011100101000";
BitMatrix result = new UPCAWriter().encode("485963095124", BarcodeFormat.UPC_A, testStr.length(), 0);
for (int i = 0; i < testStr.length(); i++) {
assertEquals("Element " + i, testStr.charAt(i) == '1', result.get(i, 0));
}
assertEquals(testStr, BitMatrixTestCase.matrixToString(result));
}
@Test
public void testAddChecksumAndEncode() throws WriterException {
CharSequence testStr = "00010100110010010011011110101000110110001010111101010100010010010001110100111001011001101101100101000";
String testStr = "00010100110010010011011110101000110110001010111101010100010010010001110100111001011001101101100101000";
BitMatrix result = new UPCAWriter().encode("12345678901", BarcodeFormat.UPC_A, testStr.length(), 0);
for (int i = 0; i < testStr.length(); i++) {
assertEquals("Element " + i, testStr.charAt(i) == '1', result.get(i, 0));
}
assertEquals(testStr, BitMatrixTestCase.matrixToString(result));
}
}

View file

@ -28,9 +28,7 @@ package com.google.zxing.oned.rss.expanded;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import javax.imageio.ImageIO;
@ -39,6 +37,7 @@ import com.google.zxing.BinaryBitmap;
import com.google.zxing.BufferedImageLuminanceSource;
import com.google.zxing.NotFoundException;
import com.google.zxing.ReaderException;
import com.google.zxing.common.AbstractBlackBoxTestCase;
import com.google.zxing.common.BitArray;
import com.google.zxing.common.GlobalHistogramBinarizer;
import org.junit.Assert;
@ -176,11 +175,7 @@ public final class RSSExpandedImage2binaryTestCase extends Assert {
private static void assertCorrectImage2binary(String fileName, String expected)
throws IOException, NotFoundException {
Path path = Paths.get("src/test/resources/blackbox/rssexpanded-1/").resolve(fileName);
if (!Files.exists(path)) {
// Support running from project root too
path = Paths.get("core").resolve(path);
}
Path path = AbstractBlackBoxTestCase.buildTestBase("src/test/resources/blackbox/rssexpanded-1/").resolve(fileName);
BufferedImage image = ImageIO.read(path.toFile());
BinaryBitmap binaryMap = new BinaryBitmap(new GlobalHistogramBinarizer(new BufferedImageLuminanceSource(image)));

View file

@ -33,9 +33,7 @@ package com.google.zxing.oned.rss.expanded;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import javax.imageio.ImageIO;
@ -49,6 +47,7 @@ import com.google.zxing.Result;
import com.google.zxing.client.result.ExpandedProductParsedResult;
import com.google.zxing.client.result.ParsedResult;
import com.google.zxing.client.result.ResultParser;
import com.google.zxing.common.AbstractBlackBoxTestCase;
import com.google.zxing.common.BitArray;
import com.google.zxing.common.GlobalHistogramBinarizer;
@ -77,11 +76,7 @@ public final class RSSExpandedImage2resultTestCase extends Assert {
private static void assertCorrectImage2result(String fileName, ExpandedProductParsedResult expected)
throws IOException, NotFoundException {
Path path = Paths.get("src/test/resources/blackbox/rssexpanded-1/").resolve(fileName);
if (!Files.exists(path)) {
// Support running from project root too
path = Paths.get("core").resolve(path);
}
Path path = AbstractBlackBoxTestCase.buildTestBase("src/test/resources/blackbox/rssexpanded-1/").resolve(fileName);
BufferedImage image = ImageIO.read(path.toFile());
BinaryBitmap binaryMap = new BinaryBitmap(new GlobalHistogramBinarizer(new BufferedImageLuminanceSource(image)));

View file

@ -28,9 +28,7 @@ package com.google.zxing.oned.rss.expanded;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.imageio.ImageIO;
@ -40,6 +38,7 @@ import com.google.zxing.BufferedImageLuminanceSource;
import com.google.zxing.NotFoundException;
import com.google.zxing.ReaderException;
import com.google.zxing.Result;
import com.google.zxing.common.AbstractBlackBoxTestCase;
import com.google.zxing.common.BitArray;
import com.google.zxing.common.GlobalHistogramBinarizer;
import org.junit.Assert;
@ -189,11 +188,7 @@ public final class RSSExpandedImage2stringTestCase extends Assert {
private static void assertCorrectImage2string(String fileName, String expected)
throws IOException, NotFoundException {
Path path = Paths.get("src/test/resources/blackbox/rssexpanded-1/").resolve(fileName);
if (!Files.exists(path)) {
// Support running from project root too
path = Paths.get("core").resolve(path);
}
Path path = AbstractBlackBoxTestCase.buildTestBase("src/test/resources/blackbox/rssexpanded-1/").resolve(fileName);
BufferedImage image = ImageIO.read(path.toFile());
BinaryBitmap binaryMap =

View file

@ -28,9 +28,7 @@ package com.google.zxing.oned.rss.expanded;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
@ -39,6 +37,7 @@ import javax.imageio.ImageIO;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.BufferedImageLuminanceSource;
import com.google.zxing.NotFoundException;
import com.google.zxing.common.AbstractBlackBoxTestCase;
import com.google.zxing.common.BitArray;
import com.google.zxing.common.GlobalHistogramBinarizer;
import com.google.zxing.oned.rss.DataCharacter;
@ -145,12 +144,7 @@ public final class RSSExpandedInternalTestCase extends Assert {
}
private static BufferedImage readImage(String fileName) throws IOException {
Path path = Paths.get("src/test/resources/blackbox/rssexpanded-1/").resolve(fileName);
if (!Files.exists(path)) {
// Support running from project root too
path = Paths.get("core").resolve(path);
}
Path path = AbstractBlackBoxTestCase.buildTestBase("src/test/resources/blackbox/rssexpanded-1/").resolve(fileName);
return ImageIO.read(path.toFile());
}

View file

@ -28,14 +28,13 @@ package com.google.zxing.oned.rss.expanded;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.imageio.ImageIO;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.BufferedImageLuminanceSource;
import com.google.zxing.common.AbstractBlackBoxTestCase;
import com.google.zxing.common.GlobalHistogramBinarizer;
final class TestCaseUtil {
@ -44,11 +43,7 @@ final class TestCaseUtil {
}
private static BufferedImage getBufferedImage(String path) throws IOException {
Path file = Paths.get(path);
if (!Files.exists(file)) {
// Support running from project root too
file = Paths.get("core").resolve(file);
}
Path file = AbstractBlackBoxTestCase.buildTestBase(path);
return ImageIO.read(file.toFile());
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB