flutter / flutter

Flutter makes it easy and fast to build beautiful apps for mobile and beyond
https://flutter.dev
BSD 3-Clause "New" or "Revised" License
162.28k stars 26.66k forks source link

[RepaintBoundary]Cannot take Screenshot of Platform Views #102866

Open xlyashuk opened 2 years ago

xlyashuk commented 2 years ago

Steps to Reproduce

  1. Execute flutter run on the code sample
  2. Press icon button to take screenshot from camera preview.

Expected results: Screenshot should contain camera preview picture.

Actual results: Screenshot doesn't include camera view.

I tested it on Samsung S9, Samsung S20 using Flutter 2.10.5 and 2.13.0-0.2.pre. Same issue happen on iOS, tested on iPhone 12 Pro Max, iOS 15.2.1, Flutter 2.10.5.

I'm aware of existing issues like https://github.com/flutter/flutter/issues/25306 which all point to resolved https://github.com/flutter/flutter/issues/83856#issuecomment-1072896951. However I can't confirm that this feature works.

Code sample ```import 'dart:async'; import 'dart:typed_data'; import 'dart:ui'; import 'package:camera/camera.dart'; import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; Future main() async { WidgetsFlutterBinding.ensureInitialized(); final cameras = await availableCameras(); final firstCamera = cameras.first; runApp( MaterialApp( theme: ThemeData.dark(), home: TakePictureScreen( camera: firstCamera, ), ), ); } // class TakePictureScreen extends StatefulWidget { const TakePictureScreen({ Key? key, required this.camera, }) : super(key: key); final CameraDescription camera; @override TakePictureScreenState createState() => TakePictureScreenState(); } class TakePictureScreenState extends State { static GlobalKey boundaryKey = GlobalKey(); late CameraController _controller; late Future _initializeControllerFuture; @override void initState() { super.initState(); _controller = CameraController( widget.camera, ResolutionPreset.veryHigh, ); _initializeControllerFuture = _controller.initialize(); } @override void dispose() { _controller.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return RepaintBoundary( key: boundaryKey, child: Scaffold( appBar: AppBar(title: const Text('Take a Screenshot')), body: FutureBuilder( future: _initializeControllerFuture, builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.done) { return CameraPreview(_controller); } else { return const Center(child: CircularProgressIndicator()); } }, ), floatingActionButton: FloatingActionButton( onPressed: () async { await _initializeControllerFuture; RenderRepaintBoundary? boundary = boundaryKey.currentContext!.findRenderObject() as RenderRepaintBoundary?; var image = await boundary!.toImage(); var byteData = await image.toByteData(format: ImageByteFormat.png); var pngBytes = byteData!.buffer.asUint8List(); await Navigator.of(context).push( MaterialPageRoute( builder: (context) => DisplayPictureScreen( imageData: pngBytes, ), ), ); }, child: const Icon(Icons.camera_alt), ), )); } } // class DisplayPictureScreen extends StatelessWidget { final Uint8List imageData; const DisplayPictureScreen({Key? key, required this.imageData}) : super(key: key); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: const Text('Display the Picture')), body: Image.memory(imageData), ); } } ```
pubspec.yaml ``` name: screenshot_test description: A new Flutter project. publish_to: 'none' # Remove this line if you wish to publish to pub.dev version: 1.0.0+1 environment: sdk: ">=2.12.0 <3.0.0" dependencies: flutter: sdk: flutter cupertino_icons: ^1.0.2 camera: ^0.9.4+21 dev_dependencies: flutter_test: sdk: flutter flutter_lints: ^2.0.0 flutter: uses-material-design: true ```
flutter doctor -v ``` [√] Flutter (Channel beta, 2.13.0-0.2.pre, on Microsoft Windows [Version 10.0.19042.1288], locale ru-RU) • Flutter version 2.13.0-0.2.pre at e:\Programs\flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 8662e22bac (10 days ago), 2022-04-20 08:21:52 -0700 • Engine revision 24a02fa5ee • Dart version 2.17.0 (build 2.17.0-266.5.beta) • DevTools version 2.12.2 [√] Android toolchain - develop for Android devices (Android SDK version 30.0.3) • Android SDK at C:\Users\Alexey\AppData\Local\Android\sdk • Platform android-31, build-tools 30.0.3 • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java • Java version OpenJDK Runtime Environment (build 11.0.11+9-b60-7590822) • All Android licenses accepted. [√] Chrome - develop for the web • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe [√] Visual Studio - develop for Windows (Visual Studio Build Tools 2019 16.5.5) • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools • Visual Studio Build Tools 2019 version 16.5.30104.148 • Windows 10 SDK version 10.0.18362.0 [√] Android Studio (version 2021.1) • Android Studio at C:\Program Files\Android\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 • Java version OpenJDK Runtime Environment (build 11.0.11+9-b60-7590822) [√] VS Code (version 1.64.2) • VS Code at C:\Users\Alexey\AppData\Local\Programs\Microsoft VS Code • Flutter extension can be installed from: https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter [√] Connected device (3 available) • Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.19042.1288] • Chrome (web) • chrome • web-javascript • Google Chrome 100.0.4896.127 • Edge (web) • edge • web-javascript • Microsoft Edge 87.0.664.66 [√] HTTP Host Availability • All required HTTP hosts are available • No issues found! D:\Work\Cars\Tests\Flutter\screenshot_test>flutter doctor -v [√] Flutter (Channel beta, 2.13.0-0.2.pre, on Microsoft Windows [Version 10.0.19042.1288], locale ru-RU) • Flutter version 2.13.0-0.2.pre at e:\Programs\flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 8662e22bac (10 days ago), 2022-04-20 08:21:52 -0700 • Engine revision 24a02fa5ee • Dart version 2.17.0 (build 2.17.0-266.5.beta) • DevTools version 2.12.2 [√] Android toolchain - develop for Android devices (Android SDK version 30.0.3) • Android SDK at C:\Users\Alexey\AppData\Local\Android\sdk • Platform android-31, build-tools 30.0.3 • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java • Java version OpenJDK Runtime Environment (build 11.0.11+9-b60-7590822) • All Android licenses accepted. [√] Chrome - develop for the web • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe [√] Visual Studio - develop for Windows (Visual Studio Build Tools 2019 16.5.5) • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools • Visual Studio Build Tools 2019 version 16.5.30104.148 • Windows 10 SDK version 10.0.18362.0 [√] Android Studio (version 2021.1) • Android Studio at C:\Program Files\Android\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 • Java version OpenJDK Runtime Environment (build 11.0.11+9-b60-7590822) [√] VS Code (version 1.64.2) • VS Code at C:\Users\Alexey\AppData\Local\Programs\Microsoft VS Code • Flutter extension can be installed from: https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter [√] Connected device (3 available) • Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.19042.1288] • Chrome (web) • chrome • web-javascript • Google Chrome 100.0.4896.127 • Edge (web) • edge • web-javascript • Microsoft Edge 87.0.664.66 [√] HTTP Host Availability • All required HTTP hosts are available • No issues found! ```

Screenshots Camera surface view:

Screenshot result:

maheshmnj commented 2 years ago

Hi @xlyashuk, Thanks for filing the issue. I am able to reproduce the issue on stable and the master channel. On Taking the screenshot error is thrown and a blank image is displayed. The Screenshot can be taken without the CameraPreview (Platformview).

A reproducible code sample can be found in the original post.

flutter doctor -v (mac) ``` [✓] Flutter (Channel stable, 2.10.5, on macOS 12.3 21E230 darwin-arm, locale en-IN) • Flutter version 2.10.5 at /Users/mahesh/Documents/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 5464c5bac7 (12 hours ago), 2022-04-18 09:55:37 -0700 • Engine revision 57d3bac3dd • Dart version 2.16.2 • DevTools version 2.9.2 [✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0) • Android SDK at /Users/mahesh/Library/Android/sdk • Platform android-32, build-tools 31.0.0 • ANDROID_HOME = /Users/mahesh/Library/Android/sdk • 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 13.2.1) • Xcode at /Applications/Xcode.app/Contents/Developer • CocoaPods version 1.10.2 [✓] 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) [✓] IntelliJ IDEA Community Edition (version 2021.2.1) • IntelliJ at /Applications/IntelliJ IDEA CE.app • Flutter plugin version 61.2.4 • Dart plugin version 212.5080.8 [✓] VS Code (version 1.65.2) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.39.20220405 [✓] Connected device (4 available) • Redmi K20 Pro (mobile) • 192.168.1.2:55555 • android-arm64 • Android 11 (API 30) • iPhone 12 Pro (mobile) • 535317A4-921C-488F-B234-C2CC6D4AE5A1 • ios • com.apple.CoreSimulator.SimRuntime.iOS-14-5 (simulator) • macOS (desktop) • macos • darwin-arm64 • macOS 12.3 21E230 darwin-arm • Chrome (web) • chrome • web-javascript • Google Chrome 100.0.4896.88 [✓] HTTP Host Availability • All required HTTP hosts are available • No issues found! ``` ``` [✓] Flutter (Channel master, 2.13.0-0.0.pre.808, on macOS 12.3 21E230 darwin-arm, locale en-IN) • Flutter version 2.13.0-0.0.pre.808 at /Users/mahesh/Documents/flutter_master • Upstream repository https://github.com/flutter/flutter.git • Framework revision 2eed8cbf93 (3 days ago), 2022-04-28 22:29:06 -0400 • Engine revision dfdfe0b3b0 • Dart version 2.18.0 (build 2.18.0-66.0.dev) • DevTools version 2.12.2 [✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0) • Android SDK at /Users/mahesh/Library/Android/sdk • Platform android-32, build-tools 31.0.0 • ANDROID_HOME = /Users/mahesh/Library/Android/sdk • 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 13.2.1) • Xcode at /Applications/Xcode.app/Contents/Developer ! CocoaPods 1.10.2 out of date (1.11.0 is recommended). CocoaPods is used to retrieve the iOS and macOS platform side's plugin code that responds to your plugin usage on the Dart side. Without CocoaPods, plugins will not work on iOS or macOS. For more info, see https://flutter.dev/platform-plugins To upgrade see https://guides.cocoapods.org/using/getting-started.html#installation for instructions. [✓] 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) [✓] IntelliJ IDEA Community Edition (version 2021.2.1) • IntelliJ at /Applications/IntelliJ IDEA CE.app • Flutter plugin version 61.2.4 • Dart plugin version 212.5080.8 [✓] VS Code (version 1.66.2) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.39.20220405 [✓] Connected device (3 available) • Redmi K20 Pro (mobile) • 192.168.1.2:5553 • android-arm64 • Android 11 (API 30) • macOS (desktop) • macos • darwin-arm64 • macOS 12.3 21E230 darwin-arm • Chrome (web) • chrome • web-javascript • Google Chrome 100.0.4896.127 [✓] HTTP Host Availability • All required HTTP hosts are available ! Doctor found issues in 1 category. ```
logs ``` Launching lib/main.dart on Redmi K20 Pro in debug mode... Warning: Mapping new ns http://schemas.android.com/repository/android/common/02 to old ns http://schemas.android.com/repository/android/common/01 Warning: Mapping new ns http://schemas.android.com/repository/android/generic/02 to old ns http://schemas.android.com/repository/android/generic/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/addon2/02 to old ns http://schemas.android.com/sdk/android/repo/addon2/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/repository2/02 to old ns http://schemas.android.com/sdk/android/repo/repository2/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/sys-img2/02 to old ns http://schemas.android.com/sdk/android/repo/sys-img2/01 Warning: Mapping new ns http://schemas.android.com/repository/android/common/02 to old ns http://schemas.android.com/repository/android/common/01 Warning: Mapping new ns http://schemas.android.com/repository/android/generic/02 to old ns http://schemas.android.com/repository/android/generic/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/addon2/02 to old ns http://schemas.android.com/sdk/android/repo/addon2/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/repository2/02 to old ns http://schemas.android.com/sdk/android/repo/repository2/01 Warning: Mapping new ns http://schemas.android.com/sdk/android/repo/sys-img2/02 to old ns http://schemas.android.com/sdk/android/repo/sys-img2/01 ✓ Built build/app/outputs/flutter-apk/app-debug.apk. Connecting to VM Service at ws://127.0.0.1:60913/BwgKTI2fIr8=/ws I/CameraManagerGlobal( 4528): Connecting to camera service W/CameraManagerGlobal( 4528): [soar.cts] ignore the status update of camera: 100 W/CameraManagerGlobal( 4528): [soar.cts] ignore the status update of camera: 101 W/CameraManagerGlobal( 4528): [soar.cts] ignore the status update of camera: 120 W/CameraManagerGlobal( 4528): [soar.cts] ignore the status update of camera: 20 W/CameraManagerGlobal( 4528): [soar.cts] ignore the status update of camera: 21 W/CameraManagerGlobal( 4528): [soar.cts] ignore the status update of camera: 60 W/CameraManagerGlobal( 4528): [soar.cts] ignore the status update of camera: 61 W/CameraManagerGlobal( 4528): [soar.cts] ignore the status update of camera: 62 W/CameraManagerGlobal( 4528): [soar.cts] ignore the status update of camera: 63 W/CameraManagerGlobal( 4528): ignore the torch status update of camera: 100 W/CameraManagerGlobal( 4528): ignore the torch status update of camera: 120 W/CameraManagerGlobal( 4528): ignore the torch status update of camera: 20 W/CameraManagerGlobal( 4528): ignore the torch status update of camera: 21 W/CameraManagerGlobal( 4528): ignore the torch status update of camera: 60 W/CameraManagerGlobal( 4528): ignore the torch status update of camera: 61 W/CameraManagerGlobal( 4528): ignore the torch status update of camera: 62 W/CameraManagerGlobal( 4528): ignore the torch status update of camera: 63 W/Camera ( 4528): The selected imageFormatGroup is not supported by Android. Defaulting to yuv420 E/libc ( 4528): Access denied finding property "persist.vendor.camera.privapp.list" W/.example.newapp( 4528): type=1400 audit(0.0:1469750): avc: denied { read } for name="u:object_r:vendor_persist_camera_prop:s0" dev="tmpfs" ino=28877 scontext=u:r:untrusted_app:s0:c61,c260,c512,c768 tcontext=u:object_r:vendor_persist_camera_prop:s0 tclass=file permissive=0 app=com.example.newapp I/Camera ( 4528): startPreview I/.example.newap( 4528): ProcessProfilingInfo new_methods=1281 is saved saved_to_disk=1 resolve_classes_delay=8000 W/BpBinder( 4528): PerfMonitor binderTransact : time=649ms interface=android.hardware.camera2.ICameraDeviceUser code=6 I/Camera ( 4528): CameraCaptureSession onConfigured I/Camera ( 4528): Updating builder settings D/Camera ( 4528): Updating builder with feature: ExposureLockFeature D/Camera ( 4528): Updating builder with feature: ExposurePointFeature D/Camera ( 4528): Updating builder with feature: ZoomLevelFeature D/Camera ( 4528): Updating builder with feature: AutoFocusFeature D/Camera ( 4528): Updating builder with feature: NoiseReductionFeature I/Camera ( 4528): updateNoiseReduction | currentSetting: fast D/Camera ( 4528): Updating builder with feature: FocusPointFeature D/Camera ( 4528): Updating builder with feature: ResolutionFeature D/Camera ( 4528): Updating builder with feature: SensorOrientationFeature D/Camera ( 4528): Updating builder with feature: FlashFeature D/Camera ( 4528): Updating builder with feature: ExposureOffsetFeature D/Camera ( 4528): Updating builder with feature: FpsRangeFeature I/Camera ( 4528): refreshPreviewCaptureSession W/MirrorManager( 4528): this model don't Support E/flutter ( 4528): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: 'package:flutter/src/rendering/proxy_box.dart': Failed assertion: line 3158 pos 12: '!debugNeedsPaint': is not true. E/flutter ( 4528): #0 _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:51:61) E/flutter ( 4528): #1 _AssertionError._throwNew (dart:core-patch/errors_patch.dart:40:5) E/flutter ( 4528): #2 RenderRepaintBoundary.toImage E/flutter ( 4528): #3 TakePictureScreenState.build. E/flutter ( 4528): E/flutter ( 4528): ```

Duplicate of https://github.com/flutter/flutter/issues/83856 (closed)

cc: @blasten

zenkog commented 2 years ago

I also have the same problem. But in my case, I must take a screenshot of the PlatformView widget.

So, is there a solution for this yet?

araisann commented 2 years ago

Have the same problem.

chinmaygarde commented 2 years ago

I don't think Flutter can take a screenshot of platform provided textures today. Especially if those textures are from protected sources. I believe the linked issue was for tests. cc @blasten to confirm.

AlexV525 commented 1 year ago

This is the same issue as https://github.com/flutter/flutter/issues/25306, but it's not solved. Instead, https://github.com/flutter/flutter/issues/83856 turn this into a test issue, then https://github.com/flutter/flutter/issues/97853 turn this into a test coverage issue. I think @blasten has already done some good work but the scope is not correct at present. We should definitely support this in the runtime, not only for tests.

zmtzawqlp commented 1 year ago

Steps to Reproduce

  1. Execute flutter run on the code sample
  2. Press icon button to take screenshot from camera preview.

Expected results: Screenshot should contain camera preview picture.

Actual results: Screenshot doesn't include camera view.

I tested it on Samsung S9, Samsung S20 using Flutter 2.10.5 and 2.13.0-0.2.pre. Same issue happen on iOS, tested on iPhone 12 Pro Max, iOS 15.2.1, Flutter 2.10.5.

I'm aware of existing issues like #25306 which all point to resolved #83856 (comment). However I can't confirm that this feature works.

Code sample pubspec.yaml flutter doctor -v Screenshots Camera surface view:

Screenshot result:

try https://pub.dev/packages/ff_native_screenshot to get screenshot by native

mohamadkenway commented 1 year ago

I have same problem when taking screenshot from Qr Code Scanner

Perondas commented 1 year ago

Still having the same issue

zenkog commented 1 year ago

Do we have solution to this issue?

sidetraxaudio commented 1 year ago

Is there even any beta/master channel work on this? It seems a rudimentary feature. Please convert the test functionality into runtime.

TheGlorySaint commented 8 months ago

Hey Flutter-Team,

is there any update on this issue?

njovy commented 2 months ago

It still hasn't been fixed yet after 2 years.

Trung15010802 commented 1 week ago

Does anyone have solution?