qq_35610846 2016-07-15 01:59 采纳率: 0%
浏览 1241

磁卡机代码如何返回数据到控制台上。

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)
    {
  • 写回答

1条回答 默认 最新

  • qq_35610846 2016-07-15 02:00
    关注
    track = 3; //写第三轨
            System.arraycopy(ThirdDataTextArea.getText().trim().getBytes(), 0, ThridData, 0, ThirdDataTextArea.getText().trim().length());
        }
        else if (ThirdDataTextArea.getText().trim().length()<1)
        {
            track = 2; //写第二轨
            System.arraycopy(SecondDataTextArea.getText().trim().getBytes(), 0, SecondData, 0, SecondDataTextArea.getText().trim().length());
        }
        else
        {
            track = 5;  //写二和三轨
            System.arraycopy(ThirdDataTextArea.getText().trim().getBytes(), 0, ThridData, 0, ThirdDataTextArea.getText().trim().length());
            System.arraycopy(SecondDataTextArea.getText().trim().getBytes(), 0, SecondData, 0, SecondDataTextArea.getText().trim().length());
        }
    
        iRet = WriteMagCard(ComPort, track, SecondData, ThridData);
        if(iRet == 0)
        {
            JOptionPane.showMessageDialog(null, "写卡成功");
        }
        else
        {
            JOptionPane.showMessageDialog(null, "写卡失败");
        }
    }
    public void handleMagRead() {
        int iRet = 0;
        int ComPort = 0;
        int track = 5;
        byte[] SecondData = new byte[256];
        byte[] ThridData = new byte[256];
        if(MagPortTextField.getText().trim().length() > 0)
            ComPort = Integer.parseInt(MagPortTextField.getText());
        else
        {
            JOptionPane.showMessageDialog(null, "请输入端口号");
            return;
        }
        //读卡
        //JOptionPane.showMessageDialog(null, "ReadMagCard(ComPort, track, SecondData, ThridData);");
        iRet = ReadMagCard(ComPort, track, SecondData, ThridData);
        if(iRet == 0)
        {
            SecondDataTextArea.setText(new String(SecondData).trim());
            ThirdDataTextArea.setText(new String(ThridData).trim());
        }
        else
        {
            SecondDataTextArea.setText(new String(""));
            ThirdDataTextArea.setText(new String(""));
            JOptionPane.showMessageDialog(null, "读卡失败");
        }
    }
    public static byte[] decode(String str){     
        byte[] bt = null;     
        try {     
                sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();     
                bt = decoder.decodeBuffer( str );     
        } 
        catch (IOException e) {     
               e.printStackTrace();     
        }
               return bt;     
     }    
    public static void main(String[] args) {
        new ReaderDemoForMagAndId();
     }
    

    }

    评论

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘