Convert Image object to rgb pixel array and back in Flutter

半城伤御伤魂 提交于 2021-02-07 12:48:17

问题


Is there a way to get an image's rgb matrix representation? and vice versa? I would like to perform image masking/filtering on the original image so it needs to be applied to its rgb matrix representation. Currently using this library to get an image from a device: https://pub.dartlang.org/packages/image_picker


回答1:


https://pub.dartlang.org/packages/image provides image conversion and manipulation utility functions.




回答2:


import 'dart:ui' as ui;
import 'package:flutter/material.dart';

// for a local iamge example
List RGBAList;
// 1. get [ImageProvider] instance
//    [ExactAssetImage] extends [AssetBundleImageProvider] extends [ImageProvider]
ExactAssetImage provider = ExactAssetImage('$local_img_uri');

// 2. get [ui.Image] by [ImageProvider]
ImageStream stream = provider.resolve(ImageConfiguration.empty);
Completer completer = Completer<ui.Image>();
ImageStreamListener listener = ImageStreamListener((frame, sync) {
    ui.Image image = frame.image;
    completer.complete(image);
    stream.removeListener(listener);
})
stream.addListener(listener);

// 3. get rgba array/list by [ui.Image]
completer.then((ui.Image image) {
    image.toByteData(format: ui.ImageByteFormat.rowRgba).then((ByteData data) {
        RGBAList = data.buffer.asUint8List().toList();
    }); 
})


来源:https://stackoverflow.com/questions/46386208/convert-image-object-to-rgb-pixel-array-and-back-in-flutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!