byteCodeGen.java
· 9.9 KiB · Java
Исходник
public static byte[] generateByteCode(List<Command> commands, Base64Converter converter) {
final ByteArrayOutputStream payload = new ByteArrayOutputStream(128);
// Initialize the printer: Clear buffers and reset states
for (int i=0; i<40; i++) {
payload.write(0x00);
payload.write(Constants.ESC);
payload.write('@');
}
for (final Command command : commands) {
switch (command.getCommandId()) {
case Align:
payload.write(Constants.ESC);
payload.write((byte) 'a');
payload.write((byte) ((((Align) command).getType() == Align.Type.Left) ? 0 : ((((Align) command).getType() == Align.Type.Center) ? 1 : 2)));
break;
case Barcode:
final Barcode barCode = (Barcode) command;
// Set Bar Code Height first
payload.write(Constants.GS);
payload.write((byte) 0x68);
payload.write((byte) barCode.getHeight());
byte barcodeType = 0;
// Print Bar Code
// TODO: Add Error Handling for invalid data. For now we are just ignoring the invalid data
switch (barCode.getType()) {
case UPCA:
barcodeType = 0;
if (barCode.getCode().length() != 11) {
System.out.println("PrinterJobHandler: Bar Code invalid length for " + barCode.getType().name() + ": " + barCode.getCode().length());
continue;
}
break;
case UPCE:
barcodeType = 1;
if (barCode.getCode().length() != 7) {
System.out.println("PrinterJobHandler: Bar Code invalid length for " + barCode.getType().name() + ": " + barCode.getCode().length());
continue;
}
break;
case JAN13:
barcodeType = 2;
if (barCode.getCode().length() != 12) {
System.out.println("PrinterJobHandler: Bar Code invalid length for " + barCode.getType().name() + ": " + barCode.getCode().length());
continue;
}
break;
case JAN8:
barcodeType = 3;
if (barCode.getCode().length() != 7) {
System.out.println("PrinterJobHandler: Bar Code invalid length for " + barCode.getType().name() + ": " + barCode.getCode().length());
continue;
}
break;
case CODE39:
barcodeType = 4;
break;
case ITF:
barcodeType = 5;
if (barCode.getCode().length() % 2 != 1) {
System.out.println("PrinterJobHandler: Bar Code invalid length for " + barCode.getType().name() + ": " + barCode.getCode().length());
continue;
}
break;
case NW7:
barcodeType = 6;
break;
}
// Send start of Bar Code
payload.write(Constants.GS);
payload.write('k');
payload.write(barcodeType);
// Send Bar Code data
for (Byte n : barCode.getCode().getBytes()) {
payload.write(n);
}
payload.write((byte) 0x00);
break;
case Bitmap:
if (converter != null) {
Bitmap bmp = (Bitmap) command;
BitSet imageData = RasterTools.imageDataToBits(converter.fromBase64(bmp.getRawb64()));
final byte widthLSB = (byte) (bmp.getWidth() & 0xFF);
final byte widthMSB = (byte) ((bmp.getWidth() >> 8) & 0xFF);
// Set line height for 24 pt
payload.write(Constants.ESC);
payload.write('3');
payload.write(24);
int offset = 0;
while (offset < bmp.getHeight()) {
payload.write(Constants.ESC);
payload.write('*');
payload.write('!');
payload.write(widthLSB);
payload.write(widthMSB);
int lineIndex = 0;
byte[] line = new byte[3 * bmp.getWidth()];
for (int x = 0; x < bmp.getWidth(); x++) {
for (int k = 0; k < 3; k++) {
byte slice = 0;
for (int b = 0; b < 8; b++) {
final int y = (((offset / 8) + k) * 8) + b;
final int i = (y * bmp.getWidth()) + x;
boolean v = false;
if (i < imageData.size()) {
v = !imageData.get(i);
}
slice |= (byte) ((v ? 1 : 0) << (7 - b));
}
line[lineIndex + k] = slice;
}
lineIndex += 3;
}
for (byte b : line) {
payload.write(b);
}
payload.write('\n');
offset += 24;
}
// Set line height for default
payload.write(Constants.ESC);
payload.write('2');
} else {
System.out.println("PrinterJobHandler: Unable to print bitmap because there is no Base64Converter");
}
break;
case Bold:
payload.write(Constants.ESC);
payload.write('E');
payload.write(((Bold) command).getEnabled() ? (byte) 1 : (byte) 0);
break;
case Color:
payload.write(Constants.ESC);
payload.write('r');
payload.write(((Color) command).getType() == Color.Type.Black ? (byte) 0 : (byte) 1);
break;
case Cut:
payload.write('\n');
payload.write('\n');
payload.write('\n');
payload.write('\n');
payload.write('\n');
payload.write(Constants.ESC);
payload.write(((Cut) command).getType() == Cut.Type.Full ? ((byte) 'i') : ((byte) 'm'));
break;
case DashedLine:
payload.write(Constants.ESC); // Align center
payload.write((byte) 'a');
payload.write((byte) 1);
switch (((DashedLine) command).getFont()) {
case FontB:
for (final Byte n : DashedLine.dashedLineB.getBytes()) {
payload.write(n);
}
break;
default: // Defaults to FontA
for (final Byte n : DashedLine.dashedLineA.getBytes()) {
payload.write(n);
}
break;
}
payload.write(Constants.ESC); // Align Left
payload.write((byte) 'a');
payload.write((byte) 0);
break;
case Encoding:
payload.write(Constants.FS);
payload.write('(');
payload.write('C');
payload.write((byte) 0x02);
payload.write((byte) 0x00);
payload.write((byte) 0x30);
payload.write(((Encoding) command).getType() == Encoding.Type.ASCII ? (byte) 1 : (byte) 2);
break;
case Font:
payload.write(Constants.ESC);
payload.write('M');
switch (((Font) command).getFont()) {
case FontA:
payload.write((byte) 0);
break;
case FontB:
payload.write((byte) 1);
break;
case FontC:
payload.write((byte) 2);
break;
case FontD:
payload.write((byte) 3);
break;
case FontE:
payload.write((byte) 4);
break;
case SpecialFontA:
payload.write((byte) 97);
break;
case SpecialFontB:
payload.write((byte) 98);
break;
}
break;
case LineHeight:
LineHeight cmd = (LineHeight) command;
if (cmd.getLineHeight() == LineHeight.DEFAULT_SPACING) {
payload.write(Constants.ESC);
payload.write('2');
} else {
payload.write(Constants.ESC);
payload.write('3');
payload.write(cmd.getLineHeight());
}
break;
case ReverseColor:
payload.write(Constants.GS);
payload.write('B');
payload.write(((ReverseColor) command).getEnabled() ? (byte) 1 : (byte) 0);
break;
case Reset:
payload.write(Constants.ESC);
payload.write('@');
break;
case Margin:
final Margin mrgn = (Margin) command;
switch (mrgn.getType()) {
case Left:
payload.write(Constants.ESC);
payload.write(Constants.SP);
payload.write((byte) (mrgn.getSize() & 0xFF));
break;
case Right:
payload.write(Constants.GS);
payload.write((byte) 'L');
payload.write((byte) ((mrgn.getSize() & 0xFFFF) % 0xFF));
payload.write((byte) ((mrgn.getSize() & 0xFFFF) / 0xFF));
}
break;
case SetTabSpace:
payload.write(Constants.ESC);
payload.write('D');
final int tabSize = ((SetTabSpace) command).getTabSpace();
for (int i = 1; i < 32; i++) {
payload.write((byte) (i * tabSize));
}
payload.write(0x00);
break;
case SetTabPosition:
payload.write(Constants.ESC);
payload.write('D');
final List<Integer> pos = ((SetTabPosition) command).getTabSpace();
for (Integer po : pos) {
payload.write(po.byteValue());
}
payload.write(0x00);
break;
case Underline:
payload.write(Constants.ESC);
payload.write('-');
payload.write(((Underline) command).getEnabled() ? (byte) 1 : (byte) 0);
break;
case NewLine:
payload.write((byte) 0x0A);
break;
case Text:
final String text = ((Text) command).getText();
for (final Byte n : text.getBytes()) {
payload.write(n);
}
break;
}
}
return payload.toByteArray();
}
| 1 | public static byte[] generateByteCode(List<Command> commands, Base64Converter converter) { |
| 2 | final ByteArrayOutputStream payload = new ByteArrayOutputStream(128); |
| 3 | |
| 4 | // Initialize the printer: Clear buffers and reset states |
| 5 | for (int i=0; i<40; i++) { |
| 6 | payload.write(0x00); |
| 7 | payload.write(Constants.ESC); |
| 8 | payload.write('@'); |
| 9 | } |
| 10 | |
| 11 | for (final Command command : commands) { |
| 12 | switch (command.getCommandId()) { |
| 13 | case Align: |
| 14 | payload.write(Constants.ESC); |
| 15 | payload.write((byte) 'a'); |
| 16 | payload.write((byte) ((((Align) command).getType() == Align.Type.Left) ? 0 : ((((Align) command).getType() == Align.Type.Center) ? 1 : 2))); |
| 17 | break; |
| 18 | case Barcode: |
| 19 | final Barcode barCode = (Barcode) command; |
| 20 | // Set Bar Code Height first |
| 21 | payload.write(Constants.GS); |
| 22 | payload.write((byte) 0x68); |
| 23 | payload.write((byte) barCode.getHeight()); |
| 24 | byte barcodeType = 0; |
| 25 | // Print Bar Code |
| 26 | // TODO: Add Error Handling for invalid data. For now we are just ignoring the invalid data |
| 27 | switch (barCode.getType()) { |
| 28 | case UPCA: |
| 29 | barcodeType = 0; |
| 30 | if (barCode.getCode().length() != 11) { |
| 31 | System.out.println("PrinterJobHandler: Bar Code invalid length for " + barCode.getType().name() + ": " + barCode.getCode().length()); |
| 32 | continue; |
| 33 | } |
| 34 | break; |
| 35 | case UPCE: |
| 36 | barcodeType = 1; |
| 37 | if (barCode.getCode().length() != 7) { |
| 38 | System.out.println("PrinterJobHandler: Bar Code invalid length for " + barCode.getType().name() + ": " + barCode.getCode().length()); |
| 39 | continue; |
| 40 | } |
| 41 | break; |
| 42 | case JAN13: |
| 43 | barcodeType = 2; |
| 44 | if (barCode.getCode().length() != 12) { |
| 45 | System.out.println("PrinterJobHandler: Bar Code invalid length for " + barCode.getType().name() + ": " + barCode.getCode().length()); |
| 46 | continue; |
| 47 | } |
| 48 | break; |
| 49 | case JAN8: |
| 50 | barcodeType = 3; |
| 51 | if (barCode.getCode().length() != 7) { |
| 52 | System.out.println("PrinterJobHandler: Bar Code invalid length for " + barCode.getType().name() + ": " + barCode.getCode().length()); |
| 53 | continue; |
| 54 | } |
| 55 | break; |
| 56 | case CODE39: |
| 57 | barcodeType = 4; |
| 58 | break; |
| 59 | case ITF: |
| 60 | barcodeType = 5; |
| 61 | if (barCode.getCode().length() % 2 != 1) { |
| 62 | System.out.println("PrinterJobHandler: Bar Code invalid length for " + barCode.getType().name() + ": " + barCode.getCode().length()); |
| 63 | continue; |
| 64 | } |
| 65 | break; |
| 66 | case NW7: |
| 67 | barcodeType = 6; |
| 68 | break; |
| 69 | } |
| 70 | // Send start of Bar Code |
| 71 | payload.write(Constants.GS); |
| 72 | payload.write('k'); |
| 73 | payload.write(barcodeType); |
| 74 | |
| 75 | // Send Bar Code data |
| 76 | for (Byte n : barCode.getCode().getBytes()) { |
| 77 | payload.write(n); |
| 78 | } |
| 79 | |
| 80 | payload.write((byte) 0x00); |
| 81 | break; |
| 82 | case Bitmap: |
| 83 | if (converter != null) { |
| 84 | Bitmap bmp = (Bitmap) command; |
| 85 | BitSet imageData = RasterTools.imageDataToBits(converter.fromBase64(bmp.getRawb64())); |
| 86 | final byte widthLSB = (byte) (bmp.getWidth() & 0xFF); |
| 87 | final byte widthMSB = (byte) ((bmp.getWidth() >> 8) & 0xFF); |
| 88 | |
| 89 | // Set line height for 24 pt |
| 90 | payload.write(Constants.ESC); |
| 91 | payload.write('3'); |
| 92 | payload.write(24); |
| 93 | |
| 94 | int offset = 0; |
| 95 | while (offset < bmp.getHeight()) { |
| 96 | payload.write(Constants.ESC); |
| 97 | payload.write('*'); |
| 98 | payload.write('!'); |
| 99 | payload.write(widthLSB); |
| 100 | payload.write(widthMSB); |
| 101 | |
| 102 | int lineIndex = 0; |
| 103 | byte[] line = new byte[3 * bmp.getWidth()]; |
| 104 | for (int x = 0; x < bmp.getWidth(); x++) { |
| 105 | for (int k = 0; k < 3; k++) { |
| 106 | byte slice = 0; |
| 107 | for (int b = 0; b < 8; b++) { |
| 108 | final int y = (((offset / 8) + k) * 8) + b; |
| 109 | final int i = (y * bmp.getWidth()) + x; |
| 110 | boolean v = false; |
| 111 | if (i < imageData.size()) { |
| 112 | v = !imageData.get(i); |
| 113 | } |
| 114 | slice |= (byte) ((v ? 1 : 0) << (7 - b)); |
| 115 | } |
| 116 | line[lineIndex + k] = slice; |
| 117 | } |
| 118 | lineIndex += 3; |
| 119 | } |
| 120 | |
| 121 | for (byte b : line) { |
| 122 | payload.write(b); |
| 123 | } |
| 124 | |
| 125 | payload.write('\n'); |
| 126 | offset += 24; |
| 127 | } |
| 128 | |
| 129 | // Set line height for default |
| 130 | payload.write(Constants.ESC); |
| 131 | payload.write('2'); |
| 132 | } else { |
| 133 | System.out.println("PrinterJobHandler: Unable to print bitmap because there is no Base64Converter"); |
| 134 | } |
| 135 | break; |
| 136 | case Bold: |
| 137 | payload.write(Constants.ESC); |
| 138 | payload.write('E'); |
| 139 | payload.write(((Bold) command).getEnabled() ? (byte) 1 : (byte) 0); |
| 140 | break; |
| 141 | case Color: |
| 142 | payload.write(Constants.ESC); |
| 143 | payload.write('r'); |
| 144 | payload.write(((Color) command).getType() == Color.Type.Black ? (byte) 0 : (byte) 1); |
| 145 | break; |
| 146 | case Cut: |
| 147 | payload.write('\n'); |
| 148 | payload.write('\n'); |
| 149 | payload.write('\n'); |
| 150 | payload.write('\n'); |
| 151 | payload.write('\n'); |
| 152 | payload.write(Constants.ESC); |
| 153 | payload.write(((Cut) command).getType() == Cut.Type.Full ? ((byte) 'i') : ((byte) 'm')); |
| 154 | break; |
| 155 | case DashedLine: |
| 156 | payload.write(Constants.ESC); // Align center |
| 157 | payload.write((byte) 'a'); |
| 158 | payload.write((byte) 1); |
| 159 | switch (((DashedLine) command).getFont()) { |
| 160 | case FontB: |
| 161 | for (final Byte n : DashedLine.dashedLineB.getBytes()) { |
| 162 | payload.write(n); |
| 163 | } |
| 164 | break; |
| 165 | default: // Defaults to FontA |
| 166 | for (final Byte n : DashedLine.dashedLineA.getBytes()) { |
| 167 | payload.write(n); |
| 168 | } |
| 169 | break; |
| 170 | } |
| 171 | payload.write(Constants.ESC); // Align Left |
| 172 | payload.write((byte) 'a'); |
| 173 | payload.write((byte) 0); |
| 174 | break; |
| 175 | case Encoding: |
| 176 | payload.write(Constants.FS); |
| 177 | payload.write('('); |
| 178 | payload.write('C'); |
| 179 | payload.write((byte) 0x02); |
| 180 | payload.write((byte) 0x00); |
| 181 | payload.write((byte) 0x30); |
| 182 | payload.write(((Encoding) command).getType() == Encoding.Type.ASCII ? (byte) 1 : (byte) 2); |
| 183 | break; |
| 184 | case Font: |
| 185 | payload.write(Constants.ESC); |
| 186 | payload.write('M'); |
| 187 | switch (((Font) command).getFont()) { |
| 188 | case FontA: |
| 189 | payload.write((byte) 0); |
| 190 | break; |
| 191 | case FontB: |
| 192 | payload.write((byte) 1); |
| 193 | break; |
| 194 | case FontC: |
| 195 | payload.write((byte) 2); |
| 196 | break; |
| 197 | case FontD: |
| 198 | payload.write((byte) 3); |
| 199 | break; |
| 200 | case FontE: |
| 201 | payload.write((byte) 4); |
| 202 | break; |
| 203 | case SpecialFontA: |
| 204 | payload.write((byte) 97); |
| 205 | break; |
| 206 | case SpecialFontB: |
| 207 | payload.write((byte) 98); |
| 208 | break; |
| 209 | } |
| 210 | break; |
| 211 | case LineHeight: |
| 212 | LineHeight cmd = (LineHeight) command; |
| 213 | if (cmd.getLineHeight() == LineHeight.DEFAULT_SPACING) { |
| 214 | payload.write(Constants.ESC); |
| 215 | payload.write('2'); |
| 216 | } else { |
| 217 | payload.write(Constants.ESC); |
| 218 | payload.write('3'); |
| 219 | payload.write(cmd.getLineHeight()); |
| 220 | } |
| 221 | break; |
| 222 | case ReverseColor: |
| 223 | payload.write(Constants.GS); |
| 224 | payload.write('B'); |
| 225 | payload.write(((ReverseColor) command).getEnabled() ? (byte) 1 : (byte) 0); |
| 226 | break; |
| 227 | case Reset: |
| 228 | payload.write(Constants.ESC); |
| 229 | payload.write('@'); |
| 230 | break; |
| 231 | case Margin: |
| 232 | final Margin mrgn = (Margin) command; |
| 233 | switch (mrgn.getType()) { |
| 234 | case Left: |
| 235 | payload.write(Constants.ESC); |
| 236 | payload.write(Constants.SP); |
| 237 | payload.write((byte) (mrgn.getSize() & 0xFF)); |
| 238 | break; |
| 239 | case Right: |
| 240 | payload.write(Constants.GS); |
| 241 | payload.write((byte) 'L'); |
| 242 | payload.write((byte) ((mrgn.getSize() & 0xFFFF) % 0xFF)); |
| 243 | payload.write((byte) ((mrgn.getSize() & 0xFFFF) / 0xFF)); |
| 244 | } |
| 245 | break; |
| 246 | case SetTabSpace: |
| 247 | payload.write(Constants.ESC); |
| 248 | payload.write('D'); |
| 249 | final int tabSize = ((SetTabSpace) command).getTabSpace(); |
| 250 | for (int i = 1; i < 32; i++) { |
| 251 | payload.write((byte) (i * tabSize)); |
| 252 | } |
| 253 | payload.write(0x00); |
| 254 | break; |
| 255 | case SetTabPosition: |
| 256 | payload.write(Constants.ESC); |
| 257 | payload.write('D'); |
| 258 | |
| 259 | final List<Integer> pos = ((SetTabPosition) command).getTabSpace(); |
| 260 | |
| 261 | for (Integer po : pos) { |
| 262 | payload.write(po.byteValue()); |
| 263 | } |
| 264 | payload.write(0x00); |
| 265 | |
| 266 | break; |
| 267 | case Underline: |
| 268 | payload.write(Constants.ESC); |
| 269 | payload.write('-'); |
| 270 | payload.write(((Underline) command).getEnabled() ? (byte) 1 : (byte) 0); |
| 271 | break; |
| 272 | case NewLine: |
| 273 | payload.write((byte) 0x0A); |
| 274 | break; |
| 275 | case Text: |
| 276 | final String text = ((Text) command).getText(); |
| 277 | for (final Byte n : text.getBytes()) { |
| 278 | payload.write(n); |
| 279 | } |
| 280 | break; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | return payload.toByteArray(); |
| 285 | } |