转换垫BLOB,然后返回到垫 [英] Convert Mat to Blob and then back to Mat

查看:312
本文介绍了转换垫BLOB,然后返回到垫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我特林使用识别OpenCV的Andr​​oid的应对
我需要转换一个通过inputFrame.gray()人脸检测期间收到垫形象;在CvCameraViewFrame成斑点是一个byte []保存到SQLite数据库,然后同时识别转换这个字节[]回可以在.cpp文件中使用JNI中的文件夹中识别code垫子文件是原生。


解决方案

这竟然是很容易与机器人板载方法:

 进口org.opencv.core.Mat;进口android.content.Context;
进口android.content.ContentValues​​;
进口android.database.Cursor;
进口android.database.sqlite.SQLiteDatabase;
进口android.database.sqlite.SQLiteDatabase.CursorFactory;
进口android.database.sqlite.SQLiteOpenHelper;类SqTable扩展SQLiteOpenHelper {
    字符串表=MYDB;    公共SQLTABLE(上下文的背景下,字符串名称,CursorFactory工厂,诠释的版本){
        超(背景下,名称,厂家,版本);
    }    @覆盖
    公共无效的onCreate(SQLiteDatabase DB){
        db.execSQL(CREATE TABLE+表+(Name文本UNIQUE,T INTEGER,W INTEGER,H INTEGER,PIX BLOB););
    }    @覆盖
    公共无效onUpgrade(SQLiteDatabase为arg0,ARG1 INT,INT ARG2){
    }    公共无效dbput(字符串名称,板M){
        长为nbytes = m.total()* m.elemSize();
        字节[]字节=新字节[(INT)为nbytes]
        m.get(0,0,字节);        dbput(姓名,m.type(),m.cols(),m.rows(),字节);
    }    公共无效dbput(字符串名称,诠释T,INT W,INT小时,字节[]字节){
        Log.d(dbput,名称+,+ T +,+ W +的x+ H);
        SQLiteDatabase分贝= this.getWritableDatabase();
        ContentValues​​值=新ContentValues​​();
        values​​.put(名,名);
        values​​.put(T,T);
        values​​.put(W,w)的;
        values​​.put(H中,h);
        values​​.put(PIX,字节);
        db.insert(表,空,价值);
        db.close();
    }     公共垫dbget(字符串名称){
        SQLiteDatabase分贝= this.getReadableDatabase();
        的String [] =列{T型,W,H,PIX};
        光标光标= db.query(表,列,名=?,
                新的String [](名称),//ð。选择ARGS
                空,//即通过...分组
                空,// F。有
                空,//克为了通过
                空值); // H。限制        如果(指针!= NULL)
            cursor.moveToFirst();        INT T = cursor.getInt(0);
        INT W = cursor.getInt(1);
        INT H = cursor.getInt(2);
        字节[] P = cursor.getBlob(3);
        板M =新垫(H,W,T);
        m.put(0,0,p)的;
        Log.d(dbget(+名称+),m.toString());
        返回米;
    }
};//在你以后的活动中使用:
SQLTABLE SQL =新SQLTABLE(这一点,IMGS,NULL,1);
板M =新的垫(200,400,CvType.CV_8UC3,新的标量(0,100,0));
Core.putText(男,世界(〜),新的点(30,80),Core.FONT_HERSHEY_SCRIPT_SIMPLEX,2.2,新的标量(200200200));
sql.dbput(你好,M);
//板M = sql.dbget(你好);

Basically I am tring to Face Recognition using OpenCV Android I need to convert Mat image that is received during face detection via inputFrame.gray(); in CvCameraViewFrame into a Blob that is a byte[] to save into SQLite Database and then while recognition convert this byte[] back to a Mat file that can be used in .cpp files in jni folder as recognition code is native.

解决方案

[edit]

it turned out to be quite easy with androids onboard methods:

import org.opencv.core.Mat;

import android.content.Context;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

class SqTable extends SQLiteOpenHelper {
    String table = "mydb";

    public SqlTable(Context context, String name, CursorFactory factory, int version) {
        super(context, name, factory, version);     
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table "+table+" (name TEXT UNIQUE, t INTEGER, w INTEGER, h INTEGER, pix BLOB);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
    }

    public void dbput(String name, Mat m) {
        long nbytes = m.total() * m.elemSize();
        byte[] bytes = new byte[ (int)nbytes ];
        m.get(0, 0,bytes);

        dbput(name, m.type(), m.cols(), m.rows(), bytes); 
    }

    public void dbput(String name, int t, int w, int h, byte[] bytes) {
        Log.d("dbput", name + " " + t + " " + w + "x" + h);
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name", name);
        values.put("t", t); 
        values.put("w", w); 
        values.put("h", h); 
        values.put("pix", bytes);
        db.insert(table, null, values);
        db.close();
    }

     public Mat dbget(String name) {
        SQLiteDatabase db = this.getReadableDatabase();
        String [] columns = {"t","w","h","pix"};
        Cursor cursor = db.query(table,columns," name = ?", 
                new String[] { name }, // d. selections args
                null, // e. group by
                null, // f. having
                null, // g. order by
                null); // h. limit

        if (cursor != null)
            cursor.moveToFirst();

        int t = cursor.getInt(0);
        int w = cursor.getInt(1);
        int h = cursor.getInt(2);
        byte[] p = cursor.getBlob(3);
        Mat m = new Mat(h,w,t);
        m.put(0,0,p);
        Log.d("dbget("+name+")", m.toString());
        return m;
    }
};

// later use in your Activity:
SqlTable sql = new SqlTable(this,"imgs",null,1);
Mat m = new Mat(200,400, CvType.CV_8UC3,new Scalar(0,100,0));
Core.putText(m, "world (~)", new Point(30,80), Core.FONT_HERSHEY_SCRIPT_SIMPLEX, 2.2, new          Scalar(200,200,200));
sql.dbput("hello",m);
// Mat m = sql.dbget("hello");

这篇关于转换垫BLOB,然后返回到垫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆