Skip to content

[camera_android_camerax] takePicture sets an unchanged flash mode on every capture #195468

Closed
flutter/packages
#13468

Description

@toyaji

Once an app calls setFlashMode, every takePicture() sets the same flash mode on
ImageCapture again, which adds roughly 250-300 ms to each capture.
This happens
even when the mode is FlashMode.off and never changes.

Steps to reproduce

  1. Create a camera with ResolutionPreset.high.
  2. Call setFlashMode(FlashMode.off).
  3. Call takePicture() repeatedly and measure the latency.

Results

ZSL off ZSL on
setFlashMode never called 517 ms 360 ms
setFlashMode(FlashMode.off) called once 794 ms 656 ms

Galaxy S21 (SM-G991N), camera_android_camerax 0.7.6, CameraX 1.6.2. Each value is
the median of 12 shots, after 2 warm-up shots, 600 ms apart. "ZSL on" means
setZeroShutterLagEnabled(true).

Why

takePicture calls ImageCapture.setFlashMode before every capture whenever a
flash mode has been set (since flutter/packages#3800). CameraX keeps the flash mode
on the ImageCapture instance for subsequent captures, so the repeated call does not
change anything. In CameraX 1.6.2, ImageCapture.setFlashMode has no check for an
unchanged mode, so each call still goes through CameraControl.setFlashMode. (The
androidx-main source has since added that check.)

Proposed fix

Only call ImageCapture.setFlashMode in takePicture when the mode differs from the
one last applied to the current ImageCapture instance. The torch behavior from
flutter/packages#4903 (clearing a previous flash mode when torch is enabled) stays
the same.

Activity

  1. toyaji commented on Oct 28, 2026

    @toyaji
    Author
  2. added
    in triage
    on Oct 28, 2026
  3. saurabh-mirajkar commented on Oct 28, 2026

    @saurabh-mirajkar
    Contributor

    @toyaji Thanks for the report. Could you please provide :

    • A minimal reproducible example (MRE) or a self-contained main.dart file
    • Android version and API level on the Galaxy S21
    • Output of flutter doctor -v
  4. added
    waiting for response
    and removed
    in triage
    on Oct 28, 2026
  5. toyaji commented on Oct 28, 2026

    @toyaji
    Author

    @saurabh-mirajkar Thanks for looking at this. Here is the requested information.

    Device: Galaxy S21 (SM-G991N), Android 15 (API 35).
    Packages: camera 0.12.2, camera_android_camerax 0.7.6.

    Minimal reproducible example

    A new app (flutter create) with camera: ^0.12.2 added and this lib/main.dart. It needs the camera permission. Tap the button; it prints the median takePicture() latency with ResolutionPreset.high, without and with a setFlashMode(FlashMode.off) call.

    import 'package:camera/camera.dart';
    import 'package:flutter/material.dart';
    
    void main() => runApp(const MaterialApp(home: FlashModePage()));
    
    /// Median `takePicture()` latency in ms over [shots] captures, after 2 warm-up
    /// captures.
    Future<int> measure(CameraController controller, {int shots = 12}) async {
      final ms = <int>[];
      for (var i = 0; i < shots + 2; i++) {
        final sw = Stopwatch()..start();
        await controller.takePicture();
        sw.stop();
        if (i >= 2) {
          ms.add(sw.elapsedMilliseconds);
        }
        await Future<void>.delayed(const Duration(milliseconds: 600));
      }
      ms.sort();
      return ms[ms.length ~/ 2];
    }
    
    Future<String> runComparison() async {
      final CameraDescription back = (await availableCameras()).firstWhere(
        (CameraDescription c) => c.lensDirection == CameraLensDirection.back,
      );
      final lines = <String>[];
      for (final setFlashOff in <bool>[false, true]) {
        final controller = CameraController(back, ResolutionPreset.high, enableAudio: false);
        await controller.initialize();
        if (setFlashOff) {
          await controller.setFlashMode(FlashMode.off);
        }
        await Future<void>.delayed(const Duration(seconds: 2));
        final int median = await measure(controller);
        await controller.dispose();
        lines.add('setFlashMode(off) called: $setFlashOff -> $median ms');
      }
      return lines.join('\n');
    }
    
    class FlashModePage extends StatefulWidget {
      const FlashModePage({super.key});
    
      @override
      State<FlashModePage> createState() => _FlashModePageState();
    }
    
    class _FlashModePageState extends State<FlashModePage> {
      String _result = 'Tap the button to run.';
      bool _running = false;
    
      Future<void> _run() async {
        setState(() => _running = true);
        final String result = await runComparison();
        debugPrint(result);
        setState(() {
          _result = result;
          _running = false;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(title: const Text('setFlashMode and takePicture latency')),
          body: Padding(padding: const EdgeInsets.all(16), child: Text(_result)),
          floatingActionButton: FloatingActionButton(
            onPressed: _running ? null : _run,
            child: _running ? const CircularProgressIndicator() : const Icon(Icons.play_arrow),
          ),
        );
      }
    }

    Output on the Galaxy S21:

    setFlashMode(off) called: false -> 522 ms
    setFlashMode(off) called: true -> 811 ms
    

    Calling setFlashMode(FlashMode.off) once adds about 290 ms to every capture, even though the mode never changes.

    flutter doctor -v
    [✓] Flutter (Channel stable, 3.44.1, on macOS 26.6.2 25G83 darwin-arm64, locale ko-KR) [1,000ms]
        • Flutter version 3.44.1 on channel stable at /Users/paul/projects/flutter
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision 924134a44c (5달 전), 2026-05-29 12:13:22 -0400
        • Engine revision c416acfeb8
        • Dart version 3.12.1
        • DevTools version 2.57.0
        • Feature flags: enable-web, enable-linux-desktop, enable-macos-desktop, enable-windows-desktop, enable-android, enable-ios, cli-animations, enable-native-assets, enable-swift-package-manager, omit-legacy-version-file, enable-lldb-debugging, enable-uiscene-migration
    
    [!] Android toolchain - develop for Android devices (Android SDK version 37.0.0) [3.7s]
        • Android SDK at /Users/paul/Library/Android/sdk
        • Emulator version 37.1.11.0 (build_id 15917651) (CL:N/A)
        • Platform android-37.0, build-tools 37.0.0
        • ANDROID_HOME = /Users/paul/Library/Android/sdk
        • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
          This is the JDK bundled with the latest Android Studio installation on this machine.
          To manually set the JDK path, use: `flutter config --jdk-dir="path/to/jdk"`.
        • Java version OpenJDK Runtime Environment (build 21.0.7+-13880790-b1038.58)
        ! Some Android licenses not accepted. To resolve this, run: flutter doctor --android-licenses
    
    [✓] Xcode - develop for iOS and macOS (Xcode 26.2) [2.4s]
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Build 17C52
        • CocoaPods version 1.16.2
    
    [✓] Chrome - develop for the web [11ms]
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Connected device (4 available) [14.3s]
        • SM G991N (mobile)    • <redacted>               • android-arm64  • Android 15 (API 35)
        • iPhone (mobile) • <redacted> • ios            • iOS 27.0.1 24A446
        • macOS (desktop)      • macos                     • darwin-arm64   • macOS 26.6.2 25G83 darwin-arm64
        • Chrome (web)         • chrome                    • web-javascript • Google Chrome 156.0.8078.48
    
    [✓] Network resources [532ms]
        • All expected network resources are available.
    
    ! Doctor found issues in 1 category.
    
  6. removed
    waiting for response
    on Oct 28, 2026
  7. added
    in triage
    platform-android
    c: performance
    p: camera
    package
    team-android
    fyi-ecosystem
    and removed
    in triage
    on Oct 28, 2026
  8. mbcorona commented on Oct 28, 2026

    @mbcorona
    Member

    Hi @toyaji, thanks for sharing the clean code sample that reproduces the issue.

    Confirmed in source: takePicture re-applies _currentFlashMode on every shot with no check for whether it changed.

    https://github.com/flutter/packages/blob/10a178cb16d4a05c18b594ab13a99c87e9081b54/packages/camera/camera_android_camerax/lib/src/android_camera_camerax.dart#L1022-L1031

    Routing to team-android. Fix is up in 13468

  9. added
    P1
    triaged-android
    on Oct 28, 2026
  10. added
    triaged-ecosystem
    on Nov 17, 2026
  11. removed
    fyi-ecosystem
    triaged-ecosystem
    on Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P1c: performancep: camerapackageplatform-androidteam-androidtriaged-android

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions