亲宝软件园·资讯

展开

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

samile6899 人气:0

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

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

知道了相应的使用方法,下面的操作也就很简单了。其实具体的原理方法也就是先使用软件设计出两张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项目则必须要注意到这一点。

加载全部内容

相关教程
猜你喜欢
用户评论