Skip to content

Dark camera preview on some Android devices #165491

Closed
flutter/packages
#8891

Description

@davidmartos96

What package does this bug report belong to?

camera

What target platforms are you seeing this bug on?

Android

Have you already upgraded your packages?

Yes

Dependency versions

No response

Steps to reproduce

In production I have an app using the camera_android plugin that just uses the frame streaming feature. I've been pinning version 0.10.8+18 for a while because we found an issue after updating to 0.10.9. Some users reported a dark camera preview.

After some investigation, I believe the issue is because of the call to this.cameraFeatures.setFpsRange in the Camera constructor. That is the only difference in behavior that .9 and .8+18 have.

Setting the fos range to a single value might be interfeering with the camera exposure. See:
https://stackoverflow.com/a/54411665

I propose 2 solutions:

  1. Don't change the default fps range unless providing a non null/nonzero fps parameter.
  2. Move the setFpsRange call to where the plugin starts recording.

P.D. We are waiting on some maturity on the camerax package before making the change. The camera streaming is a critical part on my app. Related #165092

Expected results

No dark preview

Actual results

The camera preview shows dark on some devices.

Code sample

The camera example app

Screenshots or Videos

Logs

No response

Flutter Doctor output

Doctor output
[✓] Flutter (Channel stable, 3.29.2, on Fedora Linux 41 (Workstation Edition)
    6.13.5-200.fc41.x86_64, locale en_US.UTF-8) [97ms]
    • Flutter version 3.29.2 on channel stable at
      /home/david/.local/share/mise/installs/flutter/3.29.2-stable
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision c236373904 (6 days ago), 2025-03-13 16:17:06 -0400
    • Engine revision 18b71d647a
    • Dart version 3.7.2
    • DevTools version 2.42.3

[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0) [2.0s]
    • Android SDK at /home/david/Android/Sdk/
    • Platform android-35, build-tools 35.0.0
    • Java binary at: /home/david/opt/android-studio/jbr/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.5+-13047016-b750.29)
    • All Android licenses accepted.

[✓] Chrome - develop for the web [17ms]
    • Chrome at google-chrome

[✓] Linux toolchain - develop for Linux desktop [226ms]
    • clang version 19.1.7 (Fedora 19.1.7-3.fc41)
    • cmake version 3.30.8
    • ninja version 1.12.1
    • pkg-config version 2.3.0

[✓] Android Studio (version 2024.2) [14ms]
    • Android Studio at /home/david/opt/android-studio/
    • 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
    • android-studio-dir = /home/david/opt/android-studio/
    • Java version OpenJDK Runtime Environment (build 21.0.5+-13047016-b750.29)

[✓] VS Code (version 1.98.2) [13ms]
    • VS Code at /usr/share/code
    • Flutter extension version 3.106.0

[✓] Connected device (3 available) [203ms]
    • CPH2581 (mobile) • b91b13ee • android-arm64  • Android 15 (API 35)
    • Linux (desktop)  • linux    • linux-x64      • Fedora Linux 41 (Workstation Edition)
      6.13.5-200.fc41.x86_64
    • Chrome (web)     • chrome   • web-javascript • Google Chrome 134.0.6998.35

[✓] Network resources [262ms]
    • All expected network resources are available.

• No issues found!

Activity

  1. camsim99 commented on Mar 20, 2025

    @camsim99
    Contributor

    @davidmartos96 This sounds like a good approach to me!

    Also I might be confused but I can't actually find where we actually use the FPS range value in the camera_android plugin. Can you confirm/deny this?

  2. davidmartos96 commented on Mar 20, 2025

    @davidmartos96
    ContributorAuthor

    @camsim99 Great! I opened a draft PR.

    The fps range is configured in the camera features in the Camera constructor, even when not using the fps parameter. https://github.com/flutter/packages/blob/cf9b0f25a06d8e20cac853e5b5a0631dd1a4656c/packages/camera/camera_android/android/src/main/java/io/flutter/plugins/camera/Camera.java#L249

    On my device I don't see a dark preview, but this will change the default [15, 30] range obtained when creating the FPS camera feature into [30, 30]

    The value is used here when creating the CaptureRequest https://github.com/flutter/packages/blob/cf9b0f25a06d8e20cac853e5b5a0631dd1a4656c/packages/camera/camera_android/android/src/main/java/io/flutter/plugins/camera/features/fpsrange/FpsRangeFeature.java#L93

  3. added
    platform-androidAndroid applications specifically
    packageflutter/packages repository. See also p: labels.
    team-androidOwned by Android platform team
    and removed
    in triagePresently being triaged by the triage team
    on Mar 20, 2025
  4. camsim99 commented on Mar 20, 2025

    @camsim99
    Contributor

    Gotcha! Also oops meant to comment on your PR. Confirming here that option 2 seems reasonable to me. I am gonna look a bit deeper into the code though and see if 1 is a better option, though, now that I have a better understanding of this change.

  5. camsim99 commented on Mar 22, 2025

    @camsim99
    Contributor

    My main concern with option 2 over option 1 is if someone is ok with impacting the camera preview and we take away that option. It's possible that it impacting the preview is a bug on Android's side but in quickly researching, I don't see clear evidence of that. If you're convinced that is okay, then I'm ok moving forward.

  6. davidmartos96 commented on Mar 22, 2025

    @davidmartos96
    ContributorAuthor

    @camsim99
    Yes, it's true that it would "break" if the user wanted a low fps preview.

    At the same time, if the use case would be streaming at a lower rate, the user can skip frames on the image stream callback and have a smooth preview too.
    That's what we are doing on our app.

    In any case, if we were to do approach 1, wouldn't it be necessary to update the camera feature FPS when we start recording? As the current PR is doing.
    Otherwise, when not specifying an FPS and we start recording, we would have the feature with a range such as 15-30 (default), instead of 30-30. From what I've read it's recommended to use a fixed fps for recording. But I'm no expert on the camera native APIs and features.

  7. added
    P1High-priority issues at the top of the work list
    triaged-androidTriaged by Android platform team
    on Mar 25, 2025
  8. github-actions commented on Apr 14, 2025

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  9. locked as resolved and limited conversation to collaborators on Apr 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P1High-priority issues at the top of the work listp: cameraThe camera pluginpackageflutter/packages repository. See also p: labels.platform-androidAndroid applications specificallyteam-androidOwned by Android platform teamtriaged-androidTriaged by Android platform team

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions