【问题标题】:Is there any api for Image editor in flutter app. I need adding texts in image颤振应用程序中是否有任何用于图像编辑器的 api。我需要在图像中添加文本
【发布时间】:2019-08-14 04:10:48
【问题描述】:

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

【问题讨论】:

    标签: image flutter


    【解决方案1】:

    您应该使用 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(),
                    ],
                  ),
                ),
              )),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-01-01
      • 2020-12-26
      • 1970-01-01
      • 2019-05-27
      • 2021-10-13
      • 2021-09-03
      • 2019-08-01
      • 1970-01-01
      • 2021-05-29
      相关资源
      最近更新 更多