我有一个代码将从相机拍摄的照片保存为BLOB格式的SQLite数据库,我可以在ImageVIew中显示一个图像。如何从数据库获取所有BLOB并将所有BLOB转换为位图,以便稍后将它们再次转换为OpenCV的Mat格式?
MainActivity:
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.ByteArrayOutputStream;
public class MainActivity extends Activity {
private static final int CAMERA_REQUEST = 1888;
public ImageView imageView;
Imagehelper help = new Imagehelper(this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.imageView = (ImageView) this.findViewById(R.id.imageView1);
Button B = (Button) this.findViewById(R.id.camera);
B.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100 , stream);
byte[] byteArray = stream.toByteArray();
help.insert(byteArray);
}
}
}
ImageHelper:
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
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, text_to_audio TEXT);");
}
public void insert(byte[] bytes) {
ContentValues cv = new ContentValues();
cv.put("imageblob", bytes);
Log.e("inserted", "inserted");
getWritableDatabase().insert("Image", "imageblob", cv);
}
}
答案 0 :(得分:0)
我是如何通过一些项目完成此操作的,我创建了一个将图像转换为字节数组的对象(最终成为您的BLOB)。
在这个对象中是一个静态类,它只是将字节数组解压缩回位图。对你来说,每次有BLOB时都要调用它。
下面你会看到我用来将字节数组放回Bitmap的代码的摘录。
public static final Bitmap ConvertByteArrayToBitmap(byte[] b){
return BitmapFactory.decodeByteArray(b, 0, b.length);
}
不幸的是,我对你问题的后半部分没有答案。