Olá, estou tentando pegar BLOBS salvos em um banco de dados SQLite e convertê-los para Bitmap. Após isso, preciso converter esses Bitmaps em Mat, para usar com a OpenCV. O problema é que quando abro a camera (quando deveriam ser carregadas essas fotos), o app crasha. Segue o trecho da MainActivity onde faço as conversões:
public void onCameraViewStarted(int width, int height) {
Log.i(TAG, "Started.");
//Inicializa Variaveis para personagens
//Starts Characters Variables
homer = new Mat();
homer2 = new Mat();
Mat homer3 = new Mat();
marge = new Mat();
Mat marge2 = new Mat();
Mat ottoMann1 = new Mat();
Mat ottoMann2 = new Mat();
preLoadedImages = new ArrayList<Mat>();
//Variaveis Temporarias
//Temp Variables
Mat rawHomer1 = new Mat();
Mat rawHomer2 = new Mat();
Mat rawHomer3 = new Mat();
Mat rawMarge1 = new Mat();
Mat rawMarge2 = new Mat();
Mat rawOttoMann1 = new Mat();
Mat rawOttoMann2 = new Mat();
try {
Cursor c = help.getAll();
if (c.getCount() > 0) {
// move to the first result.
c.moveToFirst();
//iterate over the results.
// iterate over rows
for (int i = 0; i < c.getCount(); i++) {
byte[] image = c.getBlob(0);
Bitmap bmp = BitmapFactory.decodeByteArray(image, 0, image.length);
Utils.bitmapToMat(bmp, rawHomer1);
Imgproc.cvtColor(rawHomer1, homer, Imgproc.COLOR_RGB2BGR);
preLoadedImages.add(homer);//0
c.moveToNext();
byte[] image2 = c.getBlob(0);
Bitmap bmp2 = BitmapFactory.decodeByteArray(image2, 0, image2.length);
Utils.bitmapToMat(bmp2, rawHomer2);
Imgproc.cvtColor(rawHomer2, homer2, Imgproc.COLOR_RGB2BGR);
preLoadedImages.add(homer2);//1
c.moveToNext();
byte[] image3 = c.getBlob(0);
Bitmap bmp3 = BitmapFactory.decodeByteArray(image3, 0, image3.length);
Utils.bitmapToMat(bmp3, rawHomer3);
Imgproc.cvtColor(rawHomer3, homer3, Imgproc.COLOR_RGB2BGR);
preLoadedImages.add(homer3);//2
c.moveToNext();
}
c.close();
}
rawMarge1 = Utils.loadResource(this, R.drawable.marge_1);
rawMarge2 = Utils.loadResource(this, R.drawable.marge_2);
Imgproc.cvtColor(rawMarge1, marge, Imgproc.COLOR_RGB2BGR);
Imgproc.cvtColor(rawMarge2, marge2, Imgproc.COLOR_RGB2BGR);
preLoadedImages.add(marge);//3
preLoadedImages.add(marge2);//4
//Otto Man
rawOttoMann1 = Utils.loadResource(this, R.drawable.otto_1);
rawOttoMann2 = Utils.loadResource(this, R.drawable.otto_2);
Imgproc.cvtColor(rawOttoMann1, ottoMann1, Imgproc.COLOR_RGB2BGR);
Imgproc.cvtColor(rawOttoMann2, ottoMann2, Imgproc.COLOR_RGB2BGR);
preLoadedImages.add(ottoMann1);//5
preLoadedImages.add(ottoMann2);//6
previousDetector = new SimpsonDetector(preLoadedImages);
previousDetector.ComputeImages();
DetectedChars = new boolean[preLoadedImages.size()];
threadControl = new boolean[preLoadedImages.size()];
} catch (IOException e) {
e.printStackTrace();
}
}
Início da MainActivity, onde inicio a classe Imagehelper que é onde o banco de dados é criado:
Imagehelper help;
public MainActivity() {
Log.i(TAG, "Instantiated new " + this.getClass());
}
/**
* Chamado quando a Activity criada.
* Called when activity is created
* */
@Override
public void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "called onCreate");
super.onCreate(savedInstanceState);
help = new Imagehelper(this);
Classe Imagehelper:
public class Imagehelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "clothes.db";
private static final int SCHEMA_VERSION = 3;
public Imagehelper(Context context) {
super(context, DATABASE_NAME, null, SCHEMA_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE Image(_id INTEGER PRIMARY KEY AUTOINCREMENT,imageblob BLOB);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void insert(byte[] bytes) {
ContentValues cv = new ContentValues();
cv.put("imageblob", bytes);
Log.e("inserted", "inserted");
getWritableDatabase().insert("Image", "imageblob", cv);
}
public Cursor getAll() {
return (getReadableDatabase().rawQuery("SELECT imageblob FROM Image", null));
}
}