Java调用外接设备详解(制卡机)

时间:2022-08-26 21:28:55

最近在开发的系统中,有一个接入外接设备制卡机更换,之前用的制卡机要重新采购,所以导致之前的调用接口方法无效,要用新的设备接口来进行调用。可是这个设备是外国进口过来的,资料很少。想要百度的话,网上资料都没有,中间经过了几天的摸索,最终还是弄了出来,所以把详细的分析及解决方法记录下来。

这个制卡机是打印磁卡/智能卡的,像我们常用的银行卡、饭卡、购物卡都可以用这个机器来进行设计并制卡,并最终发放给消费者使用。简要说下解决的思路吧,最开始拿到这个机器的时候,我是一头的雾水,也不知道从何下手,我当时最最简单的想法就是先把这个机器怎么使用,怎么操作,什么用途了解清楚,然后再谈接口代码啥的。但是找了半天,网上的资料也很少,也不知道怎么使用,最后看到机器上有一个二维码帮助的,扫了下,进入到了它们的官网,找到了相应的机器型号,然后的话看到了相应的使用说明和相应的视频教程,了解了一番,这才知道了产品正确的使用方法,所以拿到一个新玩意,想要快速了解它,最好的还是先去看他的说明书和官方教程。

Java调用外接设备详解(制卡机)

知道了相应的使用方法,下面的操作也就很简单了。其实具体的原理方法也就是先使用软件设计出两张bmp的图片,一面是磁卡/智能卡的正面,一般的话都是张设计的好看的图片,另一张的话就是磁卡/智能卡的反面,然后图片的给相应的代码解析,读取,把信息写入到卡的磁条上,最后调用机器进行打印,用其中机器的墨纸写入到空白的卡片上就可以了。

但是一般人工都是机器自带的软件提前设计好图片,如果换做用代码驱动机器的话,那就要用代码要生成图片了。当然了,在java中有相应的类来生成bmp格式的图片,然后调用相应的代码就可以了。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import java.awt.color;
import java.awt.font;
import java.awt.graphics2d;
import java.awt.image;
import java.awt.image.bufferedimage;
import java.io.bytearrayoutputstream;
import java.io.ioexception;
import java.util.arraylist;
import java.util.list;
 
import javax.imageio.imageio;
import javax.servlet.servletexception;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import javax.swing.imageicon;
import javax.swing.jtextarea;
 
import net.sf.json.jsonobject;
 
import org.apache.commons.io.ioutils;
import org.apache.struts.action.actionform;
import org.apache.struts.action.actionforward;
import org.apache.struts.action.actionmapping;
import com.wonders.yksx.jdlk.web.util.responseutil;
import com.wonders.yksx.salvation.web.action.salvationactionparent;
import com.wonders.yksx.ylbk.web.form.magencodecontainer;
import com.wonders.yksx.ylbk.web.form.magencodemodel;
import com.wonders.yksx.ylbk.web.form.printermodel;
import com.zebra.sdk.comm.connection;
import com.zebra.sdk.comm.connectionexception;
import com.zebra.sdk.common.card.containers.graphicsinfo;
import com.zebra.sdk.common.card.containers.jobstatusinfo;
import com.zebra.sdk.common.card.enumerations.cardside;
import com.zebra.sdk.common.card.enumerations.graphictype;
import com.zebra.sdk.common.card.enumerations.orientationtype;
import com.zebra.sdk.common.card.enumerations.printtype;
import com.zebra.sdk.common.card.exceptions.zebracardexception;
import com.zebra.sdk.common.card.graphics.zebracardgraphics;
import com.zebra.sdk.common.card.graphics.zebragraphics;
import com.zebra.sdk.common.card.graphics.enumerations.rotationtype;
import com.zebra.sdk.common.card.printer.zebracardprinter;
import com.zebra.sdk.common.card.printer.zebracardprinterfactory;
import com.zebra.sdk.device.zebraillegalargumentexception;
import com.zebra.sdk.printer.discovery.discoveredprinter;
import com.zebra.sdk.printer.discovery.usbdiscoverer;
 
public class makecardvolumnaction extends salvationactionparent {
 
 discoveredprinter[] printers = null;
 connection connection = null;
 zebracardprinter zebracardprinter = null;
 zebracardgraphics graphics = null;
 bytearrayoutputstream baos = null;
 protected static jtextarea statustextarea;
 
 public actionforward execute(actionmapping mapping, actionform form,
  httpservletrequest request, httpservletresponse response)
  throws ioexception, servletexception {
 
 string personname = string.valueof(new string(request.getparameter("name").getbytes("gbk"),"utf-8"));
 string cardno = string.valueof(request.getparameter("cardno"));
 string carddata = string.valueof(request.getparameter("carddata"));
 boolean flag = false;
 
 // 在session中设置listofotherinfomodel,以及医院和机关列表
 if(!personname.equals("") && !personname.equals("null")
  && !cardno.equals("") && !cardno.equals("null")
    && !carddata.equals("") && !carddata.equals("null")){
  
  jsonobject count = new jsonobject();
  try {
  //1,打印姓名和卡号
  //======开启连接 start======
  printers = usbdiscoverer.getzebrausbprinters();
  if (printers.length > 0) {
   connection = printers[0].getconnection();
 
   connection.open();
   zebracardprinter = zebracardprinterfactory.getinstance(connection);
  }
 
  if (zebracardprinter == null) {
   flag = false;
  }else{
   flag = true;
  }
  //======关闭连接 start======
  if(flag){
   list<graphicsinfo> graphicsdata = new arraylist<graphicsinfo>();
   
   bufferedimage img = null;
   graphicsinfo grinfo = new graphicsinfo();
 
   graphics = new zebracardgraphics(zebracardprinter);
   graphics.initialize(0, 0, orientationtype.landscape, printtype.monok, color.white);
 
   // front monok  正面
   grinfo.side = cardside.front;
   grinfo.printtype = printtype.monok;
   grinfo.graphictype = graphictype.bmp;
   
   img = getbufferedimage("c:\\",personname,cardno);
   
   baos = new bytearrayoutputstream();
   imageio.write(img, "bmp", baos);
 
   graphics.drawtext("aaaaa", 10, 20, null);
   
   grinfo.graphicdata = graphics.createimage(null);
   graphics.clear();
 
   graphicsdata.add(grinfo);
 
   // back monok  反面
   graphics.initialize(0, 0, orientationtype.landscape, printtype.monok, color.white);
 
   grinfo = new graphicsinfo();
   grinfo.side = cardside.back;
   grinfo.printtype = printtype.monok;
   grinfo.graphictype = graphictype.bmp;
 
   img = getbufferedimage("c:\\",personname,cardno);
   baos = new bytearrayoutputstream();
   imageio.write(img, "bmp", baos);
 
   graphics.drawimage(baos.tobytearray(), 0, 0, 0, 0, rotationtype.rotatenoneflipnone);
   grinfo.graphicdata = graphics.createimage(null);
   graphics.clear();
 
   graphicsdata.add(grinfo);
 
   int jobid = zebracardprinter.print(1, graphicsdata);
   polljobstatus(zebracardprinter, jobid);
 
   //jobstatusinfo jstatus = zebracardprinter.getjobstatus(jobid);
   zebracardprinter.getjobstatus(jobid);
   
   //2,写入磁卡数据(卡号)
   //写数据操作
   magencodecontainer magencodecontainer = buildmagencodecontainer(carddata);
   new magencodemodel().magencode(zebracardprinter, magencodecontainer, statustextarea);
   
   flag = true;
  }
  } catch (exception ex) {
  if(!flag){
   count.put("cardcount",0);
  }
  } finally {
  ioutils.closequietly(baos);
  cleanup(connection, zebracardprinter, graphics);
  
  printermodel.cleanupquietly(zebracardprinter, connection);
  
  if(!flag){
   count.put("cardcount",0);
  }else if(flag){
   count.put("cardcount",1);
  }
  }
  
  try {
  responseutil.writetojson(count, response);
  } catch (exception e) {
  e.printstacktrace();
  }
  
 }
 return null;
 }
 
 static boolean polljobstatus(zebracardprinter device, int actionid) throws connectionexception, zebracardexception, zebraillegalargumentexception {
 boolean success = false;
 long dropdeadtime = system.currenttimemillis() + 40000;
 long pollinterval = 500;
 
 // poll job status
 jobstatusinfo jstatus = null;
 
 do {
  jstatus = device.getjobstatus(actionid);
  system.out.println(string.format("job %d, status:%s, card position:%s, " + "readyfornextjob:%s, mag status:%s, contact status:%s, contactless status:%s, " + "error code:%d, alarm code:%d",
   actionid, jstatus.printstatus, jstatus.cardposition, jstatus.readyfornextjob, jstatus.magneticencoding, jstatus.contactsmartcard, jstatus.contactlesssmartcard,
   jstatus.errorinfo.value, jstatus.alarminfo.value));
 
  if (jstatus.contactsmartcard.contains("station")) {
  success = true;
  break;
  } else if (jstatus.contactlesssmartcard.contains("station")) {
  success = true;
  break;
  } else if (jstatus.printstatus.contains("done_ok")) {
  success = true;
  break;
  } else if (jstatus.printstatus.contains("alarm_handling")) {
  system.out.println("error dectected: " + jstatus.alarminfo.description);
  success = false;
  } else if (jstatus.printstatus.contains("error") || jstatus.printstatus.contains("cancelled")) {
  success = false;
  break;
  }
 
  if (system.currenttimemillis() > dropdeadtime) {
  success = false;
  break;
  }
 
  try {
  thread.sleep(pollinterval);
  } catch (interruptedexception e) {
  e.printstacktrace();
  }
 
 } while (true);
 
 return success;
 }
 
 protected static void cleanup(connection connection, zebracardprinter genericprinter, zebragraphics graphics) {
 try {
  if (genericprinter != null) {
  genericprinter.destroy();
  }
 } catch (zebracardexception e) {
  e.printstacktrace();
 }
 
 if (graphics != null) {
  graphics.close();
 }
 
 if (connection != null) {
  try {
  connection.close();
  } catch (connectionexception e) {
  e.printstacktrace();
  }
 }
 }
 
 private static magencodecontainer buildmagencodecontainer(string carddata) {
 magencodecontainer container = new magencodecontainer();
 container.cardsource = "feeder";
 container.carddestination = "eject";
 container.coercivitytype = "high";
 container.verify = true;
 container.track1data = carddata;   //传入的第一个参数
 container.track2data = carddata;  //传入的第二个参数
 container.track3data = carddata;  //传入的第三个参数
 container.quantity = 1;
 return container;
 }
 
 public static bufferedimage getbufferedimage(string path,string name,string cardno){
 bufferedimage bimage = null;
 try{
  imageicon imgicon = new imageicon(path);
  image theimg = imgicon.getimage();
  int width = theimg.getwidth(null) == -1 ? 1023 : theimg.getwidth(null);
  int height = theimg.getheight(null) == -1 ? 639 : theimg.getheight(null);
  bimage = new bufferedimage(width,height,bufferedimage.type_int_rgb);
  graphics2d g = bimage.creategraphics();
  g.setcolor(color.white);
  g.fillrect(0, 0, 1023, 639);
  g.drawimage(theimg, 0, 0, null);
  g.setcolor(color.blue);
  g.setfont(new font("arial",font.bold,60));
  g.drawstring(name,130,260);  //姓名
  
  g.setcolor(color.blue);
  g.setfont(new font("arial",font.bold,60));
  g.drawstring(cardno, 600, 260);  //卡号
  
  g.dispose();
 }catch (exception e) {
  e.printstacktrace();
 }
 return bimage;
 }
 
 
 public static void main(string[] args) {
 discoveredprinter[] printers = null;
 connection connection = null;
 zebracardprinter zebracardprinter = null;
 zebracardgraphics graphics = null;
 bytearrayoutputstream baos = null;
 jtextarea statustextareas = null;
 
 // 在session中设置listofotherinfomodel,以及医院和机关列表
  try {
  //1,打印姓名和卡号
  //======开启连接 start======
  printers = usbdiscoverer.getzebrausbprinters();
  if (printers.length > 0) {
   connection = printers[0].getconnection();
 
   connection.open();
   zebracardprinter = zebracardprinterfactory.getinstance(connection);
  }
 
  if (zebracardprinter == null) {
   return;
  }
  //======关闭连接 start======
  list<graphicsinfo> graphicsdata = new arraylist<graphicsinfo>();
 
  bufferedimage img = null;
  graphicsinfo grinfo = new graphicsinfo();
 
  graphics = new zebracardgraphics(zebracardprinter);
  graphics.initialize(0, 0, orientationtype.landscape, printtype.monok, color.white);
 
  // front monok  正面
  grinfo.side = cardside.front;
  grinfo.printtype = printtype.monok;
  grinfo.graphictype = graphictype.bmp;
  
  img = getbufferedimage("e:\\","brush","111033333");
  
  baos = new bytearrayoutputstream();
  imageio.write(img, "bmp", baos);
 
  graphics.drawtext("aaaaa", 10, 20, null);
  
  grinfo.graphicdata = graphics.createimage(null);
  graphics.clear();
 
  graphicsdata.add(grinfo);
 
  // back monok  反面
  graphics.initialize(0, 0, orientationtype.landscape, printtype.monok, color.white);
 
  grinfo = new graphicsinfo();
  grinfo.side = cardside.back;
  grinfo.printtype = printtype.monok;
  grinfo.graphictype = graphictype.bmp;
 
  img = getbufferedimage("e:\\","brush","111033333");
  baos = new bytearrayoutputstream();
  imageio.write(img, "bmp", baos);
 
  graphics.drawimage(baos.tobytearray(), 0, 0, 0, 0, rotationtype.rotatenoneflipnone);
  grinfo.graphicdata = graphics.createimage(null);
  graphics.clear();
 
  graphicsdata.add(grinfo);
 
  int jobid = zebracardprinter.print(1, graphicsdata);
  polljobstatus(zebracardprinter, jobid);
 
  jobstatusinfo jstatus = zebracardprinter.getjobstatus(jobid);
  
  //2,写入磁卡数据(卡号)
  //写数据操作
  magencodecontainer magencodecontainer = buildmagencodecontainer("111033333");
  new magencodemodel().magencode(zebracardprinter, magencodecontainer, statustextareas);
  
  } catch (exception ex) {
  } finally {
  ioutils.closequietly(baos);
  cleanup(connection, zebracardprinter, graphics);
  
  printermodel.cleanupquietly(zebracardprinter, connection);
  }
 }
}

上面的代码是jsp调用的后台请求逻辑代码,在jsp页面可以用ajax请求,然后执行相应的代码块进行相应的执行代码进行调用制卡机外接设备。值得一提的是,由于在调用相应的外接设备的时候,会调用到相应的.dll扩展文件,这个在使用tomcat/webspare的时候,需要把.dll文件放到bin目录下,不然的话可能会调用失败。这一点要特别注意,因为在sdk中,java程序的话可能是执行通过的,但是java web项目则必须要注意到这一点。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/samile6899/article/details/81088664