原文地址html
首先,你要了解flutter如何将canvas转换成图片,这里有一个很是好的例子分享给你们flutter_canvas_to_image 简单讲解一下思路:git
1.首先建立一个 ui.PictureRecorder(记录仪), 而后做为载体建立一个canvas对象github
// 建立一个记录仪
final recorder = new ui.PictureRecorder();
final canvas = new Canvas(
recorder,
new Rect.fromPoints(
new Offset(0.0, 0.0), new Offset(200.0, 200.0)));
复制代码
2.在canvas上进行绘制canvas
final stroke = new Paint()
..color = Colors.grey
..style = PaintingStyle.stroke;
canvas.drawRect(
new Rect.fromLTWH(0.0, 0.0, 200.0, 200.0), stroke);
final paint = new Paint()
..color = color
..style = PaintingStyle.fill;
canvas.drawCircle(
new Offset(
widget.rd.nextDouble() * 200.0,
widget.rd.nextDouble() * 200.0,
),
20.0,
paint);
复制代码
// 关闭记录仪
final picture = recorder.endRecording();
final img = picture.toImage(200, 200);
final pngBytes = await img.toByteData(format: new ui.EncodingFormat.png());
// 显示图片
new Image.memory(new Uint8List.view(imgBytes.buffer));
复制代码
当咱们懂得如何将canvas转为图片以后,咱们简单看下canvas clip的代码(这并非本文主要讲述的内容,因此不作详细说明),咱们以canvas.clipPath截取图片为例bash
// 绘制一个三角形的path
Path _path = Path()
..moveTo(100, 50)
..lineTo(50, 150)
..lineTo(150, 150)
..lineTo(100, 50);
canvas.clipPath(_path);
canvasClip.drawImageRect(
_image, // ui.Image
Rect.fromLTWH(0, 0, _image.width.toDouble(), _image.height.toDouble()),,
Rect.fromLTWH(0, 0, 200, 200), // 画布Rect
Paint()
);
复制代码
咱们已经将图片截取了,这个时候获取的图片大小是200*200的,可是咱们只想要那个三角形怎么办那?思路以下:app
思路有了,咱们看实际解决办法,首先针对第一点,flutter Path 为咱们提供了一个现成的方法 getBounds()
, 看下官网的描述less
getBounds() → Rect
Computes the bounding rectangle for this path.
复制代码
而后看下第二点,咱们能够将以前生成的图片经过位移画在一个新的canvas的左上角,第三步在Step.1 已经充分get了,看下代码:async
// 获取Path围成的矩形
Rect _bounds = _path.getBounds();
// 上一步生成的图片
ui.Image img = await picture.toImage(200, 200);
// 新建一个新的记录仪和canvas
final recorder2 = ui.PictureRecorder();
final canvasClip = Canvas(recorder2, Rect.fromLTWH(0, 0, size.width, size.height));
canvasClip.drawImageRect(
img,
_bound, // _bound 中已经包含左上角的offset,能够直接拿过来用
Rect.fromLTWH(0, 0, _bound.width, _bound.height),
Paint()
);
// 中止录制 生成image
final picture2 = recorder2.endRecording();
ui.Image img2 = await picture2.toImage(size.width.toInt(), size.height.toInt());
final pngBytes = await img2.toByteData(format: ui.ImageByteFormat.png);
复制代码
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:myapp/utils/image.dart';
import 'package:flutter/material.dart';
void main() => runApp(App());
const kCanvasSize = 200.0;
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: ImageGenerator(),
),
debugShowCheckedModeBanner: false,
);
}
}
class ImageGenerator extends StatefulWidget {
@override
_ImageGeneratorState createState() => _ImageGeneratorState();
}
class _ImageGeneratorState extends State<ImageGenerator> {
ByteData imgBytes;
ui.Image _image;
@override
void initState() {
super.initState();
loadImage('assets/images/face.jpg').then((image) {
setState(() {
_image = image;
});
});
}
@override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(12.0),
child: RaisedButton(
child: Text('Generate image'), onPressed: generateImage),
),
imgBytes != null
? Container(
child: Image.memory(
Uint8List.view(imgBytes.buffer),
width: kCanvasSize,
height: kCanvasSize,
))
: Container()
],
),
);
}
void generateImage() async {
final recorder = ui.PictureRecorder();
final canvas = Canvas(recorder,
Rect.fromPoints(Offset(0.0, 0.0), Offset(kCanvasSize, kCanvasSize)));
final stroke = new Paint()
..color = Colors.grey
..style = PaintingStyle.stroke;
canvas.drawRect(Rect.fromLTWH(0.0, 0.0, kCanvasSize, kCanvasSize), stroke);
Path _path = Path()
..moveTo(100, 50)
..lineTo(50, 150)
..lineTo(150, 150)
..lineTo(100, 50);
canvas.clipPath(_path);
Rect _bound = _path.getBounds();
canvas.drawImageRect(
_image,
Rect.fromLTWH(0, 0, _image.width.toDouble(), _image.height.toDouble()),
Rect.fromLTWH(0, 0, 200, 200),
Paint());
final picture = recorder.endRecording();
ui.Image img = await picture.toImage(200, 200);
print('img的尺寸: $img');
final recorder2 = ui.PictureRecorder();
final canvasClip =
Canvas(recorder2, Rect.fromLTWH(0, 0, _bound.width, _bound.height));
canvasClip.drawImageRect(
img, _bound, Rect.fromLTWH(0, 0, _bound.width, _bound.height), Paint());
final picture2 = recorder2.endRecording();
ui.Image img2 =
await picture2.toImage(_bound.width.toInt(), _bound.height.toInt());
print('img2的尺寸: $img2');
final pngBytes = await img2.toByteData(format: ui.ImageByteFormat.png);
setState(() {
imgBytes = pngBytes;
});
}
}
复制代码