需求分析:
需要实现人脸检测,识别和聚类的功能.这三个模块是独立的模块,如果面对相册中大量的数据,需要将中间结果存储到数据库中.
因为纯属验证算法的准确性,这里建立的Table比较简单.
建立三个表格,分别是Pciture\Faces\Cluster表
SQLiteDatabase 常用方法:
openOrCreateDatabase(Stringpath,SQLiteDatabase.CursorFactory factory)
打开或创建数据库
insert(Stringtable,StringnullColumnHack,ContentValues values) 插入一条记录
delete(String table,String whereClause,String[] whereArgs) 删除一条记录
query(String table,String[] columns,Stringselection,String[]selectionArgs,String groupBy,String having,StringorderBy) 查询一条记录
update(String table,ContentValues values,StringwhereClause,String[]whereArgs) 修改记录
execSQL(String sql) 执行一条SQL语句
close() 关闭数据库
Cursor常用方法
getCount() 获得总的数据项数
isFirst() 判断是否第一条记录
isLast() 判断是否最后一条记录
moveToFirst() 移动到第一条记录
moveToLast() 移动到最后一条记录
move(int offset) 移动到指定记录
moveToNext() 移动到下一条记录
moveToPrevious() 移动到上一条记录
getColumnIndexOrThrow(String columnName) 根据列 名称获得列索引
getInt(int columnIndex) 获得指定列索引的int类型值
getString(int columnIndex) 获得指定列缩影的String类型值
ContentValues
在添加、修改:代表意义包装一行数据。
本质:Map集合;key=>代表的数据库表中字段,value=>字段设定值
whereClause
用占位符填充 :条件 表达式 name=?
用值直接填充::条件 表达式 name=‘AAA’;whereArgs可以不用传值
文件结构如下:
GalleryConstrant.java中主要记录表格中列的名称,把列的名称用变量的形式存储起来,方便在其他地方引用.
GalleryDatabaseHelper.java继承SQLiteOpenHelper类,这里主要实现建表操作.
FaceDetection.java是调用实例,对数据表进行操作.
GalleryConstrant.java中主要记录表格中的列的名称
package com.lenovo.ailab.smartkit4.database;
import android.net.Uri;
/**
Created by mikeyna on 18-7-2.
/
public class GalleryConstrant {
//picture table
protected interface PictureColumns {
String ID = “_picture_id”;
String PATH = “path”;
}
public static final class Picture implements PictureColumns {
private Picture() {
}
}
//face table
protected interface FaceColumns {
String ID = “_face_id”;
String PICTURE_ID = “picture_id”;
String RECT = “face_rect”;
String FEATURE = “face_feature”;
String CLUSTER_ID = “cluster_id”;
}
public static final class Face implements FaceColumns {
private Face() {
}
}
//cluster tabel
protected interface ClusterColumns {
String ID = “_cluster_id”;
String AVG_FEATURE = “average_feature”;
}
public static final class Cluster implements ClusterColumns {
private Cluster() {
}
}
}
GalleryDatabaseHelper.java中对数据库进行操作,主要是完成建表的功能,还有就是把要建的表格统一到一个Tables的类
package com.lenovo.ailab.smartkit4.database;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
/*
Created by mikeyna on 18-7-2.
*/
public class GalleryDatabaseHelper extends SQLiteOpenHelper {
private static final String TAG = “GalleryDatabaseHelper”;
private static final String DATABASE_NAME = “smartGallery.db”;
private static final int DATABASE_VERSION = 1;
private Context mContext;
public interface Tables
{
String PICTURES = “pictures”;
String Faces = “faces”;
String CLUSTERS = “cluster”;
}
public GalleryDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
mContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.i(TAG, “onCreate!!”);
db.execSQL(“CREATE TABLE " + Tables.PICTURES + “(” +
GalleryConstrant.Picture.ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
GalleryConstrant.Picture.PATH + " VARCHAR(20));”);
db.execSQL(“CREATE TABLE " + Tables.Faces + “(” +
GalleryConstrant.Face.ID + " INTEGER PRIMARY KEY AUTOINCREMENT ,” +
GalleryConstrant.Face.PICTURE_ID + " INTEGER NOT NULL ," +
GalleryConstrant.Face.CLUSTER_ID + " INTEGER NOT NULL ," +
GalleryConstrant.Face.FEATURE + " TEXT NOT NULL ," +
GalleryConstrant.Face.RECT + " TEXT NOT NULL );");
db.execSQL(“CREATE TABLE " + Tables.CLUSTERS + “(” +
GalleryConstrant.Cluster.ID + " INTEGER PRIMARY KEY AUTOINCREMENT ,” +
GalleryConstrant.Cluster.AVG_FEATURE + " TEXT NOT NULL );");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}