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 import org.krysalis.barcode.BarcodeDimension; 64 import org.krysalis.barcode.ChecksumMode; 65 import org.krysalis.barcode.ClassicBarcodeLogicHandler; 66 import org.krysalis.barcode.output.Canvas; 67 import org.krysalis.barcode.output.CanvasProvider; 68 import org.krysalis.barcode.tools.Length; 69 70 75 public class Code39 extends GenericBarcodeImpl 76 implements Configurable { 77 78 79 protected static final double DEFAULT_WIDE_FACTOR = 2.5; 80 81 private ChecksumMode checksumMode = ChecksumMode.CP_AUTO; 82 private double intercharGapWidth = moduleWidth; 83 private double wideFactor = DEFAULT_WIDE_FACTOR; 85 88 public void configure(Configuration cfg) throws ConfigurationException { 89 Length mw = new Length(cfg.getChild("module-width").getValue("0.19mm"), "mm"); 91 this.moduleWidth = mw.getValueAsMillimeter(); 92 93 super.configure(cfg); 94 95 this.checksumMode = ChecksumMode.byName( 97 cfg.getChild("checksum").getValue(ChecksumMode.CP_AUTO.getName())); 98 99 this.wideFactor = cfg.getChild("wide-factor").getValueAsFloat((float)DEFAULT_WIDE_FACTOR); 101 102 Length igw = new Length(cfg.getChild("interchar-gap-width").getValue("1mw"), "mw"); 104 if (igw.getUnit().equalsIgnoreCase("mw")) { 105 this.intercharGapWidth = igw.getValue() * getModuleWidth(); 106 } else { 107 this.intercharGapWidth = igw.getValueAsMillimeter(); 108 } 109 } 110 111 115 public void setChecksumMode(ChecksumMode mode) { 116 this.checksumMode = mode; 117 } 118 119 123 public ChecksumMode getChecksumMode() { 124 return this.checksumMode; 125 } 126 127 130 public double getBarWidth(int width) { 131 if (width == 1) { 132 return moduleWidth; 133 } else if (width == 2) { 134 return moduleWidth * wideFactor; 135 } else if (width == -1) { 136 return this.intercharGapWidth; 137 } else throw new IllegalArgumentException ("Only widths 1 and 2 allowed"); 138 } 139 140 141 144 public void generateBarcode(CanvasProvider canvas, String msg) { 145 if ((msg == null) 146 || (msg.length() == 0)) { 147 throw new NullPointerException ("Parameter msg must not be empty"); 148 } 149 150 ClassicBarcodeLogicHandler handler = 151 new DefaultCanvasLogicHandler(this, new Canvas(canvas)); 152 153 Code39LogicImpl impl = new Code39LogicImpl(getChecksumMode()); 154 impl.generateBarcodeLogic(handler, msg); 155 } 156 157 158 161 public BarcodeDimension calcDimensions(String msg) { 162 final double width = ((msg.length() + 2) * (3 * wideFactor + 6) * moduleWidth) 163 + ((msg.length() + 1) * intercharGapWidth); 164 final double qz = (hasQuietZone() ? quietZone : 0); 165 return new BarcodeDimension(width, getHeight(), 166 width + (2 * qz), getHeight(), 167 quietZone, 0.0); 168 } 169 170 } | Popular Tags |