At last update to JUnit 4.x
[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.BitMatrix;
23 import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
24 import org.junit.Assert;
25 import org.junit.Test;
26
27 import javax.imageio.ImageIO;
28 import java.awt.image.BufferedImage;
29 import java.io.File;
30 import java.io.IOException;
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 Assert {
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       if (!file.exists()) {
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);
47       }
48       assertTrue("Please run from the 'core' directory", file.exists());
49       return ImageIO.read(file);
50     } catch (IOException e) {
51       return null;
52     }
53   }
54
55   // In case the golden images are not monochromatic, convert the RGB values to greyscale.
56   private static BitMatrix 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);
61
62     BitMatrix matrix = new BitMatrix(width, height);
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         if (luminance <= 0x7F) {
70           matrix.set(x, y);
71         }
72       }
73     }
74     return matrix;
75   }
76
77   @Test
78   public void testQRCodeWriter() throws WriterException {
79     // The QR should be multiplied up to fit, with extra padding if necessary
80     int bigEnough = 256;
81     QRCodeWriter writer = new QRCodeWriter();
82     BitMatrix matrix = writer.encode("http://www.google.com/", BarcodeFormat.QR_CODE, bigEnough,
83         bigEnough, null);
84     assertNotNull(matrix);
85     assertEquals(bigEnough, matrix.getWidth());
86     assertEquals(bigEnough, matrix.getHeight());
87
88     // The QR will not fit in this size, so the matrix should come back bigger
89     int tooSmall = 20;
90     matrix = writer.encode("http://www.google.com/", BarcodeFormat.QR_CODE, tooSmall,
91         tooSmall, null);
92     assertNotNull(matrix);
93     assertTrue(tooSmall < matrix.getWidth());
94     assertTrue(tooSmall < matrix.getHeight());
95
96     // We should also be able to handle non-square requests by padding them
97     int strangeWidth = 500;
98     int strangeHeight = 100;
99     matrix = writer.encode("http://www.google.com/", BarcodeFormat.QR_CODE, strangeWidth,
100         strangeHeight, null);
101     assertNotNull(matrix);
102     assertEquals(strangeWidth, matrix.getWidth());
103     assertEquals(strangeHeight, matrix.getHeight());
104   }
105
106   private static void compareToGoldenFile(String contents, ErrorCorrectionLevel ecLevel,
107       int resolution, String fileName) throws WriterException {
108
109     BufferedImage image = loadImage(fileName);
110     assertNotNull(image);
111     BitMatrix goldenResult = createMatrixFromImage(image);
112     assertNotNull(goldenResult);
113
114     QRCodeWriter writer = new QRCodeWriter();
115     Hashtable<EncodeHintType,Object> hints = new Hashtable<EncodeHintType,Object>();
116     hints.put(EncodeHintType.ERROR_CORRECTION, ecLevel);
117     BitMatrix generatedResult = writer.encode(contents, BarcodeFormat.QR_CODE, resolution,
118         resolution, hints);
119
120     assertEquals(resolution, generatedResult.getWidth());
121     assertEquals(resolution, generatedResult.getHeight());
122     assertEquals(goldenResult, generatedResult);
123   }
124
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   @Test
129   public void testRegressionTest() throws WriterException {
130     compareToGoldenFile("http://www.google.com/", ErrorCorrectionLevel.M, 99,
131         "renderer-test-01.png");
132
133     compareToGoldenFile("12345", ErrorCorrectionLevel.L, 58, "renderer-test-02.png");
134
135     // Test in Katakana in Shift_JIS.
136     // TODO: this test is bogus now that byte mode has been basically fixed to assuming ISO-8859-1 encoding
137     //  The real solution is to implement Kanji mode, in which case the golden file will be wrong again
138     /*
139     compareToGoldenFile(
140         new String(new byte[] {(byte)0x83, 0x65, (byte)0x83, 0x58, (byte)0x83, 0x67}, "Shift_JIS"),
141         ErrorCorrectionLevel.H, 145,
142         "renderer-test-03.png");
143      */
144   }
145
146 }