How to Capture a screenshot and share using flutter - flutter

Is it possible to take a screenshot of the screen using flame and flutter? So when the user clicks a button, it will share the screenshot with anyone he chooses to share it to.

Since you are specifically using flame, you are in luck! Flame renders your game on the canvas. Your game render method takes a canvas and draws a frame of your game.
Normally the canvas provided is the device screen, but can easily call the render method yourself with a different canvas, say, an image.
final PictureRecorder recorder = PictureRecorder();
final Rect rect = Rect.fromLTWH(0.0, 0.0, game.size.width, game.size.height);
final Canvas c = Canvas(recorder, rect);
game.render(c);
final image = await recorder.endRecording().toImage(game.size.width, game.size.height);
Then you can save that image on, let's say, a file, or in memory.
Now note that this will render only your game, and will render the exact frame you are in when you call this. But I assume that is what you want since you specifically tagged this question with flame. There might be better ways of screenshooting with Flutter in general, but using flame, this is the best way.

Related

How to find the dimensions of screen in Flutter Flame Game without Camera?

I have a Flame Game app that I want to adjust to make it available on all kinds of screens, but my app doesn't use a world or camera. All of the solutions that I've found for finding screen size in Flutter seem to use these items. I don't need a world or camera, but I don't know if there's a way that I can find the screen size without it?
I've tried researching this, but can only find solutions using world or camera.
tYou can get the size of the canvas by just checking the size variable in your FlameGame class. (gameRef.size if you want to use it from a Component that has the HasGameRef mixin)
There is a built-in camera in the FlameGame class if you want to apply a FixedResolutionViewport, which sounds like what you want to do? This is the only line you have to add to do that, preferably in your game class's onLoad method:
camera.viewport = FixedResolutionViewport(Vector2(800, 600));

How do I crop a dart:ui Image in Flutter [duplicate]

I searching an days for this question.
I want to Crop Images like that, in flutter:
GIF Source: https://github.com/ArthurHub/Android-Image-Cropper
The closest lib for this solution is the Image Lib, that lib offers manipulate images and crop, but i want to crop images in UI level like this gif. All libs I found dont offers that.
There is no widget that performs all that for you. However, I believe that it is possible to write that natively in flutter now. I don't have time at this particular moment to do it for you, but I can definitely point you in the right direction.
You're going to need to load the image in such a way that you can either draw it onto a canvas or use a RawImage to draw it rather than using the Image widget directly.
You need to figure out a co-ordinate system relative to the image
You'll need to find a way of drawing the crop indicator - you could do this either by drawing directly on the canvas or possibly using some combination of GestureDetector/Draggable/DropTarget. I'd suggest that sticking to Canvas might be the easiest to start.
Once the user has selected a part of the image, you need to translate the screen co-ordinates to picture co-ordinates.
You then have to create an off-screen canvas to draw the cropped image to. There are various transforms you'll have to do to makes sure the image ends up in the right place.
Once you've made the off-screen crop, you'll have to display the new image.
All of that is quite a lot of work, and probably a lot of finessing to get right.
Here's examples for a couple of the steps you'll need to do, but you'll have to figure out how to put them together.
Loading an image:
var byteData = await rootBundle.load("assets/image.jpg");
Uint8List lst = new Uint8List.view(byteData.buffer);
var codec = await UI.instantiateImageCodec(lst);
var nextFrame = await codec.getNextFrame();
var image = frameInfo.image;
Displaying an image on a canvas:
https://docs.flutter.io/flutter/dart-ui/Canvas/drawImageRect.html
https://docs.flutter.io/flutter/rendering/CustomPainter-class.html
Writing an image to a off-screen canvas:
ui.Image getCroppedImage(Image image, Rect src, Rect dst) {
var pictureRecorder = new ui.PictureRecorder();
Canvas canvas = new Canvas(pictureRecorder);
canvas.drawImageRect(image, src, dst, Paint());
return pictureRecorder.endRecording().toImage(dst.width.floor(), dst.height.floor());
}
You'll probably need to do something like this answer for getting the local coordinates of mouse/touch gestures.
Some advice - I'd start as simple as possible, not thinking about performance to start (i.e. draw everything each paint if needed, etc). Then once you get the basics working you can start thinking of optimization (i.e. using a RawImage, Transform, and Stack for the image and only re-drawing the selector, etc).
If you need any additional help let me know in a comment and I'll do my best to answer. Now that I've been writing about this a bit it does make me slightly curious to try implementing it so I may try at some point, but it probably won't be soon as I'm quite low on time at the moment. Good luck =D
The image_cropper plugin does exactly what you are looking for.

Is there a way to make transparent canvas for custom paint in Flutter?

Does anyone know how to make a transparent canvas in custom paint in Flutter? I have some drawings on one canvas and others on a different canvas. I want to merge these two canvases (2 layers) as one. However, since the canvas is not transparent, this seems to be impossible in Flutter. I wonder if this can be done by using paint BlendMode?

Progressive draw to canvas

I'm looking into progressive draw to canvas in flutter.
I know about CustomPainter, but somehow the canvas gets cleared between the two paint events.
What I'm looking is to add just changes to the canvas, not drawing everything that has to be drawn on canvas. Imagine a sketch app where I'd just add new lines to the canvas instead of redrawing it each time paint is requested.

Get Texture2D from a Canvas in Unity?

I am trying to post an image of my game hero to facebook. So I instantiate the prefab which contains number of UI elements, icons and texts.
I'd like to get a Texture2D or image data from it so I could send that as a payload to my facebook plugin.
If it was visible in camera and it wasn't a UI element I would get its texture from the camera, if I let it appear on the screen the full screen camera won't be looking at it, because it is rendered using canvas. Also canvas is also looking at other objects around it because this prefab doesn't occupy full screen.
I may be wrong but I don't think that you can render the UI on a texture. It doesn't seem ideal, but you could use CaptureScreenshot, then load the image and cut the part you want based on the resolution of Screen.