專用讀卡機之間的溝通,架構圖如下:
1. 環境說明
OS: win7 Pro
IDE:Netbean 8.2
Server: Glass Fish
Java 8
Cshis.dll 3.3版
JNA 請到這裡下載
讀卡機為HC-3000
2.要達成本篇功能,除了宏堡科技 HC-3000外,仍需要安全模組卡、醫事人員卡、健保VPN
網路,以上設備感謝相關人士幫助,此為本人自行研究,歡迎轉載,但禁止商用。
3.請先進行以下3個動作。
3.1 切換到File,建立一個lib資料夾。
3.2 把Cshis.dll丟到專下裡。
3.3 把jna.jar加入到Library中。
4.先來寫一下JavaBean,新建一個ReadBean.java
package com.test.readcard; import com.sun.jna.Library; import com.sun.jna.Native; import com.sun.jna.ptr.IntByReference; import java.io.UnsupportedEncodingException; import java.util.Arrays; import java.util.logging.Level; import java.util.logging.Logger; public class ReadBean implements java.io.Serializable { public ReadBean(){} String Basic; public interface CLibrary extends Library { /* CLibrary INSTANCE=(CLibrary) Native.loadLibrary((Platform.isWindows()? "msvcrt":"c"),CLibrary.class); void printf(String fromat,Object... args);*/ CLibrary INSTANCE=(CLibrary) Native.loadLibrary(("CsHis"),CLibrary.class); //開啟COM Port public int csOpenCom(int comport); //關閉COM Port public int csCloseCom(); //驗證安全模組卡 public int csVerifySAMDC(); //驗證醫事人員卡密碼 public int hpcVerifyHPCPIN(); //取得健保卡基本資料 public int hisGetBasicData(byte[] buffer, IntByReference size); } public String getBasic() { //Init 需要用到的class basicStruct bs=new basicStruct();; HisGetTreatmentNeedHPCStruct hn=new HisGetTreatmentNeedHPCStruct(); //開啟開COM Port (我是接在COM1) int err=CLibrary.INSTANCE.csOpenCom(0); //驗證安全模組卡 CLibrary.INSTANCE.csVerifySAMDC(); //驗證醫事人員卡 CLibrary.INSTANCE.hpcVerifyHPCPIN(); //取得基本資料 err=CLibrary.INSTANCE.hisGetBasicData(bs.basicdatabuffer, bs.size); StringBuilder sb=new StringBuilder(); if (err==0) { byte[] tmp=Arrays.copyOfRange(bs.basicdatabuffer,0,11); try { String cardno = new String(tmp, "UTF-8"); tmp=Arrays.copyOfRange(bs.basicdatabuffer,12,20); String name = new String(tmp, "big5"); tmp=Arrays.copyOfRange(bs.basicdatabuffer,32,42); String pid = new String(tmp, "big5"); tmp=Arrays.copyOfRange(bs.basicdatabuffer,42,49); String birth = new String(tmp, "big5"); System.out.println(cardno); System.out.println(name); System.out.println(pid); System.out.println(birth); sb.append(cardno+","); sb.append(name+","); sb.append(pid+","); sb.append(birth); } catch (UnsupportedEncodingException ex) { Logger.getLogger(ReadBean.class.getName()).log(Level.SEVERE, null, ex); } } err=CLibrary.INSTANCE.csCloseCom(); Basic= sb.toString(); return Basic; } } class basicStruct { byte[] basicdatabuffer = new byte[72]; IntByReference size=new IntByReference(72); } class HisGetTreatmentNeedHPCStruct { byte[] basicdatabuffer = new byte[498]; IntByReference size=new IntByReference(498); }
注意一件事,JavaBean一定要有Package Name,不然JSP會找不到。
接下來是JSP的部份
<%--
Document : Read
Created on : 2015/3/21, 上午 10:54:57
Author : bert
--%>
<jsp:useBean id="ReadBean" class="com.test.readcard.ReadBean" scope="page"/>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Read Card Test</title>
</head>
<body> <% String message;
message=ReadBean.getBasic();
String temp[]=message.split(",");
%>
卡號:<%out.print(temp[0]);%>
姓名:<%out.print(temp[1]);%>
身份證:<%out.print(temp[2]);%>
生日:<%out.print(temp[3]);%>
</body>
</html>
注意一件事,因為這只是一個實驗性質,所以我把認證安全模組卡以及醫事人員卡寫在一起,所以每次都會去認證,會比較花時間,如果有需要的人,請注意一下這一點。
執行結果(我用黑筆隱形部份個資)
沒有留言:
張貼留言