Black screen occurs while taking screenshot in flutter - flutter

I'm using a native surface view in a flutter When I take a screenshot using a repaint boundary widget. black screen issue occurs.
My code :
RenderRepaintBoundary boundary =_globalKey.currentContext.findRenderObject();
ui.Image image = await boundary.toImage(pixelRatio: 3.0);
ByteData byteData =
await image.toByteData(format: ui.ImageByteFormat.png);
Please guide me for solution

Related

Flutter: Image Package Crop to Circle

I am using the Image package to handle and process images in a flutter app. In this case, I am downloading the users profile pic and attempting to resize it and crop it to a circle. However, it does not appear to be cropping the image at all. Am I missing something here?
import 'package:image/image.dart' as ui;
final getProfile = await http.get(Uri.parse(profilePicPath));
final profileFile = File('${(await getTemporaryDirectory()).path}/image.png');
await profileFile.create(recursive: true);
await profileFile.writeAsBytes(getProfile.bodyBytes);
ui.Image profileImage = ui.decodeImage(profileFile.readAsBytesSync());
ui.copyCropCircle(profileImage, radius: 10);
Edit: I need to keep the image as a file and will not be shown in the UI (eventually used to overlay in another image file).
For anyone who runs into this issue, it was only a slight modification that got it to work. This pulls in the photo, stores it, resizes it, and then crops it to a cricle.
import 'package:image/image.dart' as ui;
final getProfile = await http.get(Uri.parse(profilePicPath));
final profileFile = File('${(await getTemporaryDirectory()).path}/image.png');
await profileFile.create(recursive: true);
await profileFile.writeAsBytes(getProfile.bodyBytes);
ui.Image profileImage = ui.decodeImage(profileFile.readAsBytesSync());
profileImage = ui.copyResize(profileImage, height: 100, width: 100);
profileImage = ui.copyCropCircle(profileImage, radius: 50);

Flutter: How to generate screenshots in arbitrary resolutions?

In Flutter, how do you get a screenshot for an arbitrary screen resolution? I.e. for a screen resolution that's different from the resolution the app is running in?
Is there a way to tell the Flutter engine to paint a single frame into a buffer with a specific resolution?
What I'm trying to accomplish is to generate the screenshots for the various app stores from within the app, but without scaling and cropping screenshots that were taken for different devices/resolutions.
How would you tackle that requirement?
Any advise is welcome,
Thank you!
if you are talking about taking a screenshot of a particular widget and saving it
the you have to use renderrepaint boundry with key .
dependencies - path provider
Image gallery saver ..
takeScreenshot(BuildContext context,GlobalKey _key,String slug) async {
RenderRepaintBoundary boundary = _key.currentContext.findRenderObject();
ui.Image image = await boundary.toImage(pixelRatio: 3.0);
final snackBar = SnackBar(content: Text("Image saved to gallery"));
ScaffoldMessenger.of(context).showSnackBar(snackBar);
final directory = (await getApplicationDocumentsDirectory()).path;
ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);
final result = await ImageGallerySaver.saveImage(
byteData.buffer.asUint8List(),
name: "name.jpg");
}

Flutter Web RenderRepaintBoundary toimage does not render widgets with images

RepaintBoundary(
key: snapshotContainer,
child: Image.network('https://picsum.photos/250?image=9'),
);
and
final RenderRepaintBoundary boundary =
snapshotContainer.currentContext!
.findRenderObject()! as RenderRepaintBoundary;
final ui.Image image =
await boundary.toImage(pixelRatio: 2);
final ByteData? byteData = await image.toByteData(
format: ui.ImageByteFormat.png);
final Uint8List pngBytes =
byteData!.buffer.asUint8List();
produces an empty image. is there a way how to render widgets with images inside via Flutter Web CanvasKit?
You need to run flutter in production run mode to see this working. We have been facing the same issue and it seems if you run it using this command
flutter run -d chrome --web-renderer canvaskit --release --dart-define=BROWSER_IMAGE_DECODING_ENABLED=false
it should render your images.
Word of caution is that the BROWSER_IMAGE_DECODING_ENABLED=false is a workaround for an active issue in flutter
What worked for me is to use svg instead of png images, e.g.
import 'package:flutter_svg/svg.dart';
SvgPicture.asset(AppImages.mapPin, height: 20)
This works for Web without any other command line arguments or any other changes.

how to embed widgets to images and save it to the device

I have an image with a container on it. I want to be able to edit the TextField inside the container and save the image as an image to the device.
My current setup is a stack where I have the image and a container on top of each other. How can I save that as an image?
Note that I don't want to save the whole screen, just the image and whatever on it.
Future<File> createWave(BuildContext context, GlobalKey screen) async {
RenderRepaintBoundary boundary = screen.currentContext.findRenderObject();
ui.Image image = await boundary.toImage();
ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);
Uint8List pngBytes = byteData.buffer.asUint8List();
final directory = (await getApplicationDocumentsDirectory()).path;
File imgFile = File('$directory/screenshot.png');
await imgFile.writeAsBytes(pngBytes);
return imgFile;
}
Thankfully since flutter uses skia to render it's graphics, we are able to take screenshots of widgets and there is already a really good article that does somethings similar. Basically you wrap your widget in a RepaintBoundary and assign it a key then use the key to call the RenderObject.toImage method. Here is another simple example to do this. It's amazing how simple it is :D

How take a picture automatic after 3 seconds in Flutter?

I'm developing a punch clock in a tablet with Flutter Application and I need this functionality.
I know how to make the camera show and the user make a screenshot, but I searched everywhere how to make an automatic self screenshot after 3 seconds and don't find anything.
Someone has an example, tutorial, or experience to make something like this?
You can use Timer class to do some action after certain time duration as shown below
Timer(Duration(milliseconds: 3000), () {
//after 3 seconds this will be called,
//once this is called take picture or whatever function you need to do
takeScreenshot();
});
And if you want,use this code below to take screenshot as mentioned in this answer Take Screenshot Stackoverflow answer
takeScreenShot() async{
RenderRepaintBoundary boundary =
previewContainer.currentContext.findRenderObject();
ui.Image image = await boundary.toImage();
final directory = (await getApplicationDocumentsDirectory()).path;
ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);
Uint8List pngBytes = byteData.buffer.asUint8List();
print(pngBytes);
File imgFile =new File('$directory/screenshot.png');
imgFile.writeAsBytes(pngBytes);
}
Hope this helps!