Another attack on integrating encoder and decoder: Version is done. Attempted to...
[zxing.git] / core / test / src / com / google / zxing / qrcode / QRCodeWriterTestCase.java
1 /*
2  * Copyright 2008 ZXing authors
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.google.zxing.qrcode;
18
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;
25
26 import javax.imageio.ImageIO;
27 import java.awt.image.BufferedImage;
28 import java.io.File;
29 import java.io.IOException;
30 import java.util.Arrays;
31 import java.util.Hashtable;
32
33 /**
34  * @author satorux@google.com (Satoru Takabayashi) - creator
35  * @author dswitkin@google.com (Daniel Switkin) - ported and expanded from C++
36  */
37 public final class QRCodeWriterTestCase extends TestCase {
38
39   private static final String BASE_IMAGE_PATH = "test/data/golden/qrcode/";
40
41   private static BufferedImage loadImage(String fileName) {
42     try {
43       File file = new File(BASE_IMAGE_PATH + fileName);
44       assertTrue("Please run from the 'core' directory", file.exists());
45       return ImageIO.read(file);
46     } catch (IOException e) {
47       return null;
48     }
49   }
50
51   // In case the golden images are not monochromatic, convert the RGB values to greyscale.
52   private static ByteMatrix createMatrixFromImage(BufferedImage image) {
53     int width = image.getWidth();
54     int height = image.getHeight();
55     int[] pixels = new int[width * height];
56     image.getRGB(0, 0, width, height, pixels, 0, width);
57
58     ByteMatrix matrix = new ByteMatrix(height, width);
59     for (int y = 0; y < height; y++) {
60       for (int x = 0; x < width; x++) {
61         int pixel = pixels[y * width + x];
62         int luminance = (306 * ((pixel >> 16) & 0xFF) +
63             601 * ((pixel >> 8) & 0xFF) +
64             117 * (pixel & 0xFF)) >> 10;
65         matrix.set(y, x, luminance);
66       }
67     }
68     return matrix;
69   }
70
71   public void testQRCodeWriter() throws WriterException {
72     // The QR should be multiplied up to fit, with extra padding if necessary
73     int bigEnough = 256;
74     QRCodeWriter writer = new QRCodeWriter();
75     ByteMatrix matrix = writer.encode("http://www.google.com/", BarcodeFormat.QR_CODE, bigEnough,
76         bigEnough, null);
77     assertNotNull(matrix);
78     assertEquals(bigEnough, matrix.width());
79     assertEquals(bigEnough, matrix.height());
80
81     // The QR will not fit in this size, so the matrix should come back bigger
82     int tooSmall = 20;
83     matrix = writer.encode("http://www.google.com/", BarcodeFormat.QR_CODE, tooSmall,
84         tooSmall, null);
85     assertNotNull(matrix);
86     assertTrue(tooSmall < matrix.width());
87     assertTrue(tooSmall < matrix.height());
88
89     // We should also be able to handle non-square requests by padding them
90     int strangeWidth = 500;
91     int strangeHeight = 100;
92     matrix = writer.encode("http://www.google.com/", BarcodeFormat.QR_CODE, strangeWidth,
93         strangeHeight, null);
94     assertNotNull(matrix);
95     assertEquals(strangeWidth, matrix.width());
96     assertEquals(strangeHeight, matrix.height());
97   }
98
99   private static void compareToGoldenFile(String contents, ErrorCorrectionLevel ecLevel,
100       int resolution, String fileName) throws WriterException {
101
102     BufferedImage image = loadImage(fileName);
103     assertNotNull(image);
104     ByteMatrix goldenResult = createMatrixFromImage(image);
105     assertNotNull(goldenResult);
106
107     QRCodeWriter writer = new QRCodeWriter();
108     Hashtable hints = new Hashtable();
109     hints.put(EncodeHintType.ERROR_CORRECTION, ecLevel);
110     ByteMatrix generatedResult = writer.encode(contents, BarcodeFormat.QR_CODE, resolution,
111         resolution, hints);
112
113     assertEquals("Width should be " + resolution + ", but was " + generatedResult.width(),
114         resolution, generatedResult.width());
115     assertEquals("Height should be " + resolution + ", but was " + generatedResult.height(),
116         resolution, generatedResult.height());
117     assertTrue("Expected " + goldenResult.toString() + " but got " + generatedResult.toString(),
118         Arrays.deepEquals(goldenResult.getArray(), generatedResult.getArray()));
119   }
120
121   // Golden images are generated with "qrcode_sample.cc". The images are checked with both eye balls
122   // and cell phones. We expect pixel-perfect results, because the error correction level is known,
123   // and the pixel dimensions matches exactly.
124   public void testRegressionTest() throws WriterException, IOException {
125     compareToGoldenFile("http://www.google.com/", ErrorCorrectionLevel.M, 99,
126         "renderer-test-01.png");
127
128     compareToGoldenFile("12345", ErrorCorrectionLevel.L, 58, "renderer-test-02.png");
129
130     // Test in Katakana in Shift_JIS.
131     // TODO: this test is bogus now that byte mode has been basically fixed to assuming ISO-8859-1 encoding
132     //  The real solution is to implement Kanji mode, in which case the golden file will be wrong again
133     /*
134     compareToGoldenFile(
135         new String(new byte[] {(byte)0x83, 0x65, (byte)0x83, 0x58, (byte)0x83, 0x67}, "Shift_JIS"),
136         ErrorCorrectionLevel.H, 145,
137         "renderer-test-03.png");
138      */
139   }
140
141 }