您好,登錄后才能下訂單哦!
最近在開發的系統中,有一個接入外接設備制卡機更換,之前用的制卡機要重新采購,所以導致之前的調用接口方法無效,要用新的設備接口來進行調用。可是這個設備是外國進口過來的,資料很少。想要百度的話,網上資料都沒有,中間經過了幾天的摸索,最終還是弄了出來,所以把詳細的分析及解決方法記錄下來。
這個制卡機是打印磁卡/智能卡的,像我們常用的銀行卡、飯卡、購物卡都可以用這個機器來進行設計并制卡,并最終發放給消費者使用。簡要說下解決的思路吧,最開始拿到這個機器的時候,我是一頭的霧水,也不知道從何下手,我當時最最簡單的想法就是先把這個機器怎么使用,怎么操作,什么用途了解清楚,然后再談接口代碼啥的。但是找了半天,網上的資料也很少,也不知道怎么使用,最后看到機器上有一個二維碼幫助的,掃了下,進入到了它們的官網,找到了相應的機器型號,然后的話看到了相應的使用說明和相應的視頻教程,了解了一番,這才知道了產品正確的使用方法,所以拿到一個新玩意,想要快速了解它,最好的還是先去看他的說明書和官方教程。
知道了相應的使用方法,下面的操作也就很簡單了。其實具體的原理方法也就是先使用軟件設計出兩張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項目則必須要注意到這一點。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。