读取从颤音中存储在SQLFlite数据库中的图像
在数据库中插入图像时的打印语句将显示积极的结果。
即使从数据库中阅读时,在打印语句的输出中,我也可以看到图像数据。
但是我无法显示它。如何验证问题在哪里?
------------------------------------文件:sqlfhelper.dart ------------------------------------- ------------------
表包含单个项目斑点。
class SQLHelper {
static Future<void> createTables(sql.Database database) async
{
await database
.execute("CREATE TABLE items1(id INTEGER PRIMARY KEY, picture BLOB )");
}
用于创建表的代码:
static Future<sql.Database> db() async {
return sql.openDatabase(
'kindacode1.db',
version: 1,
onCreate: (sql.Database database, int version) async {
await createTables(database);
},
);
}
在这里,我将图像文件传递给此功能,将其转换为uint8list
,然后存储在表中:
static Future<int> createItem(File? obj) async {
final db = await SQLHelper.db();
Uint8List? pic;
if (obj != null) {
pic = await obj.readAsBytes();
if (pic.isNotEmpty) {
print("VVVV");
}
final data = {'picture': pic};
final id = await db.insert('items1', data,
conflictAlgorithm: sql.ConflictAlgorithm.replace);
return id;
}
用于从数据库中读取:
static Future<List<Map<String, dynamic>>> getItems() async {
final db = await SQLHelper.db();
return db.query('items1', orderBy: "id");
}
--------------- --------------------------文件:Main.Dart ----------------------------------------------------------------------- -------
状态小部件的initstate:_refreshjournals()
函数从数据库读取。
@override
void initState() {
super.initState();
_refreshJournals();
}
想法是从数据库中读取并取出所需的图像。这是由数字形式的打印语句打印的数据。
List<Map<String, dynamic>> _journals = [];
void _refreshJournals() async {
final data = await SQLHelper.getItems();
setState(() {
_journals = data;
if (_journals.isNotEmpty) {
print("WfW\n");
print(data);
_showForm(_journals[15]['id']);
}
});
}
使用FromRawPath
将图像从UINT8LIST转换为图像。我可以在这里看到打印语句得到实现。
void _showForm(int? id) async {
if (id != null) {
final existingJournal =
_journals.firstWhere((element) => element['id'] == 15);
if (existingJournal.isNotEmpty) {
print(existingJournal);
myImage = File.fromRawPath(existingJournal['picture']);
}
}
}
被用作:
Image.file(
myImage,
scale: 0.5,
width: 20,
height: 20,
fit: BoxFit.scaleDown,
),
它在GUI中导致这一点: 带有奇怪文字的奇怪形状是图像应该是图像的地方。
图像由手机的摄像机捕获,然后存储在数据库中以在此处读取和显示:
The print statements while inserting the image in database are showing positive results.
Even while reading from the database, in the print statement's output I can see the Image data.
But I am unable to display it. How do verify where the problem lies?
-------------------------- File: sqlfhelper.dart -----------------------------
Table contains single item BLOB.
class SQLHelper {
static Future<void> createTables(sql.Database database) async
{
await database
.execute("CREATE TABLE items1(id INTEGER PRIMARY KEY, picture BLOB )");
}
Code for creating table:
static Future<sql.Database> db() async {
return sql.openDatabase(
'kindacode1.db',
version: 1,
onCreate: (sql.Database database, int version) async {
await createTables(database);
},
);
}
Here I pass the Image File to this function, convert it into Uint8List
and then store in the table:
static Future<int> createItem(File? obj) async {
final db = await SQLHelper.db();
Uint8List? pic;
if (obj != null) {
pic = await obj.readAsBytes();
if (pic.isNotEmpty) {
print("VVVV");
}
final data = {'picture': pic};
final id = await db.insert('items1', data,
conflictAlgorithm: sql.ConflictAlgorithm.replace);
return id;
}
For reading from the database:
static Future<List<Map<String, dynamic>>> getItems() async {
final db = await SQLHelper.db();
return db.query('items1', orderBy: "id");
}
---------------------------- File: main.dart -------------------------------
Stateful widget's initState: _refreshJournals()
function reads from the database.
@override
void initState() {
super.initState();
_refreshJournals();
}
Idea is to read from the database and pull out the required image. This is the data that is getting printed by the print statement in the form of numbers.
List<Map<String, dynamic>> _journals = [];
void _refreshJournals() async {
final data = await SQLHelper.getItems();
setState(() {
_journals = data;
if (_journals.isNotEmpty) {
print("WfW\n");
print(data);
_showForm(_journals[15]['id']);
}
});
}
Convert the image from uInt8List to Image using fromRawPath
. I can see the print statements getting fulfilled here.
void _showForm(int? id) async {
if (id != null) {
final existingJournal =
_journals.firstWhere((element) => element['id'] == 15);
if (existingJournal.isNotEmpty) {
print(existingJournal);
myImage = File.fromRawPath(existingJournal['picture']);
}
}
}
Being used as this:
Image.file(
myImage,
scale: 0.5,
width: 20,
height: 20,
fit: BoxFit.scaleDown,
),
It results into this in the GUI:
The strange shape with strange text is the place where the image should be.
Image is captured by camera of the phone and then stored in database to be read and displayed here :
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用内存来从字节呈现。
或使用MemoryImage类:
更多 info :
You can use memory for rendering from bytes.
Or use MemoryImage class:
more info: