从Android SQLite数据库检索所有BLOB图像
我有一个代码,可以将从相机拍摄的照片以BLOB格式保存到SQLite数据库,并且可以在ImageVIew中显示一幅图像。如何从数据库中获取所有blob并将其转换为位图,以便以后再次将其转换为OpenCV的Mat格式 主要活动:从Android SQLite数据库检索所有BLOB图像,android,sqlite,opencv,android-studio,bitmap,Android,Sqlite,Opencv,Android Studio,Bitmap,我有一个代码,可以将从相机拍摄的照片以BLOB格式保存到SQLite数据库,并且可以在ImageVIew中显示一幅图像。如何从数据库中获取所有blob并将其转换为位图,以便以后再次将其转换为OpenCV的Mat格式 主要活动: import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.os.Bundle; import android.vie
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);
}
}
我是如何在我的一些项目中实现这一点的,我创建了一个对象,将图像转换为字节数组(最终成为BLOB) 在这个对象内部是一个静态类,它将简单地将字节数组解压回位图。对你来说,每次你有斑点的时候,你都会想叫这个 下面是我用来将字节数组放回位图的代码摘录
public static final Bitmap ConvertByteArrayToBitmap(byte[] b){
return BitmapFactory.decodeByteArray(b, 0, b.length);
}
不幸的是,对于你问题的后半部分,我没有答案