欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-13 19:54:10
最近在开发的系统中,有一个接入外接设备制卡机更换,之前用的制卡机要重新采购,所以导致之前的调用接口方法无效,要用新的设备接口来进行调用。可是这个设备是外国进口过来的,资料很...

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

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

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

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

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

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项目则必须要注意到这一点。

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