Mar*_*rcG 10 image-resizing dart flutter
在Flutter/Dart中,我该如何执行以下3个步骤:
注意:我必须能够使用常规的Flutter Image小部件显示最终结果.
澄清:我不想保存图像,但我确实想在内存中实际调整大小.
Rao*_*che 14
您可以使用image.file构造函数从磁盘读取图像.
有关更多功能,您可以使用图像库
Dart库,提供以各种不同文件格式加载,保存和操作图像的功能.
文档示例中的示例
加载jpeg,调整大小,并将其另存为png
import 'dart:io' as Io;
import 'package:image/image.dart';
void main() {
// Read a jpeg image from file.
Image image = decodeImage(new Io.File('test.jpg').readAsBytesSync());
// Resize the image to a 120x? thumbnail (maintaining the aspect ratio).
Image thumbnail = copyResize(image, 120);
// Save the thumbnail as a PNG.
new Io.File('out/thumbnail-test.png')
..writeAsBytesSync(encodePng(thumbnail));
}
Run Code Online (Sandbox Code Playgroud)
kor*_*ara 12
通过图片库调整图片大小不是一个很好的方法,因为它会阻塞ui线程,并且会带来非常糟糕的用户体验。lib中有一个maxWidth参数image_picker,你可以设置它,所以在某些情况下这些写文件操作将是不必要的。
使用ResizeImage图像提供程序。
如果您想使用许多功能,或者如果您不能这样做,则使用单独的包是很好的。但只是依赖于某些东西而不是框架本身(及其底层图形引擎)可以轻松完成的事情...... :-)
如果你有一个ImageProvider现在,比如说,从内存中的字节中显示一个图像:
Image(image: MemoryImage(bytes))
Run Code Online (Sandbox Code Playgroud)
只需将它包裹在一个ResizeImage:
Image(image: ResizeImage(MemoryImage(bytes), width: 50, height: 100))
Run Code Online (Sandbox Code Playgroud)
如果您想要更多控制,只需根据此源代码创建您自己的图像提供程序。
这是一个Thumbnail在飞行中执行此操作的示例小部件
它用于Isolate将 CPU 密集型工作卸载到后台线程并使 UI 线程无卡顿
import 'dart:io';
import 'dart:isolate';
import 'package:flutter/material.dart';
import 'package:image/image.dart' as IMG;
import 'package:path/path.dart';
class Thumbnail extends StatefulWidget {
final Size size;
final File image;
const Thumbnail({Key key, this.size, this.image}) : super(key: key);
@override
_ThumbnailState createState() => _ThumbnailState();
}
class _ThumbnailState extends State<Thumbnail> {
List<int> imgBytes;
Isolate isolate;
@override
void initState() {
_asyncInit();
super.initState();
}
static _isolateEntry(dynamic d) async {
final ReceivePort receivePort = ReceivePort();
d.send(receivePort.sendPort);
final config = await receivePort.first;
print(config);
final file = File(config['path']);
final bytes = await file.readAsBytes();
IMG.Image image = IMG.decodeImage(bytes);
IMG.Image thumbnail = IMG.copyResize(
image,
width: config['size'].width.toInt(),
);
d.send(IMG.encodeNamedImage(thumbnail, basename(config['path'])));
}
_asyncInit() async {
final ReceivePort receivePort = ReceivePort();
isolate = await Isolate.spawn(_isolateEntry, receivePort.sendPort);
receivePort.listen((dynamic data) {
if (data is SendPort) {
if (mounted) {
data.send({
'path': widget.image.path,
'size': widget.size,
});
}
} else {
if (mounted) {
setState(() {
imgBytes = data;
});
}
}
});
}
@override
void dispose() {
if (isolate != null) {
isolate.kill();
}
super.dispose();
}
@override
Widget build(BuildContext context) {
return SizedBox(
height: widget.size.height,
width: widget.size.width,
child: imgBytes != null
? Image.memory(
imgBytes,
fit: BoxFit.cover,
)
: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.grey[100], Colors.grey[300]],
begin: Alignment.centerLeft,
end: Alignment.centerRight,
),
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
有很多解决方案:
ResizeImage类指示 Flutter 以指定尺寸而不是其原始大小解码图像。
用法:只需用 ResizeImage 类包装您的ImageProvider
例子 :
Image(image: ResizeImage(AssetImage('eg.png'), width: 70, height: 80)),
Run Code Online (Sandbox Code Playgroud)
ImageProvider包括
AssetImage、和。NetworkImageFileImageMemoryImage
cacheHeight 和 cacheWidth属性这些属性创建一个小部件,显示从资产、网络、内存或文件获取的 [ImageStream]。
例子 :
Image.asset('assets/image.png', cacheHeight:120 , cacheWidth: 150),
Run Code Online (Sandbox Code Playgroud)
Image.asset、Image.network和Image.file中 有这些属性Image.memory
小智 6
要调整pubspec.yaml中定义的图像的大小,请使用“ BoxFit”:
@override
Widget build(BuildContext context) {
return (new Container(
width: 250.0,
height: 250.0,
alignment: Alignment.center,
decoration: new BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/Launcher_Icon.png'),
fit: BoxFit.fill
),
),
));
}
Run Code Online (Sandbox Code Playgroud)
另请参考如何访问图像:https : //flutter.io/assets-and-images/
小智 5
您可以使用 dart ui 库中的图像类,使用 inantiateImageCodec 中的 frameInfo 获取所需宽度和高度的图像对象,然后将其保存在所需路径中
import 'dart:ui' as ui;
Uint8List m = File(path).readAsBytesSync();
ui.Image x = await decodeImageFromList(m);
ByteData bytes = await x.toByteData();
print('height is ${x.height}'); //height of original image
print('width is ${x.width}'); //width of oroginal image
print('array is $m');
print('original image size is ${bytes.lengthInBytes}');
ui.instantiateImageCodec(m, targetHeight: 800, targetWidth: 600)
.then((codec) {
codec.getNextFrame().then((frameInfo) async {
ui.Image i = frameInfo.image;
print('image width is ${i.width}');//height of resized image
print('image height is ${i.height}');//width of resized image
ByteData bytes = await i.toByteData();
File(path).writeAsBytes(bytes.buffer.asUint32List());
print('resized image size is ${bytes.lengthInBytes}');
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16077 次 |
| 最近记录: |