import com.google.zxing.Reader;
import com.google.zxing.ReaderException;
import com.google.zxing.Result;
+import com.google.zxing.ResultMetadataType;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
-
import junit.framework.TestCase;
+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;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
-
-import javax.imageio.ImageIO;
+import java.util.Map;
+import java.util.Properties;
/**
* @author Sean Owen
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);
LuminanceSource source = new BufferedImageLuminanceSource(rotatedImage);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
- if (decode(bitmap, rotation, expectedText, false)) {
+ if (decode(bitmap, rotation, expectedText, expectedMetadata, false)) {
passedCounts[x]++;
}
- if (decode(bitmap, rotation, expectedText, true)) {
+ if (decode(bitmap, rotation, expectedText, expectedMetadata, true)) {
tryHarderCounts[x]++;
}
}
return new SummaryResults(totalFound, totalMustPass, totalTests);
}
- private boolean decode(BinaryBitmap 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 + ')';
'\'' + suffix);
return false;
}
+
+ Hashtable resultMetadata = result.getResultMetadata();
+ for (Map.Entry<Object,Object> 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;
}
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.0, original.getHeight() / 2.0);
+ 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);
}
}