1 /* 2 * Copyright 2013 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.aztec; 18 19 import com.google.zxing.BarcodeFormat; 20 import com.google.zxing.EncodeHintType; 21 import com.google.zxing.Writer; 22 import com.google.zxing.aztec.encoder.AztecCode; 23 import com.google.zxing.aztec.encoder.Encoder; 24 import com.google.zxing.common.BitMatrix; 25 26 import java.nio.charset.Charset; 27 import java.util.Map; 28 29 /** 30 * Renders an Aztec code as a {@link BitMatrix}. 31 */ 32 public final class AztecWriter implements Writer { 33 34 @Override encode(String contents, BarcodeFormat format, int width, int height)35 public BitMatrix encode(String contents, BarcodeFormat format, int width, int height) { 36 return encode(contents, format, width, height, null); 37 } 38 39 @Override encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType,?> hints)40 public BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType,?> hints) { 41 Charset charset = null; // Do not add any ECI code by default 42 int eccPercent = Encoder.DEFAULT_EC_PERCENT; 43 int layers = Encoder.DEFAULT_AZTEC_LAYERS; 44 if (hints != null) { 45 if (hints.containsKey(EncodeHintType.CHARACTER_SET)) { 46 charset = Charset.forName(hints.get(EncodeHintType.CHARACTER_SET).toString()); 47 } 48 if (hints.containsKey(EncodeHintType.ERROR_CORRECTION)) { 49 eccPercent = Integer.parseInt(hints.get(EncodeHintType.ERROR_CORRECTION).toString()); 50 } 51 if (hints.containsKey(EncodeHintType.AZTEC_LAYERS)) { 52 layers = Integer.parseInt(hints.get(EncodeHintType.AZTEC_LAYERS).toString()); 53 } 54 } 55 return encode(contents, format, width, height, charset, eccPercent, layers); 56 } 57 encode(String contents, BarcodeFormat format, int width, int height, Charset charset, int eccPercent, int layers)58 private static BitMatrix encode(String contents, BarcodeFormat format, 59 int width, int height, 60 Charset charset, int eccPercent, int layers) { 61 if (format != BarcodeFormat.AZTEC) { 62 throw new IllegalArgumentException("Can only encode AZTEC, but got " + format); 63 } 64 AztecCode aztec = Encoder.encode(contents, eccPercent, layers, charset); 65 return renderResult(aztec, width, height); 66 } 67 renderResult(AztecCode code, int width, int height)68 private static BitMatrix renderResult(AztecCode code, int width, int height) { 69 BitMatrix input = code.getMatrix(); 70 if (input == null) { 71 throw new IllegalStateException(); 72 } 73 int inputWidth = input.getWidth(); 74 int inputHeight = input.getHeight(); 75 int outputWidth = Math.max(width, inputWidth); 76 int outputHeight = Math.max(height, inputHeight); 77 78 int multiple = Math.min(outputWidth / inputWidth, outputHeight / inputHeight); 79 int leftPadding = (outputWidth - (inputWidth * multiple)) / 2; 80 int topPadding = (outputHeight - (inputHeight * multiple)) / 2; 81 82 BitMatrix output = new BitMatrix(outputWidth, outputHeight); 83 84 for (int inputY = 0, outputY = topPadding; inputY < inputHeight; inputY++, outputY += multiple) { 85 // Write the contents of this row of the barcode 86 for (int inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple) { 87 if (input.get(inputX, inputY)) { 88 output.setRegion(outputX, outputY, multiple, multiple); 89 } 90 } 91 } 92 return output; 93 } 94 } 95