[camera] StartImageStream after stopImageStream returns old imageData.

This issue has been tracked since 2022-11-23.

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 the camera towards the barcode for the second time.

Minimal Code Example

// ignore_for_file: avoid_print

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

late List<CameraDescription> _cameras;

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  _cameras = await availableCameras();
  runApp(const CameraApp());
}

/// CameraApp is the Main Application.
class CameraApp extends StatefulWidget {
  /// Default Constructor
  const CameraApp({Key? key}) : super(key: key);

  @override
  State<CameraApp> createState() => _CameraAppState();
}

class _CameraAppState extends State<CameraApp> {
  late CameraController controller;

  void _processImageFromStream(CameraImage image) {
    print(image.planes[0].bytes[0]);
  }

  @override
  void initState() {
    super.initState();
    controller = CameraController(_cameras[0], ResolutionPreset.low, enableAudio: false);
    controller.initialize().then((_) {
      if (!mounted) {
        return;
      }
      setState(() {});
    }).catchError((Object e) {
      if (e is CameraException) {
        switch (e.code) {
          case 'CameraAccessDenied':
            print('User denied camera access.');
            break;
          default:
            print('Handle other errors.');
            break;
        }
      }
    });
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    if (!controller.value.isInitialized) {
      return Container();
    }
    return MaterialApp(
        home: Column(children: [
      Expanded(child: CameraPreview(controller)),
      GestureDetector(
          onTap: () {
            if (controller.value.isStreamingImages) {
              print('Stopping image stream');
              controller.stopImageStream();
            } else {
              print('Starting new image stream');
              controller.startImageStream(_processImageFromStream);
            }
          },
          child: Container(color: Colors.green, child: const Text('Start or stop image stream')))
    ]));
  }
}

Affected Camera Plugin Version
camera: 0.10.0+4, also reproducible on older versions

Flutter Doctor Output

[✓] Flutter (Channel stable, 3.3.8, on macOS 13.0.1 22A400 darwin-arm, locale en-IS)
    • Flutter version 3.3.8 on channel stable at /Users/a/Documents/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 52b3dc25f6 (2 weeks ago), 2022-11-09 12:09:26 +0800
    • Engine revision 857bd6b74c
    • Dart version 2.18.4
    • DevTools version 2.15.0

[✓] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
    • Android SDK at /Users/a/Library/Android/sdk
    • Platform android-33, build-tools 32.1.0-rc1
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.11+0-b60-7772763)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.1)
    • Xcode at /Applications/Xcode-14.1.0.app/Contents/Developer
    • Build 14B47b
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.11+0-b60-7772763)

[✓] VS Code (version 1.73.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.52.0

[✓] Connected device (3 available)
    • SM S901B (mobile) • RFCT8032PHN • android-arm64  • Android 13 (API 33)
    • macOS (desktop)   • macos       • darwin-arm64   • macOS 13.0.1 22A400 darwin-arm
    • Chrome (web)      • chrome      • web-javascript • Google Chrome 107.0.5304.110

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!

Steps To Reproduce

  1. Point the camera towards light object
  2. Start image stream
  3. Stop the image stream
  4. point camera towards dark object
  5. Start the image stream again
  6. Look at console output

Results

I/flutter (19192): Starting new image stream
I/flutter (19192): 152
I/flutter (19192): 134
I/flutter (19192): 132
I/flutter (19192): 137
I/flutter (19192): 146
I/flutter (19192): 146
I/flutter (19192): 141
I/flutter (19192): 144
I/flutter (19192): Stopping image stream
I/flutter (19192): Starting new image stream
I/flutter (19192): 142
I/flutter (19192): 5
I/flutter (19192): 2
I/flutter (19192): 2
I/flutter (19192): 5
I/flutter (19192): 3
I/flutter (19192): 4
I/flutter (19192): 3
I/flutter (19192): 2
I/flutter (19192): 4
I/flutter (19192): 3
I/flutter (19192): 5
I/flutter (19192): Stopping image stream

Expected Results

I/flutter (19192): Starting new image stream
I/flutter (19192): 152
I/flutter (19192): 134
I/flutter (19192): 132
I/flutter (19192): 137
I/flutter (19192): 146
I/flutter (19192): 146
I/flutter (19192): 141
I/flutter (19192): 144
I/flutter (19192): Stopping image stream
I/flutter (19192): Starting new image stream
I/flutter (19192): 5
I/flutter (19192): 2
I/flutter (19192): 2
I/flutter (19192): 5
I/flutter (19192): 3
I/flutter (19192): 4
I/flutter (19192): 3
I/flutter (19192): 2
I/flutter (19192): 4
I/flutter (19192): 3
I/flutter (19192): 5
I/flutter (19192): Stopping image stream

Device Information
S22(SM-S901B/DS) was used for example above - Android 13 (API 33) - Build number TP1A.220624.014.S901BXXU2BVKB
Reproducible on at least all Android phones I tested, Samsung S22, OnePlus 6, LG K40s, Samsung A12.
Have not been able to produce on IOS.

atlid8 wrote this answer on 2022-11-23

Has never happened on IOS, only reproducible on Android(tested on s22 in the example above, also tested on older LG phone and their I got more then one imageData that was old)

huycozy wrote this answer on 2022-11-24

Hi @atlid8
To be more precise, please provide the information indicated below so that we can verify the issue properly:

Also, you may want to refer to writing and formatting syntax for beautifully formatted issue description.

Thank you!

atlid8 wrote this answer on 2022-11-24

Edit done @huycozy

huycozy wrote this answer on 2022-11-25

@atlid8 thanks for updating the issue with more information. This issue is reproducible on the latest stable and master channels with Pixel 3a, Android 12. Labeling the issue for further insights from the team.

Demo video
Screen.Recording.2022-11-25.at.10.26.46.mp4
flutter doctor -v (stable and master)
[✓] Flutter (Channel stable, 3.3.9, on macOS 13.0 22A380 darwin-x64, locale en-VN)
    • Flutter version 3.3.9 on channel stable at /Users/huynq/Documents/GitHub/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision b8f7f1f986 (31 hours ago), 2022-11-23 06:43:51 +0900
    • Engine revision 8f2221fbef
    • Dart version 2.18.5
    • DevTools version 2.15.0

[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at /Users/huynq/Library/Android/sdk
    • Platform android-33, build-tools 31.0.0
    • ANDROID_HOME = /Users/huynq/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.0.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14A400
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)

[✓] IntelliJ IDEA Community Edition (version 2022.2.2)
    • IntelliJ at /Applications/IntelliJ IDEA CE.app
    • Flutter plugin version 70.0.5
    • Dart plugin version 222.4167.21

[✓] IntelliJ IDEA Community Edition (version 2022.1.1)
    • IntelliJ at /Users/huynq/Library/Application Support/JetBrains/Toolbox/apps/IDEA-C/ch-0/221.5591.52/IntelliJ IDEA CE.app
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart

[✓] VS Code (version 1.73.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.52.0

[✓] Connected device (3 available)
    • SM T225 (mobile) • R9JT3004VRJ • android-arm64  • Android 12 (API 31)
    • macOS (desktop)  • macos       • darwin-x64     • macOS 13.0 22A380 darwin-x64
    • Chrome (web)     • chrome      • web-javascript • Google Chrome 107.0.5304.110

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!
[!] Flutter (Channel master, 3.6.0-7.0.pre.52, on macOS 13.0 22A380 darwin-x64, locale en-VN)
    • Flutter version 3.6.0-7.0.pre.52 on channel master at /Users/huynq/Documents/GitHub/flutter_master
    ! Warning: `flutter` on your path resolves to /Users/huynq/Documents/GitHub/flutter/bin/flutter, which is not inside your current Flutter SDK checkout at /Users/huynq/Documents/GitHub/flutter_master. Consider adding /Users/huynq/Documents/GitHub/flutter_master/bin to the front of your path.
    ! Warning: `dart` on your path resolves to /Users/huynq/Documents/GitHub/flutter/bin/dart, which is not inside your current Flutter SDK checkout at /Users/huynq/Documents/GitHub/flutter_master. Consider adding /Users/huynq/Documents/GitHub/flutter_master/bin to the front of your path.
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision ff59250dbe (4 hours ago), 2022-11-24 18:08:30 -0500
    • Engine revision 7665ae5184
    • Dart version 2.19.0 (build 2.19.0-429.0.dev)
    • DevTools version 2.19.0
    • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades.

[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at /Users/huynq/Library/Android/sdk
    • Platform android-33, build-tools 31.0.0
    • ANDROID_HOME = /Users/huynq/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.0.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14A400
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)

[✓] IntelliJ IDEA Community Edition (version 2022.2.2)
    • IntelliJ at /Applications/IntelliJ IDEA CE.app
    • Flutter plugin version 70.0.5
    • Dart plugin version 222.4167.21

[✓] IntelliJ IDEA Community Edition (version 2022.1.1)
    • IntelliJ at /Users/huynq/Library/Application Support/JetBrains/Toolbox/apps/IDEA-C/ch-0/221.5591.52/IntelliJ IDEA CE.app
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart

[✓] VS Code (version 1.73.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.52.0

[✓] Connected device (3 available)
    • Pixel 3a (mobile) • 964AY0WL20 • android-arm64  • Android 12 (API 32)
    • macOS (desktop)   • macos      • darwin-x64     • macOS 13.0 22A380 darwin-x64
    • Chrome (web)      • chrome     • web-javascript • Google Chrome 107.0.5304.110

[✓] HTTP Host Availability
    • All required HTTP hosts are available

! Doctor found issues in 1 category.
More Details About Repo
Owner Name flutter
Repo Name flutter
Full Name flutter/flutter
Language Dart
Created Date 2015-03-06
Updated Date 2022-12-07
Star Count 147031
Watcher Count 3560
Fork Count 23915
Issue Count 11300

YOU MAY BE INTERESTED

Issue Title Created Date Updated Date