Search code examples
flutterdartvideo-capture

Capture video to canvas flutter


I would like to implement a video to canvas app like my example in js : http://jsfiddle.net/Ls9kfwot/2/

But my problem is how i can take a screenshot of video player in a specific area? like js in drawImage:

ctx.drawImage(sx,sy,swidth,sheight,x,y,width,height); // js drawimage

but in Flutter what can i use for reproduce video to canvas?

i created a simple flutter project with a video player:

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: YoYoPlayer(
        aspectRatio: 16 / 9,
        url:
            "http://upload.wikimedia.org/wikipedia/commons/7/79/Big_Buck_Bunny_small.ogv",
        videoStyle: VideoStyle(),
        videoLoadingStyle: VideoLoadingStyle(
          loading: Center(
            child: Text("Loading video"),
            //capture image of video and display it
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }

RenderRepaintBoundary and drawImage is the solution?


Solution

  • Yep, RenderRepaintBoundary and drawImage is what you want.

      GlobalKey _repaintKey = GlobalKey();
    
      @override
      Widget build(BuildContext context) {
    
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: RenderRepaintBoundary(
            key: _repaintKey,
            child: YoYoPlayer(
              aspectRatio: 16 / 9,
              url:
                  "http://upload.wikimedia.org/wikipedia/commons/7/79/Big_Buck_Bunny_small.ogv",
              videoStyle: VideoStyle(),
              videoLoadingStyle: VideoLoadingStyle(
                loading: Center(
                  child: Text("Loading video"),
                  //capture image of video and display it
                ),
              ),
            ),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: _incrementCounter,
            tooltip: 'Increment',
            child: Icon(Icons.add),
          ), // This trailing comma makes auto-formatting nicer for build methods.
        );
      }
    
    final boundary = _repaintKey.context.findRenderObject() as RenderRepaintBoundary;
    final image = await boundary.toImage();
    canvas.drawImage(image, Offset(10, 20), paint);