Post

Flutter Camera ImageStream Issue: Previous Frame Sticks Around on Restart

Flutter Camera ImageStream Issue: Previous Frame Sticks Around on Restart

This post was migrated from Tistory. You can find the original here.

An issue in the Flutter Camera plugin where restarting an ImageStream causes it to start from a leftover previous frame instead of the latest one.

Camera Plugin

https://pub.dev/packages/camera

Environment

flutter: 3.32.7

camera: ^0.10.0+1

android: 10 ~ 15

Problem

When calling controller.startImageStream -> stopImageStream -> startImageStream,

at the point of restart, the stream doesn’t give you the latest frame — instead, one frame left over from before the stop still comes through first.

Solution

https://github.com/flutter/flutter/issues/115925?utm_source=chatgpt.com

[[camera] StartImageStream after stopImageStream returns old imageData. · Issue #115925 · flutter/flutter

Description ImageStream returns old imageData when imageStream is started after stopImageStream call. This is causing barcode scanner to find the same barcode twice even though user is not pointing…

github.com](https://github.com/flutter/flutter/issues/115925?utm_source=chatgpt.com)

Given that it’s always exactly one leftover frame,

and based on the discussion in that issue, it seems like there’s a timing gap between the frame buffer and the broker.

So this isn’t really a root-cause fix —

instead, by taking advantage of lexical closures to keep a skipped variable alive inside a wrapper function,

we simply skip the first few frames after calling startImageStream.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  Future<void> startSkipImageStream({
    required CameraController controller,
    required void Function(CameraImage image) listener,
    int skipFrameCount = 1,
  }) async {
    int skipped = 0;

    await controller.startImageStream((image) {
      if (skipped < skipFrameCount) {
        skipped++;
        return;
      }

      listener(image);
    });
  }
This post is licensed under CC BY-NC 4.0 by the author.