2017-03-29 9 views
1

最近、私はJavaCVを使って顔認識に関するプロジェクトを完了しました。それはコンピュータのディレクトリに顔を保存するが、私はそれらの顔をSQLiteのようなRDBMに保存したい、MySQLは歓迎されるだろう。OpenCV(JavaCV)を保存する方法訓練された写真をデータベースに表示しますか?

コードデータベースに画像を保存するためにこれを可能にするためにコンピュータのディレクトリに

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; 
    } 

どれリードを画像を保存するための

+0

ファイルの入力ストリームを取得し、PreparedStatementを使用してBLOBとしてDBに保存します。 – webcoder

+0

あなたはこれまでに何を試しましたか? –

+0

@AndyJones私はいくつかのチュートリアルを見つけました:[Oracleデータベースに画像を保存する例](http://www.javatpoint.com/storing-image-in-oracle-database)と[MySQLデータベースに画像を挿入する方法Java](http://1bestcsharp.blogspot.com/2015/04/java-how-to-insert-image-in-mysql-Database-Using-Java.html)を参照してください。すぐに私は私の忙しいスケジュールから解放されるとき実装します。 – dgrtkhan09

答えて

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;  
} 
関連する問題