X-Git-Url: http://git.rot13.org/?p=zxing.git;a=blobdiff_plain;f=core%2Ftest%2Fsrc%2Fcom%2Fgoogle%2Fzxing%2Fcommon%2FAbstractBlackBoxTestCase.java;h=b806080e12e6b99fd4b63bbfc375ffc03ae57e43;hp=9e3ccde915c1e1488629771a143ab9f20c17a218;hb=e5d03a79a458340b69a57914a352f0a8841cdf69;hpb=7e271e44cbc65241a9dc73e47d6acf6f7e5ef150 diff --git a/core/test/src/com/google/zxing/common/AbstractBlackBoxTestCase.java b/core/test/src/com/google/zxing/common/AbstractBlackBoxTestCase.java index 9e3ccde9..b806080e 100644 --- a/core/test/src/com/google/zxing/common/AbstractBlackBoxTestCase.java +++ b/core/test/src/com/google/zxing/common/AbstractBlackBoxTestCase.java @@ -17,16 +17,20 @@ package com.google.zxing.common; import com.google.zxing.BarcodeFormat; +import com.google.zxing.BinaryBitmap; import com.google.zxing.DecodeHintType; -import com.google.zxing.MonochromeBitmapSource; +import com.google.zxing.LuminanceSource; import com.google.zxing.Reader; import com.google.zxing.ReaderException; import com.google.zxing.Result; -import com.google.zxing.client.j2se.BufferedImageMonochromeBitmapSource; -import junit.framework.TestCase; +import com.google.zxing.ResultMetadataType; +import com.google.zxing.client.j2se.BufferedImageLuminanceSource; +import org.junit.Assert; +import org.junit.Test; import javax.imageio.ImageIO; import java.awt.geom.AffineTransform; +import java.awt.geom.Rectangle2D; import java.awt.image.AffineTransformOp; import java.awt.image.BufferedImage; import java.awt.image.BufferedImageOp; @@ -35,16 +39,18 @@ import java.io.FileInputStream; import java.io.FilenameFilter; import java.io.IOException; import java.io.InputStreamReader; +import java.nio.charset.Charset; +import java.util.ArrayList; import java.util.Hashtable; import java.util.List; -import java.util.ArrayList; -import java.nio.charset.Charset; +import java.util.Map; +import java.util.Properties; /** * @author Sean Owen * @author dswitkin@google.com (Daniel Switkin) */ -public abstract class AbstractBlackBoxTestCase extends TestCase { +public abstract class AbstractBlackBoxTestCase extends Assert { protected static final Hashtable TRY_HARDER_HINT; static { @@ -60,6 +66,35 @@ public abstract class AbstractBlackBoxTestCase extends TestCase { } }; + public static class SummaryResults { + private int totalFound; + private int totalMustPass; + private int totalTests; + + public SummaryResults() { + totalFound = 0; + totalMustPass = 0; + totalTests = 0; + } + + public SummaryResults(int found, int mustPass, int total) { + totalFound = found; + totalMustPass = mustPass; + totalTests = total; + } + + public void add(SummaryResults other) { + totalFound += other.totalFound; + totalMustPass += other.totalMustPass; + totalTests += other.totalTests; + } + + public String toString() { + return "\nSUMMARY RESULTS:\n Decoded " + totalFound + " images out of " + totalTests + + " (" + (totalFound * 100 / totalTests) + "%, " + totalMustPass + " required)"; + } + } + private static class TestResult { private final int mustPassCount; private final int tryHarderCount; @@ -86,9 +121,15 @@ public abstract class AbstractBlackBoxTestCase extends TestCase { private final BarcodeFormat expectedFormat; private final List testResults; - protected AbstractBlackBoxTestCase(File testBase, + protected AbstractBlackBoxTestCase(String testBasePathSuffix, Reader barcodeReader, BarcodeFormat expectedFormat) { + // A little workaround to prevent aggravation in my IDE + File testBase = new File(testBasePathSuffix); + if (!testBase.exists()) { + // try starting with 'core' since the test base is often given as the project root + testBase = new File("core/" + testBasePathSuffix); + } this.testBase = testBase; this.barcodeReader = barcodeReader; this.expectedFormat = expectedFormat; @@ -119,7 +160,14 @@ public abstract class AbstractBlackBoxTestCase extends TestCase { return null; } + // This workaround is used because AbstractNegativeBlackBoxTestCase overrides this method but does + // not return SummaryResults. + @Test public void testBlackBox() throws IOException { + testBlackBoxCountingResults(true); + } + + public SummaryResults testBlackBoxCountingResults(boolean assertOnFailure) throws IOException { assertFalse(testResults.isEmpty()); File[] imageFiles = getImageFiles(); @@ -132,24 +180,33 @@ public abstract class AbstractBlackBoxTestCase extends TestCase { BufferedImage image = ImageIO.read(testImage); String testImageFileName = testImage.getName(); - File expectedTextFile = new File(testBase, - testImageFileName.substring(0, testImageFileName.indexOf('.')) + ".txt"); + String fileBaseName = testImageFileName.substring(0, testImageFileName.indexOf('.')); + File expectedTextFile = new File(testBase, fileBaseName + ".txt"); String expectedText = readFileAsString(expectedTextFile); + File expectedMetadataFile = new File(testBase, fileBaseName + ".metadata.txt"); + Properties expectedMetadata = new Properties(); + if (expectedMetadataFile.exists()) { + expectedMetadata.load(new FileInputStream(expectedMetadataFile)); + } + for (int x = 0; x < testCount; x++) { float rotation = testResults.get(x).getRotation(); BufferedImage rotatedImage = rotateImage(image, rotation); - MonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(rotatedImage); - if (decode(source, rotation, expectedText, false)) { + LuminanceSource source = new BufferedImageLuminanceSource(rotatedImage); + BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); + if (decode(bitmap, rotation, expectedText, expectedMetadata, false)) { passedCounts[x]++; } - if (decode(source, rotation, expectedText, true)) { + if (decode(bitmap, rotation, expectedText, expectedMetadata, true)) { tryHarderCounts[x]++; } } } // Print the results of all tests first + int totalFound = 0; + int totalMustPass = 0; for (int x = 0; x < testCount; x++) { System.out.println("Rotation " + testResults.get(x).getRotation() + " degrees:"); System.out.println(" " + passedCounts[x] + " of " + imageFiles.length + " images passed (" @@ -157,20 +214,39 @@ public abstract class AbstractBlackBoxTestCase extends TestCase { System.out.println(" " + tryHarderCounts[x] + " of " + imageFiles.length + " images passed with try harder (" + testResults.get(x).getTryHarderCount() + " required)"); + totalFound += passedCounts[x]; + totalFound += tryHarderCounts[x]; + totalMustPass += testResults.get(x).getMustPassCount(); + totalMustPass += testResults.get(x).getTryHarderCount(); + } + + int totalTests = imageFiles.length * testCount * 2; + System.out.println("TOTALS:\n Decoded " + totalFound + " images out of " + totalTests + + " (" + (totalFound * 100 / totalTests) + "%, " + totalMustPass + " required)"); + if (totalFound > totalMustPass) { + System.out.println(" *** Test too lax by " + (totalFound - totalMustPass) + " images"); + } else if (totalFound < totalMustPass) { + System.out.println(" *** Test failed by " + (totalMustPass - totalFound) + " images"); } // Then run through again and assert if any failed - for (int x = 0; x < testCount; x++) { - assertTrue("Rotation " + testResults.get(x).getRotation() + - " degrees: Too many images failed", - passedCounts[x] >= testResults.get(x).getMustPassCount()); - assertTrue("Try harder, Rotation " + testResults.get(x).getRotation() + - " degrees: Too many images failed", - tryHarderCounts[x] >= testResults.get(x).getTryHarderCount()); + if (assertOnFailure) { + for (int x = 0; x < testCount; x++) { + assertTrue("Rotation " + testResults.get(x).getRotation() + + " degrees: Too many images failed", + passedCounts[x] >= testResults.get(x).getMustPassCount()); + assertTrue("Try harder, Rotation " + testResults.get(x).getRotation() + + " degrees: Too many images failed", + tryHarderCounts[x] >= testResults.get(x).getTryHarderCount()); + } } + return new SummaryResults(totalFound, totalMustPass, totalTests); } - private boolean decode(MonochromeBitmapSource source, float rotation, String expectedText, + private boolean decode(BinaryBitmap source, + float rotation, + String expectedText, + Properties expectedMetadata, boolean tryHarder) { Result result; String suffix = " (" + (tryHarder ? "try harder, " : "") + "rotation: " + rotation + ')'; @@ -184,7 +260,7 @@ public abstract class AbstractBlackBoxTestCase extends TestCase { hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE); } } - result = getReader().decode(source, hints); + result = barcodeReader.decode(source, hints); } catch (ReaderException re) { System.out.println(re + suffix); return false; @@ -192,16 +268,29 @@ public abstract class AbstractBlackBoxTestCase extends TestCase { if (!expectedFormat.equals(result.getBarcodeFormat())) { System.out.println("Format mismatch: expected '" + expectedFormat + "' but got '" + - result.getBarcodeFormat() + "'" + suffix); + result.getBarcodeFormat() + '\'' + suffix); return false; } String resultText = result.getText(); if (!expectedText.equals(resultText)) { System.out.println("Mismatch: expected '" + expectedText + "' but got '" + resultText + - "'" + suffix); + '\'' + suffix); return false; } + + Hashtable resultMetadata = result.getResultMetadata(); + for (Map.Entry metadatum : expectedMetadata.entrySet()) { + ResultMetadataType key = ResultMetadataType.valueOf(metadatum.getKey().toString()); + Object expectedValue = metadatum.getValue(); + Object actualValue = resultMetadata == null ? null : resultMetadata.get(key); + if (!expectedValue.equals(actualValue)) { + System.out.println("Metadata mismatch: for key '" + key + "' expected '" + expectedValue + + "' but got '" + actualValue + '\''); + return false; + } + } + return true; } @@ -224,11 +313,27 @@ public abstract class AbstractBlackBoxTestCase extends TestCase { if (degrees == 0.0f) { return original; } else { + double radians = Math.toRadians(degrees); + + // Transform simply to find out the new bounding box (don't actually run the image through it) AffineTransform at = new AffineTransform(); - at.rotate(Math.toRadians(degrees), original.getWidth() / 2.0f, original.getHeight() / 2.0f); + at.rotate(radians, original.getWidth() / 2.0, original.getHeight() / 2.0); BufferedImageOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BICUBIC); + + Rectangle2D r = op.getBounds2D(original); + int width = (int) Math.ceil(r.getWidth()); + int height = (int) Math.ceil(r.getHeight()); + + // Real transform, now that we know the size of the new image and how to translate after we rotate + // to keep it centered + at = new AffineTransform(); + at.rotate(radians, width / 2.0, height / 2.0); + at.translate(((width - original.getWidth()) / 2.0), + ((height - original.getHeight()) / 2.0)); + op = new AffineTransformOp(at, AffineTransformOp.TYPE_BICUBIC); + return op.filter(original, null); } } -} \ No newline at end of file +}