2 * Copyright 2007 ZXing authors
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
16 namespace com.google.zxing.qrcode
\r
19 using System.Collections;
\r
20 using com.google.zxing.common;
\r
21 using com.google.zxing.qrcode.decoder;
\r
22 using com.google.zxing.qrcode.detector;
\r
23 using com.google.zxing.qrcode.encoder;
\r
25 public sealed class QRCodeWriter : Writer
\r
27 private static int QUIET_ZONE_SIZE = 4;
\r
28 public ByteMatrix encode(String contents, BarcodeFormat format, int width, int height)
\r
31 return encode(contents, format, width, height, null);
\r
32 }catch(Exception e){
\r
33 throw new WriterException(e.Message);
\r
37 public ByteMatrix encode(String contents, BarcodeFormat format, int width, int height,Hashtable hints) {
\r
39 if (contents == null || contents.Length == 0) {
\r
40 throw new ArgumentException("Found empty contents");
\r
43 if (format != BarcodeFormat.QR_CODE) {
\r
44 throw new ArgumentException("Can only encode QR_CODE, but got " + format);
\r
47 if (width < 0 || height < 0) {
\r
48 throw new ArgumentException("Requested dimensions are too small: " + width + 'x' +
\r
52 ErrorCorrectionLevel errorCorrectionLevel = ErrorCorrectionLevel.L;
\r
53 if (hints != null) {
\r
54 ErrorCorrectionLevel requestedECLevel = (ErrorCorrectionLevel) hints[EncodeHintType.ERROR_CORRECTION];
\r
55 if (requestedECLevel != null) {
\r
56 errorCorrectionLevel = requestedECLevel;
\r
60 QRCode code = new QRCode();
\r
61 Encoder.encode(contents, errorCorrectionLevel, code);
\r
62 return renderResult(code, width, height);
\r
65 // Note that the input matrix uses 0 == white, 1 == black, while the output matrix uses
\r
66 // 0 == black, 255 == white (i.e. an 8 bit greyscale bitmap).
\r
67 private static ByteMatrix renderResult(QRCode code, int width, int height) {
\r
68 ByteMatrix input = code.getMatrix();
\r
69 int inputWidth = input.width();
\r
70 int inputHeight = input.height();
\r
71 int qrWidth = inputWidth + (QUIET_ZONE_SIZE << 1);
\r
72 int qrHeight = inputHeight + (QUIET_ZONE_SIZE << 1);
\r
73 int outputWidth = Math.Max(width, qrWidth);
\r
74 int outputHeight = Math.Max(height, qrHeight);
\r
76 int multiple = Math.Min(outputWidth / qrWidth, outputHeight / qrHeight);
\r
77 // Padding includes both the quiet zone and the extra white pixels to accomodate the requested
\r
78 // dimensions. For example, if input is 25x25 the QR will be 33x33 including the quiet zone.
\r
79 // If the requested size is 200x160, the multiple will be 4, for a QR of 132x132. These will
\r
80 // handle all the padding from 100x100 (the actual QR) up to 200x160.
\r
81 int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;
\r
82 int topPadding = (outputHeight - (inputHeight * multiple)) / 2;
\r
84 ByteMatrix output = new ByteMatrix(outputHeight, outputWidth);
\r
85 sbyte[][] outputArray = output.getArray();
\r
87 // We could be tricky and use the first row in each set of multiple as the temporary storage,
\r
88 // instead of allocating this separate array.
\r
89 sbyte[] row = new sbyte[outputWidth];
\r
91 // 1. Write the white lines at the top
\r
92 for (int y = 0; y < topPadding; y++) {
\r
93 setRowColor(outputArray[y], unchecked((sbyte)255));
\r
96 // 2. Expand the QR image to the multiple
\r
97 sbyte[][] inputArray = input.getArray();
\r
98 for (int y = 0; y < inputHeight; y++) {
\r
99 // a. Write the white pixels at the left of each row
\r
100 for (int x = 0; x < leftPadding; x++) {
\r
101 row[x] = unchecked((sbyte) 255);
\r
104 // b. Write the contents of this row of the barcode
\r
105 int offset = leftPadding;
\r
106 for (int x = 0; x < inputWidth; x++) {
\r
107 sbyte value = (inputArray[y][x] == 1) ? (sbyte) 0 : unchecked((sbyte) 255);
\r
108 for (int z = 0; z < multiple; z++) {
\r
109 row[offset + z] = value;
\r
111 offset += multiple;
\r
114 // c. Write the white pixels at the right of each row
\r
115 offset = leftPadding + (inputWidth * multiple);
\r
116 for (int x = offset; x < outputWidth; x++) {
\r
117 row[x] = unchecked((sbyte) 255);
\r
120 // d. Write the completed row multiple times
\r
121 offset = topPadding + (y * multiple);
\r
122 for (int z = 0; z < multiple; z++) {
\r
123 System.Array.Copy(row, 0, outputArray[offset + z], 0, outputWidth);
\r
127 // 3. Write the white lines at the bottom
\r
128 int offset2 = topPadding + (inputHeight * multiple);
\r
129 for (int y = offset2; y < outputHeight; y++)
\r
131 setRowColor(outputArray[y], unchecked((sbyte) 255));
\r
136 private static void setRowColor(sbyte[] row, sbyte value) {
\r
137 for (int x = 0; x < row.Length; x++) {
\r