Flutter How to create pixel-art? - flutter

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()

Related

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.

how to calculate PPU for image sprites

I have an image for creating rounded button in unity, but, If I don't account for the correct PPU value, the button gets distorted (I'm even using sliced images):
Through trial and error, the PPU value of 300 seems to look like the original image:
but, I don't know a way to confirm if that is correct. How would we actually calculate the PPU value to use for images?
I think you just need rounded rectangle image and need to slice it as follow
I have sliced the image as shown above and added an Image component in scene like following
Notice the Pixel per unit multiplier it is to control the border radius, and now I can stretch the image and it won't get distorted. Check the screenshots for that
And for the text I have used TextMeshProUGUI as the child of the Image.

How to merge an image to fill in other image with shape?

I want to merge an image to another image in one shape. Example:
1- People image
2- Shape Image:
So how to do draw that. I already implement for merging but it's not fill to that shape.
It's possible to do this using the masking functions in the Quartz 2D framework. It's a little bit more involved than using the higher level image functions of UI Kit, but Quartz 2D gives you a lot more power to do cool graphics techniques.
The relevant Apple Developer guide to this can be found here: https://developer.apple.com/library/mac/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_images/dq_images.html
For this example, you'd want to create a mask shape for the inside part of the shape image. There are two ways you can do this. One way is to use image editing software to create a second mask image, with the same size as your shape image, with pure black in the area where you want the people image to appear, and white where you don't want to appear. In this example, that would be the area inside the blue shape. It is important to not crop this image, or else they won't match up exactly.
The other way to create the masking image would be to do that dynamically based on the shape image, and honestly, this is the way I would do it. This would mean that you're including fewer images in your app, and if you made any changes to the shape image, you wouldn't have to recreate the mask image as well. You could do this by making a small change to the way your shape image is formatted. You would need to use a format that allows transparency - png is preferred - so that there is alpha transparency in the part of the image outside of the shape, which is white in your JPEG image. Make sure the section in the center of the image is white (really, any color that is NOT USED in the wanted part of the shape image would work, but I'll say white for this example) and that you don't have parts of it that aren't pure white after image compression.
You will then use Quartz to select the area that's white, and create a mask from that. This technique is a bit more involved, but what you need can be found in the document I linked to above. Because of this, you might start with a static masking image, and then convert to the more involved technique after you've got the code to make the first technique work.
When you have your masking image, you would create the mask itself with the function CGImageMaskCreate(::::::::). You can then apply the mask to the people image using the function CGImageCreateWithMask(::), which will give you an image with the person's portrait, with the correct shape cropped from the center.
Finally, you would display this in your app by placing the masked people image on top of the shape image, and voila, you'll have what you're looking for.
Also, keep in mind, when using the Quartz 2D framework, you'll have to make sure you release images when they are no longer needed, or else you could have memory leaks.

upload and preview as repeated background image

I am searching for a tool on web but didnt got any success their at all. I want to have something like upload and preview the image as background as repeated one or any other alternative...
I have this sample:
How can I preview and cut the image as a perfect background image as repeated one. Is it possible in Photoshop or in some similiar products?
The trick i have recovered is crop a part of the image in a big canvas of Photoshop and then making the deuplicate copies of the layers...
If the image seems like irregular it means the cropped image is wrong so you have to change with a other or larger part of the image...
If it is regular and doesn't cut at the side onto the edge, it means image is perfect making background repeated...
Thanks...

Cutting image within an image with Corona SDK

I was wondering if it's possible to cut an image within an image with the Corona SDK using your finger? If it is indeed possible, I'd like to know how. It's for an iphone game. Thanks.
There are two ways you can do this.
One is to replace the image with two halves of the original.
You will need to edit the image, which is cumbersome.
Second is to use bitmap masks as alpha masks.
The masks are grayscale images: white = show, black = hide.
This method won't require you to change the image.
Create two halves of the original image.
Hide half of each image shown here as A & B
using 2 separate alpha masks.
Sample Code from Corona here
This would assume your image would always be sliced in the same way.