0、开发者pdf、sdk等资料详见:
http://download.csdn.net/detail/kunyashaw/9376694
1、测试代码
包含文字打印、条形码打印、二维码打印
1 package com.huofu.speechundecrypt; 2 3 import android.content.Context; 4 import android.os.Bundle; 5 import android.support.v7.app.AppCompatActivity; 6 import android.view.View; 7 import android.widget.Button; 8 9 import java.io.UnsupportedEncodingException; 10 11 public class MainActivity extends AppCompatActivity { 12 13 Context ctxt; 14 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 19 Button btn = new Button(getApplicationContext()); 20 btn.setText("press me"); 21 btn.setOnClickListener(new View.OnClickListener() { 22 @Override 23 public void onClick(View v) { 24 writeDeal(); 25 } 26 }); 27 28 setContentView(btn); 29 ctxt = getApplicationContext(); 30 } 31 32 33 public void writeDeal(){ 34 TscWifiActivity TscEthernetDll = new TscWifiActivity(); 35 TscEthernetDll.openport("192.168.1.200", 9100);//打开指定IP和端口号 36 37 //setup(int width, int height, int speed, int density, int sensor, int sensor_distance, int sensor_offset) 38 //宽度、高度、速度、浓度 39 //sensor为0:sensor_distance 垂直间距距离 sensor_offset垂直间距的偏移 40 //sensor为1:sensor_distance 定义黑标高度和额外送出长度 sensor_offset黑标偏移量 41 TscEthernetDll.setup(40, 30, 4, 4, 0, 5, 0); 42 43 TscEthernetDll.clearbuffer(); 44 45 /*--------------------------标签打印机初始化--------------------------*/ 46 TscEthernetDll.sendcommand("SET TEAR ON\n");//是否将撕纸位置移动到撕纸处 47 //TscEthernetDll.sendcommand("SET COUNTER @1 1\n");//设定计时器及增量 48 TscEthernetDll.sendcommand("SHIFT 10\n"); 49 50 51 /*--------------------------打印文字--------------------------*/ 52 writeDelivery(TscEthernetDll, "", 1);//打印外送标签 53 writePackagedInfo(TscEthernetDll,"",1);//打印打包标签 54 55 56 /*--------------------------打印条形码--------------------------*/ 57 //barcode(int x, int y, String type, int height, int human_readable, int rotation, int narrow, int wide, String string) 58 //x 水平坐标左上角起点 y垂直坐标左上角起点 type条形码类型 height条形码高度 human_readable(0 人眼不可识别 1人眼可识别) rotation条形码旋转角度 59 //narrow 窄bar宽度 wide宽bar宽度 string为要显示的内容 60 TscEthernetDll.barcode(100, 100, "128", 100, 1, 0, 3, 3, "123456789"); 61 TscEthernetDll.printlabel(1, 1);//打印出缓冲区的数据,第一个参数是打印的分数,第二个是没份打印的张数 62 TscEthernetDll.clearbuffer(); 63 64 /*--------------------------打印二维码--------------------------*/ 65 String cmd = "QRCODE 100,10,H,7,M,0,M1,S1,\"hello world\"\n"; 66 TscEthernetDll.sendcommand(cmd); 67 TscEthernetDll.printlabel(1, 1); 68 TscEthernetDll.clearbuffer(); 69 70 TscEthernetDll.closeport(); 71 } 72 73 /** 74 * 打印外送信息 75 * @param TscEthernetDll 76 * @param msg 77 * @param font_size 78 */ 79 public void writeDelivery(TscWifiActivity TscEthernetDll, String msg, int font_size) { 80 try { 81 //String originalText = "TEXT 550,50,\"TSS24.BF2\",180," + font_size + "," + font_size + ",\"" + msg + "\"\n"; 82 String originalText = "TEXT 260,210,\"TSS24.BF2\",180,1,1,\"万通中心店 午餐\"\n"; 83 TscEthernetDll.sendcommand((originalText.getBytes("gb2312"))); 84 85 originalText = "TEXT 220,185,\"TSS24.BF2\",180,1,1,\"300 外送\"\n"; 86 TscEthernetDll.sendcommand((originalText.getBytes("gb2312"))); 87 88 originalText = "TEXT 310,150,\"TSS24.BF2\",180,1,1,\"联系人:张**\"\n"; 89 TscEthernetDll.sendcommand((originalText.getBytes("gb2312"))); 90 91 originalText = "TEXT 310,110,\"TSS24.BF2\",180,1,1,\"电话: 185 1342 ****\"\n"; 92 TscEthernetDll.sendcommand((originalText.getBytes("gb2312"))); 93 94 95 String address = "北京市**区 ****D座 25层 **科技有限公司";//10个汉字换一行 96 97 if (address.length() <= 10) { 98 originalText = "TEXT 310,80,\"TSS24.BF2\",180,1,1,\"地址:\"\""+address+"\"\n"; 99 TscEthernetDll.sendcommand((originalText.getBytes("gb2312"))); 100 101 } else { 102 String tmpString = address.substring(0,9); 103 String tmpString2 = address.substring(9,address.length()); 104 originalText = "TEXT 310,80,\"TSS24.BF2\",180,1,1,\"地址:\"\""+tmpString+"\"\n"; 105 TscEthernetDll.sendcommand((originalText.getBytes("gb2312"))); 106 107 String printText = "TEXT 310,50,\"TSS24.BF2\",180,1,1,\""+tmpString2+"\"\n"; 108 TscEthernetDll.sendcommand((printText.getBytes("gb2312"))); 109 } 110 111 TscEthernetDll.printlabel(1, 1);//打印出缓冲区的数据,第一个参数是打印的分数,第二个是每份打印的张数 112 TscEthernetDll.clearbuffer(); 113 } catch (UnsupportedEncodingException e) { 114 e.printStackTrace(); 115 } 116 } 117 118 119 public static void writePackagedInfo(TscWifiActivity TscEthernetDll, String msg, int font_size) { 120 try { 121 //String originalText = "TEXT 550,50,\"TSS24.BF2\",180," + font_size + "," + font_size + ",\"" + msg + "\"\n"; 122 String originalText = "TEXT 260,210,\"TSS24.BF2\",180,1,1,\"万通中心店 午餐\"\n"; 123 TscEthernetDll.sendcommand((originalText.getBytes("gb2312"))); 124 125 originalText = "TEXT 220,170,\"TSS24.BF2\",180,1,1,\"300 打包\"\n"; 126 TscEthernetDll.sendcommand((originalText.getBytes("gb2312"))); 127 128 originalText = "TEXT 315,130,\"TSS24.BF2\",180,1,1,\"应收:20.5元\"\n"; 129 TscEthernetDll.sendcommand((originalText.getBytes("gb2312"))); 130 131 originalText = "TEXT 170,130,\"TSS24.BF2\",180,1,1,\"实收: 30.5元\"\n"; 132 TscEthernetDll.sendcommand((originalText.getBytes("gb2312"))); 133 134 originalText = "TEXT 315,90,\"TSS24.BF2\",180,1,1,\"优惠: 10元\"\n"; 135 TscEthernetDll.sendcommand((originalText.getBytes("gb2312"))); 136 137 138 TscEthernetDll.printlabel(1, 1);//打印出缓冲区的数据,第一个参数是打印的分数,第二个是每份打印的张数 139 TscEthernetDll.clearbuffer(); 140 } catch (UnsupportedEncodingException e) { 141 e.printStackTrace(); 142 } 143 } 144 }
2、库
1 package com.huofu.speechundecrypt; 2 3 import android.app.Activity; 4 import android.content.Context; 5 import android.os.Bundle; 6 import android.os.StrictMode; 7 import android.util.Log; 8 import android.view.Menu; 9 import android.view.View; 10 import android.widget.Button; 11 import android.widget.TextView; 12 13 import java.io.FileInputStream; 14 import java.io.IOException; 15 import java.io.InputStream; 16 import java.io.OutputStream; 17 import java.net.Socket; 18 19 /** 20 * author: Created by zzl on 15/12/25. 21 */ 22 23 public class TscWifiActivity extends Activity { 24 private static final String TAG = "THINBTCLIENT"; 25 private static final boolean D = true; 26 private InputStream InStream = null; 27 private OutputStream OutStream = null; 28 private Socket socket = null; 29 private String printerstatus = ""; 30 private int last_bytes = 0; 31 private byte[] buffer = new byte[1024]; 32 private byte[] readBuf = new byte[1024]; 33 private Button connect = null; 34 private Button closeport = null; 35 private Button sendfile = null; 36 private Button status = null; 37 private TextView tv1 = null; 38 39 public TscWifiActivity() { 40 } 41 42 public void onCreate(Bundle savedInstanceState) { 43 super.onCreate(savedInstanceState); 44 this.setContentView(2130903040); 45 this.tv1 = (TextView) this.findViewById(2131165187); 46 this.connect = (Button) this.findViewById(2131165184); 47 this.connect.setOnClickListener(new View.OnClickListener() { 48 public void onClick(View v) { 49 TscWifiActivity.this.openport("192.168.1.40", 9100); 50 TscWifiActivity.this.sendcommand("SIZE 3,1\n"); 51 TscWifiActivity.this.sendcommand("PRINT 5\n"); 52 } 53 }); 54 this.closeport = (Button) this.findViewById(2131165185); 55 this.closeport.setOnClickListener(new View.OnClickListener() { 56 public void onClick(View v) { 57 TscWifiActivity.this.closeport(); 58 } 59 }); 60 this.sendfile = (Button) this.findViewById(2131165186); 61 this.sendfile.setOnClickListener(new View.OnClickListener() { 62 public void onClick(View v) { 63 TscWifiActivity.this.openport("192.168.1.58", 9100); 64 TscWifiActivity.this.downloadpcx("UL.PCX"); 65 TscWifiActivity.this.downloadbmp("Triangle.bmp"); 66 TscWifiActivity.this.downloadttf("ARIAL.TTF"); 67 TscWifiActivity.this.setup(70, 110, 4, 4, 0, 0, 0); 68 TscWifiActivity.this.clearbuffer(); 69 TscWifiActivity.this.sendcommand("SET TEAR ON\n"); 70 TscWifiActivity.this.sendcommand("SET COUNTER @1 1\n"); 71 TscWifiActivity.this.sendcommand("@1 = \"0001\"\n"); 72 TscWifiActivity.this.sendcommand("TEXT 100,300,\"3\",0,1,1,@1\n"); 73 TscWifiActivity.this.sendcommand("PUTPCX 100,300,\"UL.PCX\"\n"); 74 TscWifiActivity.this.sendcommand("PUTBMP 100,520,\"Triangle.bmp\"\n"); 75 TscWifiActivity.this.sendcommand("TEXT 100,760,\"ARIAL.TTF\",0,15,15,\"THIS IS ARIAL FONT\"\n"); 76 TscWifiActivity.this.barcode(100, 100, "128", 100, 1, 0, 3, 3, "123456789"); 77 TscWifiActivity.this.printerfont(100, 250, "3", 0, 1, 1, "987654321"); 78 TscWifiActivity.this.printlabel(10, 1); 79 TscWifiActivity.this.sendfile("zpl.txt"); 80 TscWifiActivity.this.closeport(); 81 } 82 }); 83 this.status = (Button) this.findViewById(2131165188); 84 this.status.setOnClickListener(new View.OnClickListener() { 85 public void onClick(View v) { 86 String QQ = TscWifiActivity.this.batch(); 87 TscWifiActivity.this.tv1.setText(QQ); 88 } 89 }); 90 } 91 92 public void onStart() { 93 super.onStart(); 94 Log.e("THINBTCLIENT", "++ ON START ++"); 95 } 96 97 public void onResume() { 98 super.onResume(); 99 Log.e("THINBTCLIENT", "+ ON RESUME +"); 100 Log.e("THINBTCLIENT", "+ ABOUT TO ATTEMPT CLIENT CONNECT +"); 101 } 102 103 public void onPause() { 104 super.onPause(); 105 Log.e("THINBTCLIENT", "- ON PAUSE -"); 106 if (this.OutStream != null) { 107 try { 108 this.OutStream.flush(); 109 } catch (IOException var3) { 110 Log.e("THINBTCLIENT", "ON PAUSE: Couldn\'t flush output stream.", var3); 111 } 112 } 113 114 try { 115 this.socket.close(); 116 } catch (IOException var2) { 117 Log.e("THINBTCLIENT", "ON PAUSE: Unable to close socket.", var2); 118 } 119 120 } 121 122 public void onStop() { 123 super.onStop(); 124 Log.e("THINBTCLIENT", "-- ON STOP --"); 125 } 126 127 public void onDestroy() { 128 super.onDestroy(); 129 Log.e("THINBTCLIENT", "--- ON DESTROY ---"); 130 } 131 132 public boolean onCreateOptionsMenu(Menu menu) { 133 this.getMenuInflater().inflate(2131099648, menu); 134 return true; 135 } 136 137 public void openport(String ipaddress, int portnumber) { 138 StrictMode.setThreadPolicy((new StrictMode.ThreadPolicy.Builder()).detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build()); 139 StrictMode.setVmPolicy((new android.os.StrictMode.VmPolicy.Builder()).detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build()); 140 141 try { 142 this.socket = new Socket(ipaddress, portnumber); 143 this.InStream = this.socket.getInputStream(); 144 this.OutStream = this.socket.getOutputStream(); 145 } catch (Exception var4) { 146 ; 147 } 148 149 } 150 151 public void sendcommand(String message) { 152 byte[] msgBuffer = message.getBytes(); 153 154 try { 155 this.OutStream.write(msgBuffer); 156 } catch (IOException var4) { 157 var4.printStackTrace(); 158 } 159 160 } 161 162 public void sendcommand(byte[] message) { 163 try { 164 this.OutStream.write(message); 165 } catch (IOException var3) { 166 var3.printStackTrace(); 167 } 168 169 } 170 171 public String status() { 172 byte[] message = new byte[]{(byte) 27, (byte) 33, (byte) 63}; 173 174 try { 175 this.OutStream.write(message); 176 } catch (IOException var4) { 177 Log.e("THINBTCLIENT", "ON RESUME: Exception during write.", var4); 178 } 179 180 try { 181 Thread.sleep(1000L); 182 } catch (InterruptedException var3) { 183 var3.printStackTrace(); 184 } 185 186 int tim; 187 try { 188 while (this.InStream.available() > 0) { 189 this.readBuf = new byte[1024]; 190 tim = this.InStream.read(this.readBuf); 191 Log.e("","tim is "+tim); 192 } 193 } catch (IOException var5) { 194 var5.printStackTrace(); 195 } 196 197 if (this.readBuf[0] == 2 && this.readBuf[5] == 3) { 198 for (tim = 0; tim <= 7; ++tim) { 199 if (this.readBuf[tim] == 2 && this.readBuf[tim + 1] == 64 && this.readBuf[tim + 2] == 64 && this.readBuf[tim + 3] == 64 && this.readBuf[tim + 4] == 64 && this.readBuf[tim + 5] == 3) { 200 this.printerstatus = "Ready"; 201 this.readBuf = new byte[1024]; 202 break; 203 } 204 205 if (this.readBuf[tim] == 2 && this.readBuf[tim + 1] == 69 && this.readBuf[tim + 2] == 64 && this.readBuf[tim + 3] == 64 && this.readBuf[tim + 4] == 96 && this.readBuf[tim + 5] == 3) { 206 this.printerstatus = "Head Open"; 207 this.readBuf = new byte[1024]; 208 break; 209 } 210 211 if (this.readBuf[tim] == 2 && this.readBuf[tim + 1] == 64 && this.readBuf[tim + 2] == 64 && this.readBuf[tim + 3] == 64 && this.readBuf[tim + 4] == 96 && this.readBuf[tim + 5] == 3) { 212 this.printerstatus = "Head Open"; 213 this.readBuf = new byte[1024]; 214 break; 215 } 216 217 if (this.readBuf[tim] == 2 && this.readBuf[tim + 1] == 69 && this.readBuf[tim + 2] == 64 && this.readBuf[tim + 3] == 64 && this.readBuf[tim + 4] == 72 && this.readBuf[tim + 5] == 3) { 218 this.printerstatus = "Ribbon Jam"; 219 this.readBuf = new byte[1024]; 220 break; 221 } 222 223 if (this.readBuf[tim] == 2 && this.readBuf[tim + 1] == 69 && this.readBuf[tim + 2] == 64 && this.readBuf[tim + 3] == 64 && this.readBuf[tim + 4] == 68 && this.readBuf[tim + 5] == 3) { 224 this.printerstatus = "Ribbon Empty"; 225 this.readBuf = new byte[1024]; 226 break; 227 } 228 229 if (this.readBuf[tim] == 2 && this.readBuf[tim + 1] == 69 && this.readBuf[tim + 2] == 64 && this.readBuf[tim + 3] == 64 && this.readBuf[tim + 4] == 65 && this.readBuf[tim + 5] == 3) { 230 this.printerstatus = "No Paper"; 231 this.readBuf = new byte[1024]; 232 break; 233 } 234 235 if (this.readBuf[tim] == 2 && this.readBuf[tim + 1] == 69 && this.readBuf[tim + 2] == 64 && this.readBuf[tim + 3] == 64 && this.readBuf[tim + 4] == 66 && this.readBuf[tim + 5] == 3) { 236 this.printerstatus = "Paper Jam"; 237 this.readBuf = new byte[1024]; 238 break; 239 } 240 241 if (this.readBuf[tim] == 2 && this.readBuf[tim + 1] == 69 && this.readBuf[tim + 2] == 64 && this.readBuf[tim + 3] == 64 && this.readBuf[tim + 4] == 65 && this.readBuf[tim + 5] == 3) { 242 this.printerstatus = "Paper Empty"; 243 this.readBuf = new byte[1024]; 244 break; 245 } 246 247 if (this.readBuf[tim] == 2 && this.readBuf[tim + 1] == 67 && this.readBuf[tim + 2] == 64 && this.readBuf[tim + 3] == 64 && this.readBuf[tim + 4] == 64 && this.readBuf[tim + 5] == 3) { 248 this.printerstatus = "Cutting"; 249 this.readBuf = new byte[1024]; 250 break; 251 } 252 253 if (this.readBuf[tim] == 2 && this.readBuf[tim + 1] == 75 && this.readBuf[tim + 2] == 64 && this.readBuf[tim + 3] == 64 && this.readBuf[tim + 4] == 64 && this.readBuf[tim + 5] == 3) { 254 this.printerstatus = "Waiting to Press Print Key"; 255 this.readBuf = new byte[1024]; 256 break; 257 } 258 259 if (this.readBuf[tim] == 2 && this.readBuf[tim + 1] == 76 && this.readBuf[tim + 2] == 64 && this.readBuf[tim + 3] == 64 && this.readBuf[tim + 4] == 64 && this.readBuf[tim + 5] == 3) { 260 this.printerstatus = "Waiting to Take Label"; 261 this.readBuf = new byte[1024]; 262 break; 263 } 264 265 if (this.readBuf[tim] == 2 && this.readBuf[tim + 1] == 80 && this.readBuf[tim + 2] == 64 && this.readBuf[tim + 3] == 64 && this.readBuf[tim + 4] == 64 && this.readBuf[tim + 5] == 3) { 266 this.printerstatus = "Printing Batch"; 267 this.readBuf = new byte[1024]; 268 break; 269 } 270 271 if (this.readBuf[tim] == 2 && this.readBuf[tim + 1] == 96 && this.readBuf[tim + 2] == 64 && this.readBuf[tim + 3] == 64 && this.readBuf[tim + 4] == 64 && this.readBuf[tim + 5] == 3) { 272 this.printerstatus = "Pause"; 273 this.readBuf = new byte[1024]; 274 break; 275 } 276 277 if (this.readBuf[tim] == 2 && this.readBuf[tim + 1] == 69 && this.readBuf[tim + 2] == 64 && this.readBuf[tim + 3] == 64 && this.readBuf[tim + 4] == 64 && this.readBuf[tim + 5] == 3) { 278 this.printerstatus = "Pause"; 279 this.readBuf = new byte[1024]; 280 break; 281 } 282 } 283 } 284 285 return this.printerstatus; 286 } 287 288 public String batch() { 289 boolean printvalue = false; 290 String printbatch = ""; 291 String stringbatch = "0"; 292 String message = "~HS"; 293 this.readBuf = new byte[1024]; 294 byte[] batcharray = new byte[]{(byte) 48, (byte) 48, (byte) 48, (byte) 48, (byte) 48, (byte) 48, (byte) 48, (byte) 48}; 295 byte[] msgBuffer = message.getBytes(); 296 297 try { 298 this.OutStream.write(msgBuffer); 299 } catch (IOException var9) { 300 Log.e("THINBTCLIENT", "ON RESUME: Exception during write.", var9); 301 } 302 303 try { 304 Thread.sleep(1000L); 305 } catch (InterruptedException var8) { 306 var8.printStackTrace(); 307 } 308 309 int i; 310 try { 311 while (this.InStream.available() > 50) { 312 this.readBuf = new byte[1024]; 313 i = this.InStream.read(this.readBuf); 314 } 315 } catch (IOException var10) { 316 var10.printStackTrace(); 317 } 318 319 if (this.readBuf[0] == 2) { 320 System.arraycopy(this.readBuf, 55, batcharray, 0, 8); 321 322 for (i = 0; i <= 7; ++i) { 323 if (batcharray[i] == 44) { 324 batcharray = new byte[]{(byte) 57, (byte) 57, (byte) 57, (byte) 57, (byte) 57, (byte) 57, (byte) 57, (byte) 57}; 325 } 326 } 327 328 stringbatch = new String(batcharray); 329 int var11 = Integer.parseInt(stringbatch); 330 printbatch = Integer.toString(var11); 331 if (printbatch == "99999999") { 332 printbatch = ""; 333 } 334 } 335 336 return printbatch; 337 } 338 339 public void closeport() { 340 try { 341 this.socket.close(); 342 } catch (IOException var2) { 343 var2.printStackTrace(); 344 } 345 346 } 347 348 public void setup(int width, int height, int speed, int density, int sensor, int sensor_distance, int sensor_offset) { 349 String message = ""; 350 String size = "SIZE " + width + " mm" + ", " + height + " mm"; 351 String speed_value = "SPEED " + speed; 352 String density_value = "DENSITY " + density; 353 String sensor_value = ""; 354 if (sensor == 0) { 355 sensor_value = "GAP " + sensor_distance + " mm" + ", " + sensor_offset + " mm"; 356 } else if (sensor == 1) { 357 sensor_value = "BLINE " + sensor_distance + " mm" + ", " + sensor_offset + " mm"; 358 } 359 360 message = size + "\n" + speed_value + "\n" + density_value + "\n" + sensor_value + "\n"; 361 byte[] msgBuffer = message.getBytes(); 362 363 try { 364 this.OutStream.write(msgBuffer); 365 } catch (IOException var15) { 366 var15.printStackTrace(); 367 } 368 369 } 370 371 public void clearbuffer() { 372 String message = "CLS\n"; 373 byte[] msgBuffer = message.getBytes(); 374 375 try { 376 this.OutStream.write(msgBuffer); 377 } catch (IOException var4) { 378 var4.printStackTrace(); 379 } 380 381 } 382 383 public void barcode(int x, int y, String type, int height, int human_readable, int rotation, int narrow, int wide, String string) { 384 String message = ""; 385 String barcode = "BARCODE "; 386 String position = x + "," + y; 387 String mode = "\"" + type + "\""; 388 String height_value = "" + height; 389 String human_value = "" + human_readable; 390 String rota = "" + rotation; 391 String narrow_value = "" + narrow; 392 String wide_value = "" + wide; 393 String string_value = "\"" + string + "\""; 394 message = barcode + position + " ," + mode + " ," + height_value + " ," + human_value + " ," + rota + " ," + narrow_value + " ," + wide_value + " ," + string_value + "\n"; 395 byte[] msgBuffer = message.getBytes(); 396 397 try { 398 this.OutStream.write(msgBuffer); 399 } catch (IOException var22) { 400 var22.printStackTrace(); 401 } 402 403 } 404 405 public void printerfont(int x, int y, String size, int rotation, int x_multiplication, int y_multiplication, String string) { 406 String message = ""; 407 String text = "TEXT "; 408 String position = x + "," + y; 409 String size_value = "\"" + size + "\""; 410 String rota = "" + rotation; 411 String x_value = "" + x_multiplication; 412 String y_value = "" + y_multiplication; 413 String string_value = "\"" + string + "\""; 414 message = text + position + " ," + size_value + " ," + rota + " ," + x_value + " ," + y_value + " ," + string_value + "\n"; 415 byte[] msgBuffer = message.getBytes(); 416 417 try { 418 this.OutStream.write(msgBuffer); 419 } catch (IOException var18) { 420 var18.printStackTrace(); 421 } 422 423 } 424 425 public void printlabel(int quantity, int copy) { 426 String message = ""; 427 message = "PRINT " + quantity + ", " + copy + "\n"; 428 byte[] msgBuffer = message.getBytes(); 429 430 try { 431 this.OutStream.write(msgBuffer); 432 } catch (IOException var6) { 433 var6.printStackTrace(); 434 } 435 436 } 437 438 public void formfeed() { 439 String message = ""; 440 message = "FORMFEED\n"; 441 byte[] msgBuffer = message.getBytes(); 442 443 try { 444 this.OutStream.write(msgBuffer); 445 } catch (IOException var4) { 446 var4.printStackTrace(); 447 } 448 449 } 450 451 public void nobackfeed() { 452 String message = ""; 453 message = "SET TEAR OFF\n"; 454 byte[] msgBuffer = message.getBytes(); 455 456 try { 457 this.OutStream.write(msgBuffer); 458 } catch (IOException var4) { 459 var4.printStackTrace(); 460 } 461 462 } 463 464 public void sendfile(String filename) { 465 try { 466 FileInputStream fis = new FileInputStream("/sdcard/Download/" + filename); 467 byte[] data = new byte[fis.available()]; 468 469 while (fis.read(data) != -1) { 470 ; 471 } 472 473 this.OutStream.write(data); 474 fis.close(); 475 } catch (Exception var4) { 476 ; 477 } 478 479 } 480 481 public void downloadpcx(String filename) { 482 try { 483 FileInputStream fis = new FileInputStream("/sdcard/Download/" + filename); 484 byte[] data = new byte[fis.available()]; 485 String download = "DOWNLOAD F,\"" + filename + "\"," + data.length + ","; 486 byte[] download_head = download.getBytes(); 487 488 while (fis.read(data) != -1) { 489 ; 490 } 491 492 this.OutStream.write(download_head); 493 this.OutStream.write(data); 494 fis.close(); 495 } catch (Exception var6) { 496 ; 497 } 498 499 } 500 501 public void downloadbmp(String filename,Context ctxt) { 502 try { 503 //String name = android.os.Environment.getExternalStorageState()+File.separator+filename; 504 //FileInputStream fis = new FileInputStream(name);//("/sdcard/Download/" + filename); 505 byte[] data = new FileUtils(ctxt).readSDFile(filename).getBytes("gb2312");//new byte[fis.available()]; 506 String download = "DOWNLOAD F,\"" + filename + "\"," + data.length + ","; 507 byte[] download_head = download.getBytes(); 508 509 this.OutStream.write(download_head); 510 this.OutStream.write(data); 511 } catch (Exception var6) { 512 Log.e("",""+var6.getMessage()); 513 } 514 515 } 516 517 public void downloadbmp(String filename) { 518 try { 519 //String name = android.os.Environment.getExternalStorageState()+File.separator+filename; 520 //FileInputStream fis = new FileInputStream(name);//("/sdcard/Download/" + filename); 521 byte[] data = new FileUtils(getApplicationContext()).readSDFile(filename).getBytes("gb2312");//new byte[fis.available()]; 522 String download = "DOWNLOAD F,\"" + filename + "\"," + data.length + ","; 523 byte[] download_head = download.getBytes(); 524 525 this.OutStream.write(download_head); 526 this.OutStream.write(data); 527 } catch (Exception var6) { 528 Log.e("",""+var6.getMessage()); 529 } 530 531 } 532 533 public void downloadttf(String filename) { 534 try { 535 FileInputStream fis = new FileInputStream("/sdcard/Download/" + filename); 536 byte[] data = new byte[fis.available()]; 537 String download = "DOWNLOAD F,\"" + filename + "\"," + data.length + ","; 538 byte[] download_head = download.getBytes(); 539 540 while (fis.read(data) != -1) { 541 ; 542 } 543 544 this.OutStream.write(download_head); 545 this.OutStream.write(data); 546 fis.close(); 547 } catch (Exception var6) { 548 ; 549 } 550 551 } 552 }
3、打印效果
来源:https://www.cnblogs.com/kunyashaw/p/5083552.html