http://www.it165.net/pro/html/201503/35460.html

网上关于通过android来操作打印机的例子太少了,为了方便更多的开发同仁,将近日所学分享一下。

我这边是通过android设备通过无线来对打印机(佳博58mm热敏式-58130iC)操作,实现餐厅小票的打印。写了一个简单的小demo,分享下。

前提:

1、android设备一个(coolPad8085N)

2、小票打印机(佳博 58mm热敏式打印机-58130IC)

这里将打印机IP设置为固定IP(这里略微复杂,是前辈设置过的,我没有具体操作,问了一下:打印机自检出的条子可以显示IP、通过自带或者官网的window管理软件设置)

3、无线路由器一个(从局域网口引出一根网线接到小票打印机网口)

4、android设备连接wifi到同一网络

(这样就保证了android和打印机在同一网络内,并且打印机的IP是固定的,开发人员是知道的,很变态)

流程:

1、android运行app,根据打印机的IP地址和端口号创建套接字socket,得到可写流outputStream

2、根据订单号,获取订单信息(demo是模拟的)

3、套入模板,打印小票

难点在于小票的排版的实现,我做了个工具库,如下:


view source print ? 001. 1packagecom; 002. 2 003. 3 004. 4 005. 5publicclassprinterCmdUtils { 006. 6 007. 7publicstaticfinalbyteESC =27;//换码 008. 8publicstaticfinalbyteFS =28;//文本分隔符 009. 9publicstaticfinalbyteGS =29;//组分隔符 010. 10publicstaticfinalbyteDLE =16;//数据连接换码 011. 11publicstaticfinalbyteEOT =4;//传输结束 012. 12publicstaticfinalbyteENQ =5;//询问字符 013. 13publicstaticfinalbyteSP =32;//空格 014. 14publicstaticfinalbyteHT =9;//横向列表 015. 15publicstaticfinalbyteLF =10;//打印并换行(水平定位) 016. 16publicstaticfinalbyteCR =13;//归位键 017. 17publicstaticfinalbyteFF =12;//走纸控制(打印并回到标准模式(在页模式下) ) 018. 18publicstaticfinalbyteCAN =24;//作废(页模式下取消打印数据 ) 019. 19 020. 20 021. 21 022. 22//------------------------打印机初始化----------------------------- 023. 23 024. 24 025. 25/** 026. 26 * 打印机初始化 027. 27 * @return 028. 28 */ 029. 29publicstaticbyte[] init_printer() 030. 30{ 031. 31byte[] result =newbyte[2]; 032. 32result[0] = ESC; 033. 33result[1] =64; 034. 34returnresult; 035. 35} 036. 36 037. 37 038. 38//------------------------换行----------------------------- 039. 39 040. 40 041. 41/** 042. 42 * 换行 043. 43 * @param lineNum要换几行 044. 44 * @return 045. 45 */ 046. 46publicstaticbyte[] nextLine(intlineNum) 047. 47{ 048. 48byte[] result =newbyte[lineNum]; 049. 49for(inti=0;i<lineNum;i++) 050. 50{ 051. 51result[i] = LF; 052. 52} 053. 53 054. 54returnresult; 055. 55} 056. 56 057. 57 058. 58//------------------------下划线----------------------------- 059. 59 060. 60 061. 61/** 062. 62 * 绘制下划线(1点宽) 063. 63 * @return 064. 64 */ 065. 65publicstaticbyte[] underlineWithOneDotWidthOn() 066. 66{ 067. 67byte[] result =newbyte[3]; 068. 68result[0] = ESC; 069. 69result[1] =45; 070. 70result[2] =1; 071. 71returnresult; 072. 72} 073. 73 074. 74 075. 75/** 076. 76 * 绘制下划线(2点宽) 077. 77 * @return 078. 78 */ 079. 79publicstaticbyte[] underlineWithTwoDotWidthOn() 080. 80{ 081. 81byte[] result =newbyte[3]; 082. 82result[0] = ESC; 083. 83result[1] =45; 084. 84result[2] =2; 085. 85returnresult; 086. 86} 087. 87 088. 88 089. 89/** 090. 90 * 取消绘制下划线 091. 91 * @return 092. 92 */ 093. 93publicstaticbyte[] underlineOff() 094. 94{ 095. 95byte[] result =newbyte[3]; 096. 96result[0] = ESC; 097. 97result[1] =45; 098. 98result[2] =0; 099. 99returnresult; 100. 100} 101. 101 102. 102 103. 103//------------------------加粗----------------------------- 104. 104 105. 105 106. 106/** 107. 107 * 选择加粗模式 108. 108 * @return 109. 109 */ 110. 110publicstaticbyte[] boldOn() 111. 111{ 112. 112byte[] result =newbyte[3]; 113. 113result[0] = ESC; 114. 114result[1] =69; 115. 115result[2] =0xF; 116. 116returnresult; 117. 117} 118. 118 119. 119 120. 120/** 121. 121 * 取消加粗模式 122. 122 * @return 123. 123 */ 124. 124publicstaticbyte[] boldOff() 125. 125{ 126. 126byte[] result =newbyte[3]; 127. 127result[0] = ESC; 128. 128result[1] =69; 129. 129result[2] =0; 130. 130returnresult; 131. 131} 132. 132 133. 133 134. 134//------------------------对齐----------------------------- 135. 135 136. 136 137. 137/** 138. 138 * 左对齐 139. 139 * @return 140. 140 */ 141. 141publicstaticbyte[] alignLeft() 142. 142{ 143. 143byte[] result =newbyte[3]; 144. 144result[0] = ESC; 145. 145result[1] =97; 146. 146result[2] =0; 147. 147returnresult; 148. 148} 149. 149 150. 150 151. 151/** 152. 152 * 居中对齐 153. 153 * @return 154. 154 */ 155. 155publicstaticbyte[] alignCenter() 156. 156{ 157. 157byte[] result =newbyte[3]; 158. 158result[0] = ESC; 159. 159result[1] =97; 160. 160result[2] =1; 161. 161returnresult; 162. 162} 163. 163 164. 164 165. 165/** 166. 166 * 右对齐 167. 167 * @return 168. 168 */ 169. 169publicstaticbyte[] alignRight() 170. 170{ 171. 171byte[] result =newbyte[3]; 172. 172result[0] = ESC; 173. 173result[1] =97; 174. 174result[2] =2; 175. 175returnresult; 176. 176} 177. 177 178. 178 179. 179/** 180. 180 * 水平方向向右移动col列 181. 181 * @param col 182. 182 * @return 183. 183 */ 184. 184publicstaticbyte[] set_HT_position(bytecol ) 185. 185{ 186. 186byte[] result =newbyte[4]; 187. 187result[0] = ESC; 188. 188result[1] =68; 189. 189result[2] = col; 190. 190result[3] =0; 191. 191returnresult; 192. 192} 193. 193//------------------------字体变大----------------------------- 194. 194 195. 195 196. 196/** 197. 197 * 字体变大为标准的n倍 198. 198 * @param num 199. 199 * @return 200. 200 */ 201. 201publicstaticbyte[] fontSizeSetBig(intnum) 202. 202{ 203. 203byterealSize =0; 204. 204switch(num) 205. 205{ 206. 206case1: 207. 207realSize =0;break; 208. 208case2: 209. 209realSize =17;break; 210. 210case3: 211. 211realSize =34;break; 212. 212case4: 213. 213realSize =51;break; 214. 214case5: 215. 215realSize =68;break; 216. 216case6: 217. 217realSize =85;break; 218. 218case7: 219. 219realSize =102;break; 220. 220case8: 221. 221realSize =119;break; 222. 222} 223. 223byte[] result =newbyte[3]; 224. 224result[0] =29; 225. 225result[1] =33; 226. 226result[2] = realSize; 227. 227returnresult; 228. 228} 229. 229 230. 230 231. 231//------------------------字体变小----------------------------- 232. 232 233. 233 234. 234/** 235. 235 * 字体取消倍宽倍高 236. 236 * @param num 237. 237 * @return 238. 238 */ 239. 239publicstaticbyte[] fontSizeSetSmall(intnum) 240. 240{ 241. 241byte[] result =newbyte[3]; 242. 242result[0] = ESC; 243. 243result[1] =33; 244. 244 245. 245returnresult; 246. 246} 247. 247 248. 248 249. 249//------------------------切纸----------------------------- 250. 250 251. 251 252. 252/** 253. 253 * 进纸并全部切割 254. 254 * @return 255. 255 */ 256. 256publicstaticbyte[] feedPaperCutAll() 257. 257{ 258. 258byte[] result =newbyte[4]; 259. 259result[0] = GS; 260. 260result[1] =86; 261. 261result[2] =65; 262. 262result[3] =0; 263. 263returnresult; 264. 264} 265. 265 266. 266 267. 267/** 268. 268 * 进纸并切割(左边留一点不切) 269. 269 * @return 270. 270 */ 271. 271publicstaticbyte[] feedPaperCutPartial() 272. 272{ 273. 273byte[] result =newbyte[4]; 274. 274result[0] = GS; 275. 275result[1] =86; 276. 276result[2] =66; 277. 277result[3] =0; 278. 278returnresult; 279. 279} 280. 280 281. 281 282. 282//------------------------切纸----------------------------- 283. 283publicstaticbyte[] byteMerger(byte[] byte_1,byte[] byte_2){ 284. 284byte[] byte_3 =newbyte[byte_1.length+byte_2.length]; 285. 285System.arraycopy(byte_1,0, byte_3,0, byte_1.length); 286. 286System.arraycopy(byte_2,0, byte_3, byte_1.length, byte_2.length); 287. 287returnbyte_3; 288. 288} 289. 289 290. 290 291. 291publicstaticbyte[] byteMerger(byte[][] byteList){ 292. 292 293. 293intlength =0; 294. 294for(inti=0;i<byteList.length;i++) 295. 295{ 296. 296length += byteList[i].length; 297. 297} 298. 298byte[] result =newbyte[length]; 299. 299 300. 300intindex =0; 301. 301for(inti=0;i<byteList.length;i++) 302. 302{ 303. 303byte[] nowByte = byteList[i]; 304. 304for(intk=0;k<byteList[i].length;k++) 305. 305{ 306. 306result[index] = nowByte[k]; 307. 307index++; 308. 308} 309. 309} 310. 310for(inti =0;i<index;i++) 311. 311{ 312. 312CommonUtils.LogWuwei('','result['+i+'] is '+result[i]); 313. 313} 314. 314returnresult; 315. 315} 316. 316 317. 317 318. 318 319. 319}

工具库包括字体加粗、字体放大缩小、切纸、换行等基本操作,还有就是对byte数组的拼接,byte数组的拼接有很多方式,只实现了一种方式

效果图:

简单的代码解析:

view source print ? 01. publicbyte[] clientPaper() 02. { 03. 04. try{ 05. byte[] next2Line = printerCmdUtils.nextLine(2); 06. byte[] title ='出餐单(午餐)**万通中心店'.getBytes('gb2312'); 07. 08. byte[] boldOn = printerCmdUtils.boldOn(); 09. byte[] fontSize2Big = printerCmdUtils.fontSizeSetBig(3); 10. byte[] center= printerCmdUtils.alignCenter(); 11. byte[] Focus ='网 507'.getBytes('gb2312'); 12. byte[] boldOff = printerCmdUtils.boldOff(); 13. byte[] fontSize2Small = printerCmdUtils.fontSizeSetSmall(3); 14. 15. byte[] left= printerCmdUtils.alignLeft(); 16. byte[] orderSerinum ='订单编号:11234'.getBytes('gb2312'); 17. boldOn = printerCmdUtils.boldOn(); 18. byte[] fontSize1Big = printerCmdUtils.fontSizeSetBig(2); 19. byte[] FocusOrderContent ='韭菜鸡蛋饺子-小份(单)'.getBytes('gb2312'); 20. boldOff = printerCmdUtils.boldOff(); 21. byte[] fontSize1Small = printerCmdUtils.fontSizeSetSmall(2); 22. 23. 24. next2Line = printerCmdUtils.nextLine(2); 25. 26. byte[] priceInfo ='应收:22元 优惠:2.5元 '.getBytes('gb2312'); 27. byte[] nextLine = printerCmdUtils.nextLine(1); 28. 29. byte[] priceShouldPay ='实收:19.5元'.getBytes('gb2312'); 30. nextLine = printerCmdUtils.nextLine(1); 31. 32. byte[] takeTime ='取餐时间:2015-02-13 12:51:59'.getBytes('gb2312'); 33. nextLine = printerCmdUtils.nextLine(1); 34. byte[] setOrderTime ='下单时间:2015-02-13 12:35:15'.getBytes('gb2312'); 35. 36. byte[] tips_1 ='微信关注'**'自助下单每天免1元'.getBytes('gb2312'); 37. nextLine = printerCmdUtils.nextLine(1); 38. byte[] tips_2 ='饭后点评再奖5毛'.getBytes('gb2312'); 39. nextLine = printerCmdUtils.nextLine(1); 40. 41. byte[] breakPartial = printerCmdUtils.feedPaperCutPartial(); 42. 43. byte[][] cmdBytes= { 44. title,nextLine, 45. center,boldOn,fontSize2Big,Focus,boldOff,fontSize2Small,next2Line, 46. left,orderSerinum,nextLine, 47. center,boldOn,fontSize1Big,FocusOrderContent,boldOff,fontSize1Small,nextLine, 48. left,next2Line, 49. priceInfo,nextLine, 50. priceShouldPay,next2Line, 51. takeTime,nextLine, 52. setOrderTime,next2Line, 53. center,tips_1,nextLine, 54. center,tips_2,next2Line, 55. breakPartial 56. }; 57. 58. returnprinterCmdUtils.byteMerger(cmdBytes); 59. 60. }catch(UnsupportedEncodingException e) { 61. // TODO Auto-generated catch block 62. e.printStackTrace(); 63. } 64. returnnull; 65. }

字节码拼接过程如下:

view source print ? 01. 1publicstaticbyte[] byteMerger(byte[][] byteList){ 02. 2 03. 3intlength =0; 04. 4for(inti=0;i<byteList.length;i++) 05. 5{ 06. 6length += byteList[i].length; 07. 7} 08. 8byte[] result =newbyte[length]; 09. 9 10. 10intindex =0; 11. 11for(inti=0;i<byteList.length;i++) 12. 12{ 13. 13byte[] nowByte = byteList[i]; 14. 14for(intk=0;k<byteList[i].length;k++) 15. 15{ 16. 16result[index] = nowByte[k]; 17. 17index++; 18. 18} 19. 19} 20. 20for(inti =0;i<index;i++) 21. 21{ 22. 22CommonUtils.LogWuwei('','result['+i+'] is '+result[i]); 23. 23} 24. 24returnresult; 25. 25}

将上步得到的字节码通过可写流写入套接字,这时小票打印机就会打印对应的内容。

1、demo链接地址如下:

http://pan.baidu.com/s/1eQ9y8mQ

更多相关文章

  1. Android使用TextView实现无下划线超链接 | 萝卜白菜的博客
  2. Android(安卓)IoT开发实战 | 03 - Android日志工具Log的使用
  3. Android调用打印机
  4. 在 Kotlin 中使用 Logger 打印 OkHttp 网络请求返回的 Logcat 日
  5. Android不同版本获取当前wifi信息方法
  6. Android遇上打印机
  7. Android(安卓)调用shell命令打印
  8. Android(安卓)定义全局LOG打印日志,并显示调用者相关信息
  9. android 加下划线

随机推荐

  1. SQL Server导入、导出、备份数据方法
  2. .NET Framework SQL Server 数据提供程序
  3. 对有自增长字段的表导入数据注意事项
  4. mssql自动备份及自动清除日志文件服务器
  5. Sql 语句学习指南第1/2页
  6. MSSQL 清空数据库的方法
  7. SQL Select语句完整的执行顺序
  8. SQL Server 海量数据导入的最快方法
  9. 一条SQL语句搞定Sql2000 分页
  10. 批量执行sql语句的方法