Small style stuff
[zxing.git] / core / src / com / google / zxing / qrcode / QRCodeWriter.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.Writer;
22 import com.google.zxing.WriterException;
23 import com.google.zxing.common.BitMatrix;
24 import com.google.zxing.qrcode.encoder.ByteMatrix;
25 import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
26 import com.google.zxing.qrcode.encoder.Encoder;
27 import com.google.zxing.qrcode.encoder.QRCode;
28
29 import java.util.Hashtable;
30
31 /**
32  * This object renders a QR Code as a BitMatrix 2D array of greyscale values.
33  *
34  * @author dswitkin@google.com (Daniel Switkin)
35  */
36 public final class QRCodeWriter implements Writer {
37
38   private static final int QUIET_ZONE_SIZE = 4;
39
40   public BitMatrix encode(String contents, BarcodeFormat format, int width, int height)
41       throws WriterException {
42
43     return encode(contents, format, width, height, null);
44   }
45
46   public BitMatrix encode(String contents, BarcodeFormat format, int width, int height,
47       Hashtable hints) throws WriterException {
48
49     if (contents == null || contents.length() == 0) {
50       throw new IllegalArgumentException("Found empty contents");
51     }
52
53     if (format != BarcodeFormat.QR_CODE) {
54       throw new IllegalArgumentException("Can only encode QR_CODE, but got " + format);
55     }
56
57     if (width < 0 || height < 0) {
58       throw new IllegalArgumentException("Requested dimensions are too small: " + width + 'x' +
59           height);
60     }
61
62     ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L;
63     if (hints != null) {
64       ErrorCorrectionLevel requestedECLevel = (ErrorCorrectionLevel) hints.get(EncodeHintType.ERROR_CORRECTION);
65       if (requestedECLevel != null) {
66         errorCorrectionLevel = requestedECLevel;
67       }
68     }
69
70     QRCode code = new QRCode();
71     Encoder.encode(contents, errorCorrectionLevel, hints, code);
72     return renderResult(code, width, height);
73   }
74
75   // Note that the input matrix uses 0 == white, 1 == black, while the output matrix uses
76   // 0 == black, 255 == white (i.e. an 8 bit greyscale bitmap).
77   private static BitMatrix renderResult(QRCode code, int width, int height) {
78     ByteMatrix input = code.getMatrix();
79     int inputWidth = input.getWidth();
80     int inputHeight = input.getHeight();
81     int qrWidth = inputWidth + (QUIET_ZONE_SIZE << 1);
82     int qrHeight = inputHeight + (QUIET_ZONE_SIZE << 1);
83     int outputWidth = Math.max(width, qrWidth);
84     int outputHeight = Math.max(height, qrHeight);
85
86     int multiple = Math.min(outputWidth / qrWidth, outputHeight / qrHeight);
87     // Padding includes both the quiet zone and the extra white pixels to accommodate the requested
88     // dimensions. For example, if input is 25x25 the QR will be 33x33 including the quiet zone.
89     // If the requested size is 200x160, the multiple will be 4, for a QR of 132x132. These will
90     // handle all the padding from 100x100 (the actual QR) up to 200x160.
91     int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;
92     int topPadding = (outputHeight - (inputHeight * multiple)) / 2;
93
94     BitMatrix output = new BitMatrix(outputWidth, outputHeight);
95
96     for (int inputY = 0, outputY = topPadding; inputY < inputHeight; inputY++, outputY += multiple) {
97       // Write the contents of this row of the barcode
98       for (int inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple) {
99         if (input.get(inputX, inputY) == 1) {
100           output.setRegion(outputX, outputY, multiple, multiple);
101         }
102       }
103     }
104
105     return output;
106   }
107
108 }