1 58 package org.krysalis.barcode.impl; 59 60 import org.apache.avalon.framework.configuration.Configurable; 61 import org.apache.avalon.framework.configuration.Configuration; 62 import org.apache.avalon.framework.configuration.ConfigurationException; 63 64 import org.krysalis.barcode.BarcodeDimension; 65 import org.krysalis.barcode.ChecksumMode; 66 import org.krysalis.barcode.ClassicBarcodeLogicHandler; 67 import org.krysalis.barcode.output.Canvas; 68 import org.krysalis.barcode.output.CanvasProvider; 69 import org.krysalis.barcode.tools.Length; 70 71 76 public class Interleaved2Of5 extends GenericBarcodeImpl 77 implements Configurable { 78 79 private static final double DEFAULT_WIDE_FACTOR = 3.0; 80 81 private ChecksumMode checksumMode = ChecksumMode.CP_AUTO; 82 private double wideFactor = DEFAULT_WIDE_FACTOR; 84 87 public void configure(Configuration cfg) throws ConfigurationException { 88 Length mw = new Length(cfg.getChild("module-width").getValue("0.21mm"), "mm"); 90 this.moduleWidth = mw.getValueAsMillimeter(); 91 92 super.configure(cfg); 93 94 this.checksumMode = ChecksumMode.byName( 96 cfg.getChild("checksum").getValue(ChecksumMode.CP_AUTO.getName())); 97 98 this.wideFactor = cfg.getChild("wide-factor").getValueAsFloat((float)DEFAULT_WIDE_FACTOR); 100 } 101 102 105 public void generateBarcode(CanvasProvider canvas, String msg) { 106 if ((msg == null) 107 || (msg.length() == 0)) { 108 throw new NullPointerException ("Parameter msg must not be empty"); 109 } 110 111 ClassicBarcodeLogicHandler handler = 112 new DefaultCanvasLogicHandler(this, new Canvas(canvas)); 113 115 Interleaved2Of5LogicImpl impl = new Interleaved2Of5LogicImpl(getChecksumMode()); 116 impl.generateBarcodeLogic(handler, msg); 117 } 118 119 122 public BarcodeDimension calcDimensions(String msg) { 123 int msgLen = msg.length(); 124 if (getChecksumMode() == ChecksumMode.CP_ADD) { 125 msgLen++; 126 } 127 if ((msgLen % 2) != 0) { 128 msgLen++; } 130 final double charwidth = 2 * wideFactor + 3; 131 final double width = ((msgLen * charwidth) + 6 + wideFactor) * moduleWidth; 132 final double qz = (hasQuietZone() ? quietZone : 0); 133 return new BarcodeDimension(width, getHeight(), 134 width + (2 * qz), getHeight(), 135 quietZone, 0.0); 136 } 137 138 141 public double getBarWidth(int width) { 142 if (width == 1) { 143 return moduleWidth; 144 } else if (width == 2) { 145 return moduleWidth * wideFactor; 146 } else throw new IllegalArgumentException ("Only widths 1 and 2 allowed"); 147 } 148 149 153 public void setChecksumMode(ChecksumMode mode) { 154 this.checksumMode = mode; 155 } 156 157 161 public ChecksumMode getChecksumMode() { 162 return this.checksumMode; 163 } 164 165 } | Popular Tags |