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.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;
26 import java.util.Hashtable;
28 public final class QRCodeWriter implements Writer {
30 public ByteMatrix encode(byte[] contents, BarcodeFormat format, int width, int height) {
32 return encode(contents, format, width, height, null);
35 public ByteMatrix encode(byte[] contents, BarcodeFormat format, int width, int height, Hashtable hints) {
37 if (contents == null || contents.length == 0) {
38 throw new IllegalArgumentException("Found empty contents");
41 if (format != BarcodeFormat.QR_CODE) {
42 throw new IllegalArgumentException("Can only encode QR_CODE, but got " + format);
45 if (width < 0 || height < 0) {
46 throw new IllegalArgumentException("Requested dimensions are too small: " + width + "x" +
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);
56 // TODO need a "WriterException" or something
57 throw new RuntimeException("Could not generate a QR Code");
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);
70 int multiple = Math.min(outputWidth / inputWidth, outputHeight / inputHeight);
71 int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;
72 int topPadding = (outputHeight - (inputHeight * multiple)) / 2;
74 ByteMatrix output = new ByteMatrix(outputHeight, outputWidth);
75 byte[][] outputArray = output.getArray();
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];
81 // 1. Write the white lines at the top
82 for (int y = 0; y < topPadding; y++) {
83 setRowColor(outputArray[y], (byte) 255);
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++) {
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;
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++) {
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);
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);
126 private static void setRowColor(byte[] row, byte value) {
127 for (int x = 0; x < row.length; x++) {