001/* 002 * Copyright 2008 ZXing authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.itextpdf.text.pdf.qrcode; 018 019 020import java.util.Map; 021 022/** 023 * This object renders a QR Code as a ByteMatrix 2D array of greyscale values. 024 * 025 * @author dswitkin@google.com (Daniel Switkin) 026 * @since 5.0.2 027 */ 028public final class QRCodeWriter { 029 030 private static final int QUIET_ZONE_SIZE = 4; 031 032 public ByteMatrix encode(String contents, int width, int height) 033 throws WriterException { 034 035 return encode(contents, width, height, null); 036 } 037 038 public ByteMatrix encode(String contents, int width, int height, 039 Map<EncodeHintType,Object> hints) throws WriterException { 040 041 if (contents == null || contents.length() == 0) { 042 throw new IllegalArgumentException("Found empty contents"); 043 } 044 045 if (width < 0 || height < 0) { 046 throw new IllegalArgumentException("Requested dimensions are too small: " + width + 'x' + 047 height); 048 } 049 050 ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L; 051 if (hints != null) { 052 ErrorCorrectionLevel requestedECLevel = (ErrorCorrectionLevel) hints.get(EncodeHintType.ERROR_CORRECTION); 053 if (requestedECLevel != null) { 054 errorCorrectionLevel = requestedECLevel; 055 } 056 } 057 058 QRCode code = new QRCode(); 059 Encoder.encode(contents, errorCorrectionLevel, hints, code); 060 return renderResult(code, width, height); 061 } 062 063 // Note that the input matrix uses 0 == white, 1 == black, while the output matrix uses 064 // 0 == black, 255 == white (i.e. an 8 bit greyscale bitmap). 065 private static ByteMatrix renderResult(QRCode code, int width, int height) { 066 ByteMatrix input = code.getMatrix(); 067 int inputWidth = input.getWidth(); 068 int inputHeight = input.getHeight(); 069 int qrWidth = inputWidth + (QUIET_ZONE_SIZE << 1); 070 int qrHeight = inputHeight + (QUIET_ZONE_SIZE << 1); 071 int outputWidth = Math.max(width, qrWidth); 072 int outputHeight = Math.max(height, qrHeight); 073 074 int multiple = Math.min(outputWidth / qrWidth, outputHeight / qrHeight); 075 // Padding includes both the quiet zone and the extra white pixels to accommodate the requested 076 // dimensions. For example, if input is 25x25 the QR will be 33x33 including the quiet zone. 077 // If the requested size is 200x160, the multiple will be 4, for a QR of 132x132. These will 078 // handle all the padding from 100x100 (the actual QR) up to 200x160. 079 int leftPadding = (outputWidth - (inputWidth * multiple)) / 2; 080 int topPadding = (outputHeight - (inputHeight * multiple)) / 2; 081 082 ByteMatrix output = new ByteMatrix(outputWidth, outputHeight); 083 byte[][] outputArray = output.getArray(); 084 085 // We could be tricky and use the first row in each set of multiple as the temporary storage, 086 // instead of allocating this separate array. 087 byte[] row = new byte[outputWidth]; 088 089 // 1. Write the white lines at the top 090 for (int y = 0; y < topPadding; y++) { 091 setRowColor(outputArray[y], (byte) 255); 092 } 093 094 // 2. Expand the QR image to the multiple 095 byte[][] inputArray = input.getArray(); 096 for (int y = 0; y < inputHeight; y++) { 097 // a. Write the white pixels at the left of each row 098 for (int x = 0; x < leftPadding; x++) { 099 row[x] = (byte) 255; 100 } 101 102 // b. Write the contents of this row of the barcode 103 int offset = leftPadding; 104 for (int x = 0; x < inputWidth; x++) { 105 byte value = (inputArray[y][x] == 1) ? 0 : (byte) 255; 106 for (int z = 0; z < multiple; z++) { 107 row[offset + z] = value; 108 } 109 offset += multiple; 110 } 111 112 // c. Write the white pixels at the right of each row 113 offset = leftPadding + (inputWidth * multiple); 114 for (int x = offset; x < outputWidth; x++) { 115 row[x] = (byte) 255; 116 } 117 118 // d. Write the completed row multiple times 119 offset = topPadding + (y * multiple); 120 for (int z = 0; z < multiple; z++) { 121 System.arraycopy(row, 0, outputArray[offset + z], 0, outputWidth); 122 } 123 } 124 125 // 3. Write the white lines at the bottom 126 int offset = topPadding + (inputHeight * multiple); 127 for (int y = offset; y < outputHeight; y++) { 128 setRowColor(outputArray[y], (byte) 255); 129 } 130 131 return output; 132 } 133 134 private static void setRowColor(byte[] row, byte value) { 135 for (int x = 0; x < row.length; x++) { 136 row[x] = value; 137 } 138 } 139 140}