问题
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