安卓数据库案例4

1、调用SQLiteDatabase的 beginTransaction() 方法来开启一个事务,然后在一个异常捕获的代码块中去执行具体的数据库操作,
2、当所有的操作都完成之后,调用 setTransactionSuccessful() 表示事务已经执行成功了
3、最后在finally代码块中调用 endTransaction() 来结束事务。
在这里插入图片描述

 /**

 * Created by mikeyna on 18-7-3.
 */

public class FaceDetection {
    private static String TAG = "FaceDetection";
    private ArrayList<String> mPicturePathList = new ArrayList<>();
    private Context mContext;
    GalleryDatabaseHelper mDBHelper;
    public FaceDetection(Context context)
    {
        mContext = context;
        mDBHelper = new GalleryDatabaseHelper(context);
    }
    public void init(String folderDir)
    {
        String labelfile = "";
        String conf = "/sdcard/tfmodel/caffemodel/facedetector/deploy.prototxt";
        String model =  "/sdcard/tfmodel/caffemodel/facedetector/res10_300x300_ssd_iter_140000_fp16.caffemodel";
        SmartNative.faceDetectorInit(model, conf, labelfile);
        getPicturePaths(folderDir);
        StorePictureInfo2DB();
    }
    public void close()
    {
        SmartNative.faceDetectorClose();
    }
    //获取相片的名称
    private ArrayList<String> getPicturePaths(String dir)
    {
        mPicturePathList.clear();
        mPicturePathList = Files.searchFiles(dir, ".jpg", false, mPicturePathList);
        return mPicturePathList;
    }
    //store picture information to database
    private void StorePictureInfo2DB()
    {
        SQLiteDatabase db = mDBHelper.getReadableDatabase();
        for(String imagePath : mPicturePathList)
        {
            Cursor cursor = db.query(GalleryDatabaseHelper.Tables.PICTURES,
                     null,
                    GalleryConstrant.Picture.PATH + " =?", new String[]{imagePath},
                    null, null, null);
            if(cursor != null) {
                if(cursor.moveToNext())
                    continue;
            }
            ContentValues values = new ContentValues();
            values.put(GalleryConstrant.Picture.PATH, imagePath);
            db.insert(GalleryDatabaseHelper.Tables.PICTURES, null, values);
        }
    }
    //face detection and store result to database
    public void faceDetectAndStoreResult(){
        SQLiteDatabase db = mDBHelper.getReadableDatabase();
        Cursor cursor = db.query(GalleryDatabaseHelper.Tables.PICTURES,
                new String[]{GalleryConstrant.Picture.ID, GalleryConstrant.Picture.PATH},
                null, null, null, null,
                null, null);
        if (cursor == null) {
            Log.i(TAG, "cursor is null");
            return ;
        }
        try {
            while(cursor.moveToNext()) {
                int imageId = cursor.getInt(0);
                String imagePath = cursor.getString(1);
                ArrayList<LabelInfo>  labelInfoList = DetectFace(imagePath);

                if(labelInfoList != null)
                {
                    ContentValues values = new ContentValues();
                    //删除原始记录
                    db.delete(GalleryDatabaseHelper.Tables.Faces,
                            GalleryConstrant.Face.PICTURE_ID + "=?",
                            new String[]{String.valueOf(imageId)});
                    //将现有的人脸检测结果插入数据库
                    for(LabelInfo labelInfo : labelInfoList)
                    {
                        Gson g = new Gson();
                        String jsonString = g.toJson(labelInfo.boundingBox);
                        //add label information to database
                        values.put(GalleryConstrant.Face.PICTURE_ID, imageId);
                        values.put(GalleryConstrant.Face.CLUSTER_ID, -1);
                        values.put(GalleryConstrant.Face.RECT, jsonString);
                        values.put(GalleryConstrant.Face.FEATURE, "");
                        db.insert(GalleryDatabaseHelper.Tables.Faces, null, values);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cursor.close();
        }
    }
    private ArrayList<LabelInfo> DetectFace(String imagePath)
    {
        if(imagePath.compareTo("") == 0) return null;
        ArrayList<LabelInfo> labelInfos = SmartNative.faceDetect(imagePath);
        return labelInfos;
    }
}

4、数据库是什么时候用?

  • 比如金额交易,数据的批量删除和存储等。比如现在要进行银行转账,不可能扣了A的钱,但是B没有收到,然后A的钱就不知道哪里去了,再比如,现在要备份短信,要么成功备份到磁盘,要么备份失败,而不可能在没有备份成功的情况就把本地给删除了。
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值