最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java調(diào)用外接設(shè)備詳解(制卡機)

 更新時間:2018年07月19日 11:30:03   作者:samile6899  
這篇文章主要為大家詳細介紹了Java調(diào)用外接設(shè)備的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下

最近在開發(fā)的系統(tǒng)中,有一個接入外接設(shè)備制卡機更換,之前用的制卡機要重新采購,所以導(dǎo)致之前的調(diào)用接口方法無效,要用新的設(shè)備接口來進行調(diào)用??墒沁@個設(shè)備是外國進口過來的,資料很少。想要百度的話,網(wǎng)上資料都沒有,中間經(jīng)過了幾天的摸索,最終還是弄了出來,所以把詳細的分析及解決方法記錄下來。

這個制卡機是打印磁卡/智能卡的,像我們常用的銀行卡、飯卡、購物卡都可以用這個機器來進行設(shè)計并制卡,并最終發(fā)放給消費者使用。簡要說下解決的思路吧,最開始拿到這個機器的時候,我是一頭的霧水,也不知道從何下手,我當時最最簡單的想法就是先把這個機器怎么使用,怎么操作,什么用途了解清楚,然后再談接口代碼啥的。但是找了半天,網(wǎng)上的資料也很少,也不知道怎么使用,最后看到機器上有一個二維碼幫助的,掃了下,進入到了它們的官網(wǎng),找到了相應(yīng)的機器型號,然后的話看到了相應(yīng)的使用說明和相應(yīng)的視頻教程,了解了一番,這才知道了產(chǎn)品正確的使用方法,所以拿到一個新玩意,想要快速了解它,最好的還是先去看他的說明書和官方教程。

知道了相應(yīng)的使用方法,下面的操作也就很簡單了。其實具體的原理方法也就是先使用軟件設(shè)計出兩張BMP的圖片,一面是磁卡/智能卡的正面,一般的話都是張設(shè)計的好看的圖片,另一張的話就是磁卡/智能卡的反面,然后圖片的給相應(yīng)的代碼解析,讀取,把信息寫入到卡的磁條上,最后調(diào)用機器進行打印,用其中機器的墨紙寫入到空白的卡片上就可以了。

但是一般人工都是機器自帶的軟件提前設(shè)計好圖片,如果換做用代碼驅(qū)動機器的話,那就要用代碼要生成圖片了。當然了,在JAVA中有相應(yīng)的類來生成BMP格式的圖片,然后調(diào)用相應(yīng)的代碼就可以了。

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中設(shè)置ListOfOtherInfoModel,以及醫(yī)院和機關(guān)列表
 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;
  }
  //======關(guān)閉連接 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,寫入磁卡數(shù)據(jù)(卡號)
   //寫數(shù)據(jù)操作
   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;   //傳入的第一個參數(shù)
 container.track2Data = cardData;  //傳入的第二個參數(shù)
 container.track3Data = cardData;  //傳入的第三個參數(shù)
 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中設(shè)置ListOfOtherInfoModel,以及醫(yī)院和機關(guān)列表
  try {
  //1,打印姓名和卡號 
  //======開啟連接 START======
  printers = UsbDiscoverer.getZebraUsbPrinters();
  if (printers.length > 0) {
   connection = printers[0].getConnection();
 
   connection.open();
   zebraCardPrinter = ZebraCardPrinterFactory.getInstance(connection);
  }
 
  if (zebraCardPrinter == null) {
   return;
  }
  //======關(guān)閉連接 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,寫入磁卡數(shù)據(jù)(卡號)
  //寫數(shù)據(jù)操作
  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調(diào)用的后臺請求邏輯代碼,在JSP頁面可以用AJAX請求,然后執(zhí)行相應(yīng)的代碼塊進行相應(yīng)的執(zhí)行代碼進行調(diào)用制卡機外接設(shè)備。值得一提的是,由于在調(diào)用相應(yīng)的外接設(shè)備的時候,會調(diào)用到相應(yīng)的.dll擴展文件,這個在使用TOMCAT/WebSpare的時候,需要把.dll文件放到bin目錄下,不然的話可能會調(diào)用失敗。這一點要特別注意,因為在SDK中,java程序的話可能是執(zhí)行通過的,但是java web項目則必須要注意到這一點。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • IDEA高效查看源碼的快捷鍵及小技巧

    IDEA高效查看源碼的快捷鍵及小技巧

    本篇文章這一部分的內(nèi)容主要為大家介紹了一些平時看源碼的時候常用的快捷鍵/小技巧!非常好用!掌握這些快捷鍵/小技巧,看源碼的效率提升一個等級
    2022-01-01
  • java?CompletableFuture異步任務(wù)編排示例詳解

    java?CompletableFuture異步任務(wù)編排示例詳解

    這篇文章主要為大家介紹了java?CompletableFuture異步任務(wù)編排示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11
  • Nacos框架與原理之Nacos的參數(shù)

    Nacos框架與原理之Nacos的參數(shù)

    這篇文章主要介紹了Nacos框架與原理之Nacos的參數(shù),Nacos?中的參數(shù)有很多,如:命名空間、分組名、服務(wù)名、保護閾值、服務(wù)路由類型、臨時實例等,但下面文章我們要講解的是參數(shù),參數(shù)是什么呢,下面一起進去文章學(xué)習(xí)詳細內(nèi)容吧
    2022-05-05
  • Java獲取中文拼音、中文首字母縮寫和中文首字母的示例

    Java獲取中文拼音、中文首字母縮寫和中文首字母的示例

    本文主要介紹了Java獲取中文拼音、中文首字母縮寫和中文首字母,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2016-10-10
  • 實例解析Java單例模式編程中對抽象工廠模式的運用

    實例解析Java單例模式編程中對抽象工廠模式的運用

    這篇文章主要介紹了實例解析Java單例模式編程中對抽象工廠模式的運用,抽象工廠模式可以看作是工廠方法模式的升級版,本需要的朋友可以參考下
    2016-02-02
  • spring boot中的properties參數(shù)配置詳解

    spring boot中的properties參數(shù)配置詳解

    這篇文章主要介紹了spring boot中的properties參數(shù)配置,需要的朋友可以參考下
    2017-09-09
  • Java應(yīng)用EasyExcel工具類

    Java應(yīng)用EasyExcel工具類

    這篇文章主要介紹了Java應(yīng)用EasyExcel工具類,文中有非常詳細的代碼示例,對正在學(xué)習(xí)java的小伙伴們有一定的幫助,需要的朋友可以參考下
    2021-05-05
  • java連接mysql數(shù)據(jù)庫及測試是否連接成功的方法

    java連接mysql數(shù)據(jù)庫及測試是否連接成功的方法

    這篇文章主要介紹了java連接mysql數(shù)據(jù)庫及測試是否連接成功的方法,結(jié)合完整實例形式分析了java基于jdbc連接mysql數(shù)據(jù)庫并返回連接狀態(tài)的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下
    2017-09-09
  • mac 安裝java1.8的過程詳解

    mac 安裝java1.8的過程詳解

    這篇文章主要介紹了mac 安裝java1.8,包括下載過程及配置環(huán)境相關(guān)知識介紹,本文結(jié)合實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-09-09
  • java利用jacob將word轉(zhuǎn)pdf

    java利用jacob將word轉(zhuǎn)pdf

    這篇文章主要為大家詳細介紹了java利用jacob將word轉(zhuǎn)pdf,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-12-12

最新評論

平山县| 大理市| 九龙县| 佛学| 泾源县| 灵川县| 远安县| 金昌市| 师宗县| 阿瓦提县| 平顺县| 株洲市| 肥西县| 沙雅县| 舞钢市| 时尚| 阿图什市| 资溪县| 柞水县| 尼木县| 鄂托克旗| 铜山县| 江门市| 小金县| 平塘县| 沁阳市| 桃源县| 洱源县| 莒南县| 永新县| 皋兰县| 福州市| 麟游县| 顺昌县| 临江市| 海南省| 尚志市| 岳阳市| 西宁市| 阳山县| 柳河县|