import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
-import com.google.zxing.common.ByteMatrix;
+import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import junit.framework.TestCase;
private static BufferedImage loadImage(String fileName) {
try {
File file = new File(BASE_IMAGE_PATH + fileName);
+ if (!file.exists()) {
+ // try starting with 'core' since the test base is often given as the project root
+ file = new File("core/" + BASE_IMAGE_PATH + fileName);
+ }
assertTrue("Please run from the 'core' directory", file.exists());
return ImageIO.read(file);
} catch (IOException e) {
}
// In case the golden images are not monochromatic, convert the RGB values to greyscale.
- private static ByteMatrix createMatrixFromImage(BufferedImage image) {
+ private static BitMatrix createMatrixFromImage(BufferedImage image) {
int width = image.getWidth();
int height = image.getHeight();
int[] pixels = new int[width * height];
image.getRGB(0, 0, width, height, pixels, 0, width);
- ByteMatrix matrix = new ByteMatrix(height, width);
+ BitMatrix matrix = new BitMatrix(width, height);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int pixel = pixels[y * width + x];
int luminance = (306 * ((pixel >> 16) & 0xFF) +
601 * ((pixel >> 8) & 0xFF) +
117 * (pixel & 0xFF)) >> 10;
- matrix.set(y, x, luminance);
+ if (luminance <= 0x7F) {
+ matrix.set(x, y);
+ }
}
}
return matrix;
// The QR should be multiplied up to fit, with extra padding if necessary
int bigEnough = 256;
QRCodeWriter writer = new QRCodeWriter();
- ByteMatrix matrix = writer.encode("http://www.google.com/", BarcodeFormat.QR_CODE, bigEnough,
+ BitMatrix matrix = writer.encode("http://www.google.com/", BarcodeFormat.QR_CODE, bigEnough,
bigEnough, null);
assertNotNull(matrix);
- assertEquals(bigEnough, matrix.width());
- assertEquals(bigEnough, matrix.height());
+ assertEquals(bigEnough, matrix.getWidth());
+ assertEquals(bigEnough, matrix.getHeight());
// The QR will not fit in this size, so the matrix should come back bigger
int tooSmall = 20;
matrix = writer.encode("http://www.google.com/", BarcodeFormat.QR_CODE, tooSmall,
tooSmall, null);
assertNotNull(matrix);
- assertTrue(tooSmall < matrix.width());
- assertTrue(tooSmall < matrix.height());
+ assertTrue(tooSmall < matrix.getWidth());
+ assertTrue(tooSmall < matrix.getHeight());
// We should also be able to handle non-square requests by padding them
int strangeWidth = 500;
matrix = writer.encode("http://www.google.com/", BarcodeFormat.QR_CODE, strangeWidth,
strangeHeight, null);
assertNotNull(matrix);
- assertEquals(strangeWidth, matrix.width());
- assertEquals(strangeHeight, matrix.height());
+ assertEquals(strangeWidth, matrix.getWidth());
+ assertEquals(strangeHeight, matrix.getHeight());
}
private static void compareToGoldenFile(String contents, ErrorCorrectionLevel ecLevel,
BufferedImage image = loadImage(fileName);
assertNotNull(image);
- ByteMatrix goldenResult = createMatrixFromImage(image);
+ BitMatrix goldenResult = createMatrixFromImage(image);
assertNotNull(goldenResult);
QRCodeWriter writer = new QRCodeWriter();
- Hashtable hints = new Hashtable();
+ Hashtable<EncodeHintType,Object> hints = new Hashtable<EncodeHintType,Object>();
hints.put(EncodeHintType.ERROR_CORRECTION, ecLevel);
- ByteMatrix generatedResult = writer.encode(contents, BarcodeFormat.QR_CODE, resolution,
+ BitMatrix generatedResult = writer.encode(contents, BarcodeFormat.QR_CODE, resolution,
resolution, hints);
- assertEquals("Width should be " + resolution + ", but was " + generatedResult.width(),
- resolution, generatedResult.width());
- assertEquals("Height should be " + resolution + ", but was " + generatedResult.height(),
- resolution, generatedResult.height());
- assertTrue("Expected " + goldenResult.toString() + " but got " + generatedResult.toString(),
- Arrays.deepEquals(goldenResult.getArray(), generatedResult.getArray()));
+ assertEquals(resolution, generatedResult.getWidth());
+ assertEquals(resolution, generatedResult.getHeight());
+ assertEquals(goldenResult, generatedResult);
}
// Golden images are generated with "qrcode_sample.cc". The images are checked with both eye balls
// and cell phones. We expect pixel-perfect results, because the error correction level is known,
// and the pixel dimensions matches exactly.
- public void testRegressionTest() throws WriterException, IOException {
+ public void testRegressionTest() throws WriterException {
compareToGoldenFile("http://www.google.com/", ErrorCorrectionLevel.M, 99,
"renderer-test-01.png");