扑-画布drawImage返回空白

时间:2019-02-01 14:58:36

标签: flutter android-canvas flutter-layout flutter-animation

我正在尝试使用Canvas和Picture Recorder创建图像。

我能够使用正确输出两个圆圈的图像。

c.drawCircle(offset, 20.0, paint);
c.drawCircle(offset2, 20.0, paint);

但是使用以下代码将返回空白图像

c.drawImage(imagetoDraw, offset3, paint);

这就是我导入 imagetoDraw 资产

的方式
  getImageFromAsset(){
    rootBundle.load("images/1.jpg").then( (bd) {
      Uint8List lst = new Uint8List.view(bd.buffer);
      ui.instantiateImageCodec(lst).then( (codec) {
        codec.getNextFrame().then(
                (frameInfo) {
              imagetoDraw = frameInfo.image;
              print ("bkImage instantiated: $imagetoDraw");
            }
      );
    });
  });
  }

我想念什么?

完整代码:

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

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;
  ui.Image imagetoDraw;

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

    getImageFromAsset();
  }

  getImageFromAsset(){
    rootBundle.load("images/1.jpg").then( (bd) {
      Uint8List lst = new Uint8List.view(bd.buffer);
      ui.instantiateImageCodec(lst).then( (codec) {
        codec.getNextFrame().then(
                (frameInfo) {
              imagetoDraw = frameInfo.image;
              print ("bkImage instantiated: $imagetoDraw");
            }
      );
    });
  });
  }

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

  Future<Image> _generate() async {
    ui.PictureRecorder recorder = new ui.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(10.0, 10.0);
    final offset2 = new Offset(90.0, 90.0);

    final offset3 = new Offset(30.0, 30.0);

    c.drawCircle(offset, 20.0, paint);
    c.drawCircle(offset2, 20.0, paint);

    c.drawImage(imagetoDraw, offset3, paint);

    var picture = recorder.endRecording();

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

    var image = Image.memory(pngBytes.buffer.asUint8List());
    return image;
  }

  @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),
      ),
    );
  }
}

1 个答案:

答案 0 :(得分:0)

这似乎是一个与 GPU 相关的问题,如评论中所述,flutter run --enable-software-rendering 可以是一种解决方法。我已经在本地尝试过,它运行良好,没有问题。