一尘不染

可以用Dart / Flutter绘制图像吗?

flutter

我正在寻找从flutter应用程序中生成图像(jpeg或png)的路径。图片将由圆圈,线条,文字等组成。

确实确实存在一种使用画布在屏幕上绘画的方法(https://docs.flutter.io/flutter/dart-
ui/Canvas/Canvas.html),但是似乎不存在与之等效的方法创建可以在应用程序内部显示或发送/使用的图像。

有没有可以绘制图像的飞镖库?考虑到潜在的skia框架,这似乎是可能的。在Dart-html包中,有一个CanvasRenderingContext2D。

编辑:开始以下工作(根据Richard的建议)将是一个开始:

import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'dart:ui';
import 'dart:typed_data';
import 'dart:async';
import 'dart:io';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  Image _image;

  @override
  void initState() {
    super.initState();
    _image = new Image.network(
      'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png',
    );
  }

  Future<String> get _localPath async {
    final directory =
        await getApplicationDocumentsDirectory(); //From path_provider package
    return directory.path;
  }

  Future<File> get _localFile async {
    final path = await _localPath;
    return new File('$path/tempImage.png');
  }

  Future<File> writeImage(ByteData pngBytes) async {
    final file = await _localFile;
    // Write the file
    file.writeAsBytes(pngBytes.buffer.asUint8List());
    return file;
  }

  _generateImage() {
    _generate().then((val) => setState(() {
          _image = val;
        }));
  }

  Future<Image> _generate() async {
    PictureRecorder recorder = new PictureRecorder();
    Canvas c = new Canvas(recorder);
    var rect = new Rect.fromLTWH(0.0, 0.0, 100.0, 100.0);
    c.clipRect(rect);

    final paint = new Paint();
    paint.strokeWidth = 2.0;
    paint.color = const Color(0xFF333333);
    paint.style = PaintingStyle.fill;

    final offset = new Offset(50.0, 50.0);
    c.drawCircle(offset, 40.0, paint);
    var picture = recorder.endRecording();

    final pngBytes =
        await picture.toImage(100, 100).toByteData(format: ImageByteFormat.png);

    //Aim #1. Upade _image with generated image.
    var image = Image.memory(pngBytes.buffer.asUint8List());
    return image;

    //new Image.memory(pngBytes.buffer.asUint8List());
    // _image = new Image.network(
    //   'https://github.com/flutter/website/blob/master/_includes/code/layout/lakes/images/lake.jpg?raw=true',
    // );

    //Aim #2. Write image to file system.
    //writeImage(pngBytes);
    //Make a temporary file (see elsewhere on SO) and writeAsBytes(pngBytes.buffer.asUInt8List())
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            _image,
          ],
        ),
      ),
      floatingActionButton: new FloatingActionButton(
        onPressed: _generateImage,
        tooltip: 'Generate',
        child: new Icon(Icons.add),
      ),
    );
  }
}

阅读 303

收藏
2020-08-13

共1个答案

一尘不染

PictureRecorder可让您创建画布,使用画布绘制方法并提供endRecording()返回Picture。您可以将此图片绘制到其他场景或画布上,或用于.toImage(width,height).toByteData(format)将其转换为PNG(或原始格式-不支持jpeg)。

例如:

import 'dart:ui';
import 'dart:typed_data';
....
  PictureRecorder recorder = new PictureRecorder();
  Canvas c = new Canvas(recorder);
  c.drawPaint(paint); // etc
  Picture p = recorder.endRecording();
  ByteData pngBytes =
      await p.toImage(100, 100).toByteData(format: ImageByteFormat.png);

确保您使用的是Flutter 0.4.4,否则您可能没有format可用的参数。

但是,看完您的编辑后,我怀疑您确实在寻找CustomPainterWidget在何处提供可以在其上进行绘制的Canvas。

2020-08-13