Minor style tweaks

git-svn-id: https://zxing.googlecode.com/svn/trunk@428 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2008-06-13 12:15:49 +00:00
parent 6f7a348f5d
commit 70c64558cf

View file

@ -29,6 +29,7 @@ import javax.imageio.ImageIO;
import java.awt.geom.AffineTransform; import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp; import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FilenameFilter; import java.io.FilenameFilter;
@ -44,9 +45,9 @@ import java.util.Vector;
*/ */
public abstract class AbstractBlackBoxTestCase extends TestCase { public abstract class AbstractBlackBoxTestCase extends TestCase {
private static final Hashtable TRY_HARDER_HINT; private static final Hashtable<DecodeHintType, Object> TRY_HARDER_HINT;
static { static {
TRY_HARDER_HINT = new Hashtable(); TRY_HARDER_HINT = new Hashtable<DecodeHintType, Object>();
TRY_HARDER_HINT.put(DecodeHintType.TRY_HARDER, Boolean.TRUE); TRY_HARDER_HINT.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
} }
@ -54,19 +55,24 @@ public abstract class AbstractBlackBoxTestCase extends TestCase {
public boolean accept(File dir, String name) { public boolean accept(File dir, String name) {
String lowerCase = name.toLowerCase(); String lowerCase = name.toLowerCase();
return lowerCase.endsWith(".jpg") || lowerCase.endsWith(".jpeg") || return lowerCase.endsWith(".jpg") || lowerCase.endsWith(".jpeg") ||
lowerCase.endsWith(".gif") || lowerCase.endsWith(".png") || lowerCase.endsWith(".gif") || lowerCase.endsWith(".png") ||
lowerCase.endsWith(".url"); lowerCase.endsWith(".url");
} }
}; };
private class TestResult { private static class TestResult {
public int mustPassCount; private final int mustPassCount;
public float rotation; private final float rotation;
TestResult(int mustPassCount, float rotation) { TestResult(int mustPassCount, float rotation) {
this.mustPassCount = mustPassCount; this.mustPassCount = mustPassCount;
this.rotation = rotation; this.rotation = rotation;
} }
public int getMustPassCount() {
return mustPassCount;
}
public float getRotation() {
return rotation;
}
} }
private final File testBase; private final File testBase;
@ -94,7 +100,7 @@ public abstract class AbstractBlackBoxTestCase extends TestCase {
} }
public void testBlackBox() throws IOException { public void testBlackBox() throws IOException {
assertTrue(testResults.size() > 0); assertFalse(testResults.isEmpty());
assertTrue("Please run from the 'core' directory", testBase.exists()); assertTrue("Please run from the 'core' directory", testBase.exists());
File[] imageFiles = testBase.listFiles(IMAGE_NAME_FILTER); File[] imageFiles = testBase.listFiles(IMAGE_NAME_FILTER);
@ -116,22 +122,22 @@ public abstract class AbstractBlackBoxTestCase extends TestCase {
String expectedText = readFileAsString(expectedTextFile); String expectedText = readFileAsString(expectedTextFile);
for (int x = 0; x < testResults.size(); x++) { for (int x = 0; x < testResults.size(); x++) {
if (testOneImage(image, testResults.get(x).rotation, expectedText)) { if (doTestOneImage(image, testResults.get(x).getRotation(), expectedText)) {
passedCounts[x]++; passedCounts[x]++;
} }
} }
} }
for (int x = 0; x < testResults.size(); x++) { for (int x = 0; x < testResults.size(); x++) {
System.out.println("Rotation " + testResults.get(x).rotation + " degrees: " + passedCounts[x] + System.out.println("Rotation " + testResults.get(x).getRotation() + " degrees: " + passedCounts[x] +
" of " + imageFiles.length + " images passed (" + testResults.get(x).mustPassCount + " of " + imageFiles.length + " images passed (" + testResults.get(x).getMustPassCount() +
" required)"); " required)");
assertTrue("Rotation " + testResults.get(x).rotation + " degrees: Too many images failed", assertTrue("Rotation " + testResults.get(x).getRotation() + " degrees: Too many images failed",
passedCounts[x] >= testResults.get(x).mustPassCount); passedCounts[x] >= testResults.get(x).getMustPassCount());
} }
} }
private boolean testOneImage(BufferedImage image, float rotationInDegrees, String expectedText) { private boolean doTestOneImage(BufferedImage image, float rotationInDegrees, String expectedText) {
BufferedImage rotatedImage = rotateImage(image, rotationInDegrees); BufferedImage rotatedImage = rotateImage(image, rotationInDegrees);
MonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(rotatedImage); MonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(rotatedImage);
Result result; Result result;
@ -142,16 +148,16 @@ public abstract class AbstractBlackBoxTestCase extends TestCase {
return false; return false;
} }
if (expectedFormat != result.getBarcodeFormat()) { if (!expectedFormat.equals(result.getBarcodeFormat())) {
System.out.println("Format mismatch: expected '" + expectedFormat + "' but got '" + System.out.println("Format mismatch: expected '" + expectedFormat + "' but got '" +
result.getBarcodeFormat() + "' (rotation: " + rotationInDegrees + ")"); result.getBarcodeFormat() + "' (rotation: " + rotationInDegrees + ')');
return false; return false;
} }
String resultText = result.getText(); String resultText = result.getText();
if (!expectedText.equals(resultText)) { if (!expectedText.equals(resultText)) {
System.out.println("Mismatch: expected '" + expectedText + "' but got '" + resultText + System.out.println("Mismatch: expected '" + expectedText + "' but got '" + resultText +
"' (rotation: " + rotationInDegrees + ")"); "' (rotation: " + rotationInDegrees + ')');
return false; return false;
} }
@ -162,18 +168,18 @@ public abstract class AbstractBlackBoxTestCase extends TestCase {
fail("Normal mode succeeded but \"try harder\" failed"); fail("Normal mode succeeded but \"try harder\" failed");
return false; return false;
} }
if (expectedFormat != result.getBarcodeFormat()) { if (!expectedFormat.equals(result.getBarcodeFormat())) {
System.out.println("Try Harder Format mismatch: expected '" + expectedFormat + "' but got '" + System.out.println("Try Harder Format mismatch: expected '" + expectedFormat + "' but got '" +
result.getBarcodeFormat() + "' (rotation: " + rotationInDegrees + ")"); result.getBarcodeFormat() + "' (rotation: " + rotationInDegrees + ')');
} else if (!expectedText.equals(resultText)) { } else if (!expectedText.equals(resultText)) {
System.out.println("Try Harder Mismatch: expected '" + expectedText + "' but got '" + System.out.println("Try Harder Mismatch: expected '" + expectedText + "' but got '" +
resultText + "' (rotation: " + rotationInDegrees + ")"); resultText + "' (rotation: " + rotationInDegrees + ')');
} }
return true; return true;
} }
private static String readFileAsString(File file) throws IOException { private static String readFileAsString(File file) throws IOException {
StringBuilder result = new StringBuilder(); StringBuilder result = new StringBuilder((int) file.length());
InputStreamReader reader = new InputStreamReader(new FileInputStream(file), "UTF-8"); InputStreamReader reader = new InputStreamReader(new FileInputStream(file), "UTF-8");
try { try {
char[] buffer = new char[256]; char[] buffer = new char[256];
@ -188,13 +194,13 @@ public abstract class AbstractBlackBoxTestCase extends TestCase {
} }
private static BufferedImage rotateImage(BufferedImage original, float degrees) { private static BufferedImage rotateImage(BufferedImage original, float degrees) {
if (degrees != 0.0f) { if (degrees == 0.0f) {
AffineTransform at = new AffineTransform();
at.rotate(Math.toRadians(degrees), original.getWidth() / 2, original.getHeight() / 2);
AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BICUBIC);
return op.filter(original, null);
} else {
return original; return original;
} else {
AffineTransform at = new AffineTransform();
at.rotate(Math.toRadians(degrees), original.getWidth() / 2.0f, original.getHeight() / 2.0f);
BufferedImageOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BICUBIC);
return op.filter(original, null);
} }
} }