在Flutter应用中是否有用于图像编辑器的API。我需要在图像中添加文字

时间:2019-03-23 08:40:18

标签: image flutter

是否可以像旋转一样编辑图像并在图像上添加文本。是否有任何插件?我需要图像编辑器来添加具有各种字体和颜色的文本。谢谢

2 个答案:

答案 0 :(得分:0)

您应该使用 RepaintBoundary 将其捕获为小部件,并使用stack来重叠小部件。

看看这段代码如何将小部件捕获为图像。

import 'dart:async';
import 'dart:convert';
import 'dart:typed_data';
import 'dart:ui' as ui;

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  GlobalKey _globalKey = new GlobalKey();

  bool inside = false;
  Uint8List imageInMemory;

  Future<Uint8List> _capturePng() async {
    try {
      print('inside');
      inside = true;
      RenderRepaintBoundary boundary =
      _globalKey.currentContext.findRenderObject();
      ui.Image image = await boundary.toImage(pixelRatio: 3.0);
      ByteData byteData =
      await image.toByteData(format: ui.ImageByteFormat.png);
      Uint8List pngBytes = byteData.buffer.asUint8List();
//      String bs64 = base64Encode(pngBytes);
//      print(pngBytes);
//      print(bs64);
      print('png done');
      setState(() {
        imageInMemory = pngBytes;
        inside = false;
      });
      return pngBytes;
    } catch (e) {
      print(e);
    }
  }

  @override
  Widget build(BuildContext context) {
    return RepaintBoundary(
      key: _globalKey,
      child: new Scaffold(
          appBar: new AppBar(
            title: new Text('Widget To Image demo'),
          ),
          body: SingleChildScrollView(
            child: Center(
              child: new Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  new Text(
                    'click the button below to capture image',
                  ),
                  new RaisedButton(
                    child: Text('capture Image'),
                    onPressed: _capturePng,
                  ),
                  inside ? CircularProgressIndicator()
                      :
                  imageInMemory != null
                      ? Container(
                      child: Image.memory(imageInMemory),
                      margin: EdgeInsets.all(10))
                      : Container(),
                ],
              ),
            ),
          )),
    );
  }
}

答案 1 :(得分:0)

其他(更有效的方法)是在图像上Stack文本小部件。 Here is whole tutorial for that