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

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.

Related

Flutter How to create pixel-art?

Imagine this, where I can render a picture as a canvas & then interact with whatever pixel I want.
Firstly I'd need to parse the pixels somehow, I've tried parsing this picture by it's on-picture pixel amount by using filesize/12x12 & trying to only render sub-lists of the bytes with Image.memory, but it didn't really work out well since it still doesn't make the pixels interactable & it renders it top-down. Maybe I would need to use an image that's true to size pixel amount instead but I'd then need to upscale it somehow.
Do I map each pixel to an container widget? Do I use the flutter canvas library? Im pretty confused.
How would I go about doing this?
Our app,you want code like that??
: https://apps.apple.com/vn/app/pixel-art-editor-for-mcpe/id1603567060
EDIT:
First: you need this: https://pub.dev/packages/image
img.Image image = img.decodeImage(file.readAsBytesSync());
image.setPixelSafe(x,y,color);// this line draw a color to pixel at x,y
next step paint image to canvas or Image.memory()

How to Capture a screenshot and share using 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.

Drawing Base64Encoded image as canvas background in flutter

I am facing two problems with my code:
1) I am unable to add a Base64Encoded string image as a background in Canvas.
2) Anything I am drawing in my canvas using freehand, is not clipping at the bottom edge of the canvas
Would like to have some help in achieveing the bove two tasks please. I have attached my existing code as well.
=============================================================
UPDATE:
So, I finally manage to add the image on canvas, thanks for the help. The issue I have landed onto is to constraint the image inside the container, regardless of the image size. I have attached screenshots of two different image sizes to better illustrate my problem :

Is there any way to find exact boundaries of an image in cocos2d instead of making a rect of that image?

I want to cut a part of large image with the shape of a small image. Is there any way to find the exact shape of small image instead of making rect of that image using boundingBox? Please reply..
You could do it by masking the bigger sprite with the smaller one.
Here you have a great tutorial on that:
http://www.raywenderlich.com/4421/how-to-mask-a-sprite-with-cocos2d-1-0
And here is the tool that they use to play with the blending functions
http://www.andersriggelsen.dk/glblendfunc.php

iPhone, how do you draw from a texturepage using coregraphics?

I'm trying to work out how to draw from a TexturePage using CoreGraphics.
Given a texture page (CGImageRef) which contains multiple 64x64 packed textures, how do I render sub areas from that page onto the device context.
CGContextDrawImage seems to only take a destination rect. I noticed CGImageCreateWithImageInRect, however this creates a new image. I don't want a new image I simply want to draw from the original image.
I'm sure this is possible, however I'm new to iPhone development.
Any help much appreciated.
Thanks
What's wrong with CGImageCreateWithImageInRect?
CGImageRef subImage = CGImageCreateWithImageInRect(image, srcRect);
if (subImage) {
CGContextDrawImage(context, destRect, subImage);
CFRelease(subImage);
}
Edit: Wait a minute. Use CGImageCreateWithImageInRect. That is what it's for.
Here are the ideas I wrote up initially; I will leave them in case they're useful.
See if you can create a sub-image of some kind from another image, such that it borrows the original image's buffer (much like some substring implementations). Then you could draw using the sub-image.
It might be that Core Graphics is intended more for compositing than for image manipulation, so you may have to use separate image files in your application bundle. If the SDK docs don't particularly recommend what you're doing, then I suggest you go that route since it seems the most simple and natural way to do it.
You could use OpenGLES instead, in which case you can specify the texture coordinates of polygon vertices to select just that section of your big texture.