java - (ASK) 临时指纹扫描仪保存文件

标签 java fingerprint

我的项目在通过毕业时遇到问题.. 我尝试制作一个考勤指纹系统..

我的问题是当我扫描指纹并保存指纹模板时..然后我关闭我的netbeans... 保存文件未保存,正在被删除...如果我想验证它们,我必须再次扫描我的指纹..

任何人都可以帮助我解决问题..

谢谢...

+++ 这里是代码+++

import java.io.*;
import java.beans.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.digitalpersona.onetouch.*;

public class MainForm extends JFrame
{
public static String TEMPLATE_PROPERTY = "template";
private DPFPTemplate template;
    private String file;
public class TemplateFileFilter extends javax.swing.filechooser.FileFilter {
    @Override public boolean accept(File f) {
        return f.getName().endsWith(".fpt");
    }
    @Override public String getDescription() {
        return "Fingerprint Template File (*.fpt)";
    }
}
MainForm() {
    setState(Frame.NORMAL);
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    this.setTitle("Verifikasi dan Pendaftaran Sidik Jari");
    setResizable(false);

    final JButton enroll = new JButton("Pendaftaran Sidik Jari");
    enroll.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) { onEnroll(); }});

    final JButton verify = new JButton("Verifikasi Sidik Jari");
    verify.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) { onVerify(); }});

    final JButton save = new JButton("Simpan");
    save.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) { onSave(); }});

    final JButton load = new JButton("Baca Template");
    load.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) { onLoad(); }});

    final JButton quit = new JButton("Tutup");
    quit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) { System.exit(0); }});

    this.addPropertyChangeListener(TEMPLATE_PROPERTY, new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent evt) {
            verify.setEnabled(template != null);
            save.setEnabled(template != null);
            if (evt.getNewValue() == evt.getOldValue()) return;
            if (template != null)

                JOptionPane.showMessageDialog(MainForm.this, "Template sidik jari siap untuk verifikasi sidik jari.", "Pendaftaran Sidik Jari", JOptionPane.INFORMATION_MESSAGE);
                                    onSave();


        }
    });

    JPanel center = new JPanel();
    center.setLayout(new GridLayout(4, 1, 0, 5));
    center.setBorder(BorderFactory.createEmptyBorder(20, 20, 5, 20));
    center.add(enroll);
    center.add(verify);
    center.add(save);
    center.add(load);

    JPanel bottom = new JPanel(new FlowLayout(FlowLayout.TRAILING));
    bottom.setBorder(BorderFactory.createEmptyBorder(5, 20, 5, 20));
    bottom.add(quit);

    setLayout(new BorderLayout());
    add(center, BorderLayout.CENTER);
    add(bottom, BorderLayout.PAGE_END);

    pack();
    setSize((int)(getSize().width*1.6), getSize().height);
    setLocationRelativeTo(null);
    setTemplate(null);
    setVisible(true);
}

private void onEnroll() {
    EnrollmentForm form = new EnrollmentForm(this);
    form.setVisible(true);
}

private void onVerify() {
    VerificationForm form = new VerificationForm(this);
    form.setVisible(true);
}

private void onSave() {
    JFileChooser chooser = new JFileChooser();
    chooser.addChoosableFileFilter(new TemplateFileFilter());
    while (true) {
        if (chooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
            try {
                File file = chooser.getSelectedFile();
                                    //file="xxx";
                if (!file.toString().toLowerCase().endsWith(".fpt"))
                    file = new File(file.toString() + ".fpt");
                                           // file=file.toString()+".fpt";
                if (file.exists()) {
                    int choice = JOptionPane.showConfirmDialog(this,
                        String.format("File \"%1$s\" sudah ada.\nApakah mau mengganti?", file.toString()),
                        "Penyimpanan Sidik Jari",
                        JOptionPane.YES_NO_CANCEL_OPTION);
                    if (choice == JOptionPane.NO_OPTION)
                        continue;
                    else if (choice == JOptionPane.CANCEL_OPTION)
                        break;
                                }
                FileOutputStream stream = new FileOutputStream(file);
                stream.write(getTemplate().serialize());
                stream.close();
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this, ex.getLocalizedMessage(), "Penyimpanan Sidik Jari", JOptionPane.ERROR_MESSAGE);
            }
        }
        break;
    }
}

private void onLoad() {
    JFileChooser chooser = new JFileChooser();
    chooser.addChoosableFileFilter(new TemplateFileFilter());
    if(chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
        try {
            FileInputStream stream = new FileInputStream(chooser.getSelectedFile());
            byte[] data = new byte[stream.available()];
            stream.read(data);
            stream.close();
            DPFPTemplate t = DPFPGlobal.getTemplateFactory().createTemplate();
            t.deserialize(data);
            setTemplate(t);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getLocalizedMessage(), "Loading Proses ", JOptionPane.ERROR_MESSAGE);
        }
    }
}

public DPFPTemplate getTemplate() {
    return template;
}
public void setTemplate(DPFPTemplate template) {
    DPFPTemplate old = this.template;
    this.template = template;
    firePropertyChange(TEMPLATE_PROPERTY, old, template);
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new MainForm();
        }
    });
}

}

最佳答案

在 onSave 方法中,您尝试使用以下方式获取文件名:

file.toString()

你应该这样做:

file.getName()

toString() 以字符串形式返回对象。

*已编辑* 对于File,toString返回文件的路径,原始代码在这里是正确的。

要确保字节写入文件,您应该使用:

file.flush()

或者使用 ObjectOutputStream。它的 write(byte[]) 会阻塞,直到字节实际写入

FileOutputStream stream = new FileOutputStream(file);
ObjectOutputStream out = new ObjectOutputStream(stream);
out.writeObject(getTemplate().serialize());
out.flush();
out.close();
stream.close();

关于java - (ASK) 临时指纹扫描仪保存文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15923684/

相关文章:

php - 散列 session 指纹真的有必要吗?

java - 按姓氏对对象的 ArrayList 排序,然后按名字排序

android - 唯一设备指纹

java - org.apache.jasper.JasperException : An exception occurred processing

java.io.StreamCorruptedException : invalid stream header: EFBFBDEF

javascript - 通过 WebAuthn API 识别手指 ID

android - *INTERACT_ACROSS_USERS* 导致使用指纹登录 Android 时崩溃

php - 使用 PHP 从指纹设备获取数据

java - 不同的数据库系统实现(例如 MySQL 和 Microsoft SQL Server)对 `DataSource` 和 `DriverManager` 的影响是否不同?

java - 如何在应用程序中包含大型字符串数组资源?