读取从颤音中存储在SQLFlite数据库中的图像

发布于 2025-01-24 03:53:38 字数 3031 浏览 0 评论 0原文

在数据库中插入图像时的打印语句将显示积极的结果。
即使从数据库中阅读时,在打印语句的输出中,我也可以看到图像数据。
但是我无法显示它。如何验证问题在哪里?

------------------------------------文件: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 :

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抠脚大汉 2025-01-31 03:53:38

您可以使用内存来从字节呈现。

 child: Image.memory(Uint8List bytes);

或使用MemoryImage类:

var _image = MemoryImage(image);

更多 info

You can use memory for rendering from bytes.

 child: Image.memory(Uint8List bytes);

Or use MemoryImage class:

var _image = MemoryImage(image);

more info:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文