2 * Copyright 2008 ZXing authors
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.google.zxing.qrcode;
19 import com.google.zxing.BarcodeFormat;
20 import com.google.zxing.EncodeHintType;
21 import com.google.zxing.WriterException;
22 import com.google.zxing.common.ByteMatrix;
23 import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
24 import junit.framework.TestCase;
26 import javax.imageio.ImageIO;
27 import java.awt.image.BufferedImage;
29 import java.io.IOException;
30 import java.util.Arrays;
31 import java.util.Hashtable;
34 * @author satorux@google.com (Satoru Takabayashi) - creator
35 * @author dswitkin@google.com (Daniel Switkin) - ported and expanded from C++
37 public final class QRCodeWriterTestCase extends TestCase {
39 private static final String BASE_IMAGE_PATH = "test/data/golden/qrcode/";
41 private static BufferedImage loadImage(String fileName) {
43 File file = new File(BASE_IMAGE_PATH + fileName);
45 // try starting with 'core' since the test base is often given as the project root
46 file = new File("core/" + BASE_IMAGE_PATH + fileName);
48 assertTrue("Please run from the 'core' directory", file.exists());
49 return ImageIO.read(file);
50 } catch (IOException e) {
55 // In case the golden images are not monochromatic, convert the RGB values to greyscale.
56 private static ByteMatrix createMatrixFromImage(BufferedImage image) {
57 int width = image.getWidth();
58 int height = image.getHeight();
59 int[] pixels = new int[width * height];
60 image.getRGB(0, 0, width, height, pixels, 0, width);
62 ByteMatrix matrix = new ByteMatrix(height, width);
63 for (int y = 0; y < height; y++) {
64 for (int x = 0; x < width; x++) {
65 int pixel = pixels[y * width + x];
66 int luminance = (306 * ((pixel >> 16) & 0xFF) +
67 601 * ((pixel >> 8) & 0xFF) +
68 117 * (pixel & 0xFF)) >> 10;
69 matrix.set(y, x, luminance);
75 public void testQRCodeWriter() throws WriterException {
76 // The QR should be multiplied up to fit, with extra padding if necessary
78 QRCodeWriter writer = new QRCodeWriter();
79 ByteMatrix matrix = writer.encode("http://www.google.com/", BarcodeFormat.QR_CODE, bigEnough,
81 assertNotNull(matrix);
82 assertEquals(bigEnough, matrix.width());
83 assertEquals(bigEnough, matrix.height());
85 // The QR will not fit in this size, so the matrix should come back bigger
87 matrix = writer.encode("http://www.google.com/", BarcodeFormat.QR_CODE, tooSmall,
89 assertNotNull(matrix);
90 assertTrue(tooSmall < matrix.width());
91 assertTrue(tooSmall < matrix.height());
93 // We should also be able to handle non-square requests by padding them
94 int strangeWidth = 500;
95 int strangeHeight = 100;
96 matrix = writer.encode("http://www.google.com/", BarcodeFormat.QR_CODE, strangeWidth,
98 assertNotNull(matrix);
99 assertEquals(strangeWidth, matrix.width());
100 assertEquals(strangeHeight, matrix.height());
103 private static void compareToGoldenFile(String contents, ErrorCorrectionLevel ecLevel,
104 int resolution, String fileName) throws WriterException {
106 BufferedImage image = loadImage(fileName);
107 assertNotNull(image);
108 ByteMatrix goldenResult = createMatrixFromImage(image);
109 assertNotNull(goldenResult);
111 QRCodeWriter writer = new QRCodeWriter();
112 Hashtable<EncodeHintType,Object> hints = new Hashtable<EncodeHintType,Object>();
113 hints.put(EncodeHintType.ERROR_CORRECTION, ecLevel);
114 ByteMatrix generatedResult = writer.encode(contents, BarcodeFormat.QR_CODE, resolution,
117 assertEquals("Width should be " + resolution + ", but was " + generatedResult.width(),
118 resolution, generatedResult.width());
119 assertEquals("Height should be " + resolution + ", but was " + generatedResult.height(),
120 resolution, generatedResult.height());
121 assertTrue("Expected " + goldenResult.toString() + " but got " + generatedResult.toString(),
122 Arrays.deepEquals(goldenResult.getArray(), generatedResult.getArray()));
125 // Golden images are generated with "qrcode_sample.cc". The images are checked with both eye balls
126 // and cell phones. We expect pixel-perfect results, because the error correction level is known,
127 // and the pixel dimensions matches exactly.
128 public void testRegressionTest() throws WriterException {
129 compareToGoldenFile("http://www.google.com/", ErrorCorrectionLevel.M, 99,
130 "renderer-test-01.png");
132 compareToGoldenFile("12345", ErrorCorrectionLevel.L, 58, "renderer-test-02.png");
134 // Test in Katakana in Shift_JIS.
135 // TODO: this test is bogus now that byte mode has been basically fixed to assuming ISO-8859-1 encoding
136 // The real solution is to implement Kanji mode, in which case the golden file will be wrong again
139 new String(new byte[] {(byte)0x83, 0x65, (byte)0x83, 0x58, (byte)0x83, 0x67}, "Shift_JIS"),
140 ErrorCorrectionLevel.H, 145,
141 "renderer-test-03.png");