1 47 package com.lowagie.text.pdf; 48 import java.awt.Canvas ; 49 import java.awt.Color ; 50 import java.awt.Image ; 51 import java.awt.image.MemoryImageSource ; 52 53 import com.lowagie.text.Rectangle; 54 55 66 public class BarcodePostnet extends Barcode{ 67 68 70 private static final byte BARS[][] = 71 { 72 {1,1,0,0,0}, 73 {0,0,0,1,1}, 74 {0,0,1,0,1}, 75 {0,0,1,1,0}, 76 {0,1,0,0,1}, 77 {0,1,0,1,0}, 78 {0,1,1,0,0}, 79 {1,0,0,0,1}, 80 {1,0,0,1,0}, 81 {1,0,1,0,0} 82 }; 83 84 85 public BarcodePostnet() { 86 n = 72f / 22f; x = 0.02f * 72f; barHeight = 0.125f * 72f; size = 0.05f * 72f; codeType = POSTNET; } 92 93 97 public static byte[] getBarsPostnet(String text) { 98 int total = 0; 99 for (int k = text.length() - 1; k >= 0; --k) { 100 int n = text.charAt(k) - '0'; 101 total += n; 102 } 103 text += (char)(((10 - (total % 10)) % 10) + '0'); 104 byte bars[] = new byte[text.length() * 5 + 2]; 105 bars[0] = 1; 106 bars[bars.length - 1] = 1; 107 for (int k = 0; k < text.length(); ++k) { 108 int c = text.charAt(k) - '0'; 109 System.arraycopy(BARS[c], 0, bars, k * 5 + 1, 5); 110 } 111 return bars; 112 } 113 114 118 public Rectangle getBarcodeSize() { 119 float width = ((code.length() + 1) * 5 + 1) * n + x; 120 return new Rectangle(width, barHeight); 121 } 122 123 159 public Rectangle placeBarcode(PdfContentByte cb, Color barColor, Color textColor) { 160 if (barColor != null) 161 cb.setColorFill(barColor); 162 byte bars[] = getBarsPostnet(code); 163 byte flip = 1; 164 if (codeType == PLANET) { 165 flip = 0; 166 bars[0] = 0; 167 bars[bars.length - 1] = 0; 168 } 169 float startX = 0; 170 for (int k = 0; k < bars.length; ++k) { 171 cb.rectangle(startX, 0, x - inkSpreading, bars[k] == flip ? barHeight : size); 172 startX += n; 173 } 174 cb.fill(); 175 return getBarcodeSize(); 176 } 177 178 185 public java.awt.Image createAwtImage(Color foreground, Color background) { 186 int f = foreground.getRGB(); 187 int g = background.getRGB(); 188 Canvas canvas = new Canvas (); 189 int barWidth = (int)x; 190 if (barWidth <= 0) 191 barWidth = 1; 192 int barDistance = (int)n; 193 if (barDistance <= barWidth) 194 barDistance = barWidth + 1; 195 int barShort = (int)size; 196 if (barShort <= 0) 197 barShort = 1; 198 int barTall = (int)barHeight; 199 if (barTall <= barShort) 200 barTall = barShort + 1; 201 int width = ((code.length() + 1) * 5 + 1) * barDistance + barWidth; 202 int pix[] = new int[width * barTall]; 203 byte bars[] = getBarsPostnet(code); 204 byte flip = 1; 205 if (codeType == PLANET) { 206 flip = 0; 207 bars[0] = 0; 208 bars[bars.length - 1] = 0; 209 } 210 int idx = 0; 211 for (int k = 0; k < bars.length; ++k) { 212 boolean dot = (bars[k] == flip); 213 for (int j = 0; j < barDistance; ++j) { 214 pix[idx + j] = ((dot && j < barWidth) ? f : g); 215 } 216 idx += barDistance; 217 } 218 int limit = width * (barTall - barShort); 219 for (int k = width; k < limit; k += width) 220 System.arraycopy(pix, 0, pix, k, width); 221 idx = limit; 222 for (int k = 0; k < bars.length; ++k) { 223 for (int j = 0; j < barDistance; ++j) { 224 pix[idx + j] = ((j < barWidth) ? f : g); 225 } 226 idx += barDistance; 227 } 228 for (int k = limit + width; k < pix.length; k += width) 229 System.arraycopy(pix, limit, pix, k, width); 230 Image img = canvas.createImage(new MemoryImageSource (width, barTall, pix, 0, width)); 231 232 return img; 233 } 234 } | Popular Tags |