Replace IllegalStateException; still want to make it WriterException
[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.Writer;
21 import com.google.zxing.common.ByteMatrix;
22 import com.google.zxing.qrcode.encoder.ByteArray;
23 import com.google.zxing.qrcode.encoder.Encoder;
24 import com.google.zxing.qrcode.encoder.QRCode;
25
26 import java.util.Hashtable;
27
28 public final class QRCodeWriter implements Writer {
29
30   public ByteMatrix encode(byte[] contents, BarcodeFormat format, int width, int height) {
31
32     return encode(contents, format, width, height, null);
33   }
34
35   public ByteMatrix encode(byte[] contents, BarcodeFormat format, int width, int height, Hashtable hints) {
36
37     if (contents == null || contents.length == 0) {
38       throw new IllegalArgumentException("Found empty contents");
39     }
40
41     if (format != BarcodeFormat.QR_CODE) {
42       throw new IllegalArgumentException("Can only encode QR_CODE, but got " + format);
43     }
44
45     if (width < 0 || height < 0) {
46       throw new IllegalArgumentException("Requested dimensions are too small: " + width + "x" +
47           height);
48     }
49
50     // TODO: Check hints for error correction level instead of hardcoding
51     int errorCorrectionLevel = QRCode.EC_LEVEL_L;
52     QRCode code = new QRCode();
53     if (Encoder.Encode(new ByteArray(contents), errorCorrectionLevel, code)) {
54       return renderResult(code, width, height);
55     } else {
56       // TODO need a "WriterException" or something
57       throw new RuntimeException("Could not generate a QR Code");
58     }
59   }
60
61   // Note that the input matrix uses 0 == white, 1 == black, while the output matrix uses
62   // 0 == black, 255 == white (i.e. an 8 bit greyscale bitmap).
63   private ByteMatrix renderResult(QRCode code, final int width, final int height) {
64     ByteMatrix input = code.matrix();
65     int inputWidth = input.width();
66     int inputHeight = input.height();
67     int outputWidth = Math.max(width, inputWidth);
68     int outputHeight = Math.max(height, inputHeight);
69
70     int multiple = Math.min(outputWidth / inputWidth, outputHeight / inputHeight);
71     int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;
72     int topPadding = (outputHeight - (inputHeight * multiple)) / 2;
73
74     ByteMatrix output = new ByteMatrix(outputHeight, outputWidth);
75     byte[][] outputArray = output.getArray();
76
77     // We could be tricky and use the first row in each set of multiple as the temporary storage,
78     // instead of allocating this separate array.
79     byte[] row = new byte[outputWidth];
80
81     // 1. Write the white lines at the top
82     for (int y = 0; y < topPadding; y++) {
83       setRowColor(outputArray[y], (byte) 255);
84     }
85
86     // 2. Expand the QR image to the multiple
87     final byte[][] inputArray = input.getArray();
88     for (int y = 0; y < inputHeight; y++) {
89       // a. Write the white pixels at the left of each row
90       for (int x = 0; x < leftPadding; x++) {
91         row[x] = (byte) 255;
92       }
93
94       // b. Write the contents of this row of the barcode
95       int offset = leftPadding;
96       for (int x = 0; x < inputWidth; x++) {
97         byte value = (inputArray[y][x] == 1) ? 0 : (byte) 255;
98         for (int z = 0; z < multiple; z++) {
99           row[offset + z] = value;
100         }
101         offset += multiple;
102       }
103
104       // c. Write the white pixels at the right of each row
105       offset = leftPadding + (inputWidth * multiple);
106       for (int x = offset; x < outputWidth; x++) {
107         row[x] = (byte) 255;
108       }
109
110       // d. Write the completed row multiple times
111       offset = topPadding + (y * multiple);
112       for (int z = 0; z < multiple; z++) {
113         System.arraycopy(row, 0, outputArray[offset + z], 0, outputWidth);
114       }
115     }
116
117     // 3. Write the white lines at the bottom
118     int offset = topPadding + (inputHeight * multiple);
119     for (int y = offset; y < outputHeight; y++) {
120       setRowColor(outputArray[y], (byte) 255);
121     }
122
123     return output;
124   }
125
126   private static void setRowColor(byte[] row, byte value) {
127     for (int x = 0; x < row.length; x++) {
128       row[x] = value;
129     }
130   }
131
132 }