Quasi
Quasi

Reputation: 679

How to get a pixel color from Image, in Flutter?

How can I get the pixel color at a specific (x,y) coordinate from an Image that I have in my assets folder. I already tried this package : https://pub.dev/packages/image because it has a getPixel(x,y) method, but if I try to load a File: Image image = decodeImage(File('../assets/image.jpg').readAsBytesSync()); ,I get this error: (OS Error: No such file or directory, errno = 2). And it doesnt matter if its a relative or absolute path, it wont't work.

So I noticed that you have to use final ByteData data = await rootBundle.load('assets/image.jpg'); to load an Image. And my newest approach was to use the Image package and create an new image: img.Image myImage = img.Image.fromBytes(width, height, bytes); But for that I need a list of bytes List<int> bytes. And I'm not sure how to get that list? Do I get it from ByteData when loading the image from the assets folder?

So 2 questions: How can I get the pixel color at a specific (x,y) from an image. And if thats not possible, how can I get a list of bytes from an image?

Upvotes: 0

Views: 1978

Answers (1)

Christopher Moore
Christopher Moore

Reputation: 17143

You can get bytes from the ByteData that you already figured out how to get. Do data.buffer to get the ByteBuffer and use its asUint8List method to get a List<int> that you can pass to create an img.Image with its fromBytes constructor.

You can use the getPixel method to get the bytes from a particular pixel.

Upvotes: 1

Related Questions