如何保存OpenCV(JavaCV)Trained Faces图片到数据库?

时间:2017-03-29 15:50:13

标签: java mysql opencv javacv

最近,我完成了一个关于使用JavaCV进行人脸识别的项目。它将面部保存到计算机目录但我想将这些面部保存到像SQLite这样的RDBM,MySQL将是受欢迎的。

将图像保存到计算机目录的代码

private String createPictureFilePathName(File dir) {
    File file = new File(dir.getPath() + "/image-0.png");
    int counter = 1;
    while (file.exists()) {
        file = new File(dir.getPath() + "/image-" + counter + ".png");
        counter++;
    }
    return file.getPath();
}

private static JFileChooser initPictureDirChooser() {
        JFileChooser chooser = new JFileChooser();
        chooser.setAcceptAllFileFilterUsed(false);
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setCurrentDirectory(new File(System.getProperty("user.home")).getAbsoluteFile());
        return chooser;
    }

是否可以将图像保存到数据库?

1 个答案:

答案 0 :(得分:0)

获取文件的输入流,并使用preparedstatement将其作为blob保存到db中。

示例代码:

public boolean saveFile(File file) throws Exception{
    boolean completed = false;
    ConnectionManager conn = new ConnectionManager();
    try {
        PreparedStatement statement = conn.getConnection().prepareStatement("QUERY_TO_INSERT_FILE");
        statement.setBlob(1, file.getInputStream());           
        statement.execute();
        conn.commit();
        completed = true;
    } catch (SQLException e) {
        conn.rollbackQuietly();
        e.printStackTrace();
        throw e;
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }finally{
        conn.close();
    }                           
    return completed;       
}