将指纹模板保存到数据库 mysql - java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23456524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 23:06:32  来源:igfitidea点击:

Save a fingerprint template into a database mysql - java

javamysqldatabasesdkfingerprint

提问by rave

I am developing a project with the sdk fingerprint of Griaule , and as a start I'm creating a program without a GUI that allows the user to scan his fingerprint and store it in a mysql database already created earlier. I'm here to ask you a hand with regard to storing the fingerprint in the database. In the program I created, I captured the fingerprint, I extracted the template from the fingerprint through a function I call extract () . After that I should call another function , enroll (), which allows me to save the fingerprint in a database. Even looking at the SDK examples I did not understand how it works, can someone help me? thanks in advance! :)

我正在使用 Griaule 的 sdk 指纹开发一个项目,作为开始,我正在创建一个没有 GUI 的程序,该程序允许用户扫描他的指纹并将其存储在之前已经创建的 mysql 数据库中。我在这里请教您将指纹存储在数据库中的问题。在我创建的程序中,我捕获了指纹,我通过一个我调用的函数从指纹中提取了模板 extract() 。之后我应该调用另一个函数 enroll(),它允许我将指纹保存在数据库中。即使查看 SDK 示例我也不明白它是如何工作的,有人可以帮助我吗?提前致谢!:)

public void enroll() {
   try {
       //Inserts the template on the database
       enrollStmt.setBinaryStream(1,new ByteArrayInputStream(template.getData()), template.getData().length);
       enrollStmt.executeUpdate();

       //Picks the ID generated for it. 
       ResultSet rs = insertedIdStmt.executeQuery();
       rs.next();
       ui.writeLog("Fingerprint enrolled with id = "+Integer.toString(rs.getInt(1)));

   } catch (SQLException e) {
       ui.writeLog("Error enrolling template");
   }
}

采纳答案by Hirak

It is saving the finger print data as BLOB in the database. Blob (Binary Large Object) is nothing but a byte array representation of information, mainly used to store images etc in database. In your case, the fingerprint information is being stored.

它将指纹数据保存为数据库中的 BLOB。Blob(Binary Large Object)只不过是信息的字节数组表示,主要用于在数据库中存储图像等。在您的情况下,正在存储指纹信息。

enrollStmt.setBinaryStream(1,new ByteArrayInputStream(template.getData()), template.getData().length);

In this line, the bytearrayinputstream is created using the data in the template object. template.getData is giving you the byte[] representation of the fingerprint information. Then the byte[] is getting saved in database, by

在这一行中,bytearrayinputstream 是使用模板对象中的数据创建的。template.getData 为您提供了指纹信息的 byte[] 表示。然后字节 [] 被保存在数据库中,通过

enrollStmt.executeUpdate();

enrollStmt.executeUpdate();

Whereas, the following query gives you the id for the data stored, for your use.

而以下查询为您提供了存储数据的 id,供您使用。

 ResultSet rs = insertedIdStmt.executeQuery();

回答by rave

Ok thank you very much Hirak , so I open a new connection with a function I created called initdb () , structured as follows:

好的,非常感谢 Hirak,所以我用我创建的一个名为 initdb () 的函数打开了一个新连接,结构如下:

private void initDB() {

    try {
           //Loads the JDBC driver. 
           Class.forName("com.mysql.jdbc.Driver").newInstance();

           /**Connection to the Database.*/
           Connection db;
           String user = "root";
           String password = ""; 

           // connect to a memory database
           db = DriverManager.getConnection("jdbc:mysql://localhost:3306/impronte?user=" + user + "&password=" + password);

           Statement stm = (Statement) db.createStatement();

         //Creates the statements that will be executed on the database,
           enrollStmt = db.prepareStatement("INSERT INTO persona(template) values(?)");
           insertedIdStmt = db.prepareStatement("SELECT MAX(ID) FROM persona");

       } catch (Exception e) {
           System.out.println("Error connecting to the database.");
           System.out.println(e.getMessage());
       }

}

and inside the enroll function this code, but nevertheless gives me the error("Error enrolling template"):

并在注册函数中使用此代码,但仍然给了我错误(“错误注册模板”):

public void enroll ( Template template ) throws GrFingerJavaException {
   try {
           //Inserts the template on the database
           enrollStmt.setBinaryStream(1,new ByteArrayInputStream(template.getData()), template.getData().length);
           enrollStmt.executeUpdate();

           //Picks the ID generated for it. 
           ResultSet rs = insertedIdStmt.executeQuery();
           rs.next();
           System.out.println("Fingerprint enrolled with id = "+Integer.toString(rs.getInt(1)));

           System.out.println("Fingerprint enrolled");

       } catch (SQLException e) {
           System.out.println("Error enrolling template");
       }
}

回答by michaelprime_

You need to set the templateto the template class of the sdk you are using before calling the binaryStream, see what I meant: Line 6 if I'm correct.

template在调用 binaryStream 之前,您需要将 设置为您正在使用的 sdk 的模板类,看看我的意思:第 6 行,如果我是正确的。

public void enroll ( Template template ) throws GrFingerJavaException {  
     try {

       //Inserts the template on the database

        Template temp = template;
         if(temp != null){
           byte[] b = temp.serialize();   
           enrollStmt.setBytes(1, b); 
           }

        enrollStmt.executeUpdate();

       //Picks the ID generated for it. 
       ResultSet rs = insertedIdStmt.executeQuery();
       rs.next();
       System.out.println("Fingerprint enrolled with id = "+Integer.toString(rs.getInt(1)));

       System.out.println("Fingerprint enrolled");

   } catch (SQLException e) {
       System.out.println("Error enrolling template");
   }

}

}