package com.demo.ReaderDemoForMagAndId;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.swing.*;
import sun.misc.*;
import com.sun.jna.Library;
import com.sun.jna.Native;
public class ReaderDemoForMagAndId extends JFrame implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
public interface GHC_IDREADER_API extends Library{
GHC_IDREADER_API INSTANCE=(GHC_IDREADER_API)Native.loadLibrary("libGetIDCardInfo", GHC_IDREADER_API.class);
//port 串口 赋值1-256
//baud 波特率支持 9600~115200
//extendCmd 拓展命令 暂时保留
//idCardInfo 身份证信息
//timeout 超过时间
//ifCreatePhoto 图像生成标志
int GetIdCardInfo(int port,int baud,byte[] extendCmd,byte[] idCardInfo,int timeout,long ifCreatePhoto);
}
GHC_IDREADER_API IDREADER_dll = GHC_IDREADER_API.INSTANCE;
public interface GHC_MAGRAEDER_API extends Library{
GHC_MAGRAEDER_API INSTANCE=(GHC_MAGRAEDER_API)Native.loadLibrary("ghc715", GHC_MAGRAEDER_API.class);
// 绝对路径在使用时要修改一下
//GHC_API INSTANCE=(GHC_API)Native.loadLibrary(
// "D:\\workspace\\MagCard715Reader\\src\\ghc715.dll", GHC_API.class);
//comport 串口 赋值1-256
//track 读的卡轨道号,赋值1-5 track=5 读2、3道
//model 机器类型,赋值1/2
//cmto 表示超时的时间,单位是毫秒
//Data1 二轨数据
//Date2 一轨或三轨数据
int readcard(int comport,int track,byte[] data1,byte[] data2,int model,long cmto);
int writecard(int comport,int track,byte[] data1,byte[] data2,int model,long cmto);
}
GHC_MAGRAEDER_API MAGREADER_dll = GHC_MAGRAEDER_API.INSTANCE;
int model = 2;
int cmto = 5000;
int ibaud = 115200; // 默认波特率
int itimeout = 3; // 3s
int iifCreatePhoto = 0; //默认不生成图片
byte[] iextendCmd = new byte[3]; //拓展命令为空
int ReadMagCard(int iPortNo, int track, byte[] data1, byte[] data2){
return MAGREADER_dll.readcard(iPortNo, track, data1, data2, model, cmto);
}
int WriteMagCard(int iPortNO, int track, byte[] data1, byte[] data2){
return MAGREADER_dll.writecard(iPortNO, track, data1, data2, model, cmto);
}
int ReadIDCard(int port , byte[] idCardInfo)
{
return IDREADER_dll.GetIdCardInfo(port, ibaud, iextendCmd, idCardInfo, itimeout, iifCreatePhoto);
}
private class WindowCloser extends WindowAdapter {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
}
//控件
JPanel MainPanelForIDReader = new JPanel();
JPanel IDCardInfo_Panel = new JPanel();
JTextArea IDCardInfo_Area = new JTextArea("", 11, 30);
ImageIcon ID_Pic = new ImageIcon("default.jpg");
JLabel Pic_Label = new JLabel(ID_Pic);
JPanel Btn_Panel = new JPanel();
JLabel PortLabel = new JLabel("端口号");
JTextField PortTextField = new JTextField("",6);
JButton ReadCard = new JButton("读卡");
JPanel MainPanelForMagReader = new JPanel();
JPanel RowSecondData = new JPanel(); //第二磁道显示
JLabel SecondLabel = new JLabel("第二磁道");
JTextArea SecondDataTextArea = new JTextArea("", 3, 40);
JPanel RowThirdData = new JPanel(); //第三磁道显示
JLabel ThirdLabel = new JLabel("第三磁道");
JTextArea ThirdDataTextArea = new JTextArea("", 3, 40);
JPanel RowBtn = new JPanel(); //按钮区域
JLabel MagPortLabel = new JLabel("端口号");
JTextField MagPortTextField = new JTextField("",6);
JButton ReadMagCard = new JButton("读卡");
JButton WriteMagCard = new JButton("写卡");
public ReaderDemoForMagAndId()
{
super("ReaderDemoForMagAndId");
//身份证界面显示
addWindowListener(new WindowCloser());
GridLayout MainPanelLayout = new GridLayout(2, 1, 3, 3);
setLayout(MainPanelLayout);
BoxLayout boxLayout = new BoxLayout(MainPanelForIDReader, BoxLayout.Y_AXIS);
MainPanelForIDReader.setLayout(boxLayout);
//控制身份证信息的位置
FlowLayout FL_Info = new FlowLayout(FlowLayout.CENTER);
IDCardInfo_Panel.setLayout(FL_Info);
IDCardInfo_Panel.add(IDCardInfo_Area);
Dimension Pic = new Dimension(150, 200);
Pic_Label.setPreferredSize(Pic);
IDCardInfo_Panel.add(Pic_Label);
MainPanelForIDReader.add(IDCardInfo_Panel);
//身份证端口号代码,读卡
FlowLayout BTN = new FlowLayout(FlowLayout.CENTER);
Btn_Panel.setLayout(BTN);
Btn_Panel.add(PortLabel);
Btn_Panel.add(PortTextField);
Btn_Panel.add(ReadCard);
MainPanelForIDReader.add(Btn_Panel);
//布局
GridLayout GridLayoutForMagReader = new GridLayout(3, 1, 3, 3);
MainPanelForMagReader.setLayout(GridLayoutForMagReader);
//第二磁道代码
FlowLayout FL02 = new FlowLayout(FlowLayout.CENTER);
RowSecondData.setLayout(FL02);
SecondDataTextArea.setEditable(true);
SecondDataTextArea.setLineWrap(true);
RowSecondData.add(SecondLabel);
RowSecondData.add(SecondDataTextArea);
MainPanelForMagReader.add(RowSecondData);
System.out.println(RowSecondData);
//第三磁道代码
FlowLayout FL03 = new FlowLayout(FlowLayout.CENTER);
RowThirdData.setLayout(FL03);
ThirdDataTextArea.setEditable(true);
ThirdDataTextArea.setLineWrap(true);
RowThirdData.add(ThirdLabel);
RowThirdData.add(ThirdDataTextArea);
MainPanelForMagReader.add(RowThirdData);
System.out.println(RowThirdData);
//端口号,读卡写卡
FlowLayout FL = new FlowLayout(FlowLayout.CENTER);
RowBtn.setLayout(FL);
RowBtn.add(MagPortLabel);
RowBtn.add(MagPortTextField);
RowBtn.add(ReadMagCard);
RowBtn.add(WriteMagCard);
MainPanelForMagReader.add(RowBtn);
System.out.println(RowBtn);
//设置边框
MainPanelForIDReader.setBorder(BorderFactory.createTitledBorder(" ID Card Reader"));
MainPanelForMagReader.setBorder(BorderFactory.createTitledBorder(" Mag Card Reader"));
add(MainPanelForIDReader);
add(MainPanelForMagReader);
MagPortTextField.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
if (Character.isDigit(c) && c != '0' && MagPortTextField.getText().trim().length() < 4)//只允许数字,且长度不大于1
return;
e.consume();
}
});
SecondDataTextArea.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
if ((Character.isDigit(c) || c == ';' || c == ':' || c == '>' || c == '=')&& SecondDataTextArea.getText().trim().length() < 64)//只允许数字,且长度不大于1
return;
e.consume();
}
});
ThirdDataTextArea.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
if ((Character.isDigit(c) || c == ';' || c == ':' || c == '>' || c == '=')&& ThirdDataTextArea.getText().trim().length() < 128)//只允许数字,且长度不大于1
return;
e.consume();
}
});
PortTextField.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
if (Character.isDigit(c) && PortTextField.getText().trim().length() < 4)//只允许数字,且长度不大于1
return;
e.consume();
}
});
//返回提示是否读卡失败与成功
ReadCard.addActionListener(this);
ReadMagCard.addActionListener(this);
WriteMagCard.addActionListener(this);
setVisible(true);
pack();
}
@Override
public void actionPerformed(ActionEvent Btn) {
// TODO Auto-generated method stub
Object target = Btn.getSource();
//String label = btn.getActionCommand();
if (target == ReadMagCard)
{
handleMagRead();
//JOptionPane.showMessageDialog(null, "ReadMagCard");
}
else if(target == WriteMagCard)
{
handleMagWrite();
//JOptionPane.showMessageDialog(null, "handleMagWrite");
}
else if(target == ReadCard)
{
handleIDRead();
//JOptionPane.showMessageDialog(null, "handleIDRead");
}
}
public void handleIDRead() {
int iRet = 0;
int comport = 0;
byte[] idCardInfo = new byte[102400];
byte[] tmp = new byte[5120];
byte[] pic_tmp = new byte[51200];
if(PortTextField.getText().trim().length() > 0)
comport = Integer.parseInt(PortTextField.getText());
else
{
JOptionPane.showMessageDialog(null, "请输入端口号");
return;
}
//读卡
iRet = ReadIDCard(comport , idCardInfo);
if(iRet == 0)
{
int j=0;
int i=0;
for( ;j < 8; i++)
{
if(idCardInfo[i] == '|')
{
j++;
tmp[i] = '\n';
}
else
{
tmp[i] = idCardInfo[i];
}
}
for(int k = 0;k < 51200; k++,i++)
{
if(idCardInfo[i] == '|')
{
break;
}
else
{
pic_tmp[k] = idCardInfo[i];
}
}
String IDPicInfo = new String( pic_tmp );
pic_tmp = null;
pic_tmp = decode(IDPicInfo);
ImageIcon icon=new ImageIcon(pic_tmp);
Pic_Label.setIcon(icon);
try{
String IDInfo = new String( tmp ,"gb2312");
IDCardInfo_Area.setText(IDInfo.trim());
}
catch( UnsupportedEncodingException ex)
{
return;
}
}
else
{
IDCardInfo_Area.setText(new String(""));
Pic_Label.setIcon(ID_Pic);
JOptionPane.showMessageDialog(null, "读卡失败");
}
}
public void handleMagWrite() {
int iRet = 0;
int ComPort = 0;
int track = 5;
byte[] SecondData = new byte[256];
byte[] ThridData = new byte[256];
//MagCard test = new MagCard();
if(MagPortTextField.getText().trim().length() > 0)
ComPort = Integer.parseInt(MagPortTextField.getText());
else
{
JOptionPane.showMessageDialog(null, "请输入端口号");
return;
}
if(SecondDataTextArea.getText().trim().length()<1 || ThirdDataTextArea.getText().trim().length()<1 )
{
JOptionPane.showMessageDialog(null, "请输入写卡数据");
return;
}
if(SecondDataTextArea.getText().trim().length()<1)
{