I need to compare two images thus I need to decode these two images (assets) into bitmaps. I tried this:
import 'package:flutter/services.dart';
import 'package:image/image.dart';
var imageData = await rootBundle.load("assets/images/test.png");
Image firstImage = Image.fromBytes(100, 100, imageData.buffer.asUint8List());
int pixel = firstImage.getPixel(5, 0);
print('$pixel');
but it doesn't work, because after I set the second argument of getPixel(x, y)
to any value except 0 it throws an exception:
E/flutter (11752): [ERROR:flutter/shell/common/shell.cc(199)] Dart Error: Unhandled exception:
E/flutter (11752): RangeError (index): Index out of range: index should be less than 86: 101
E/flutter (11752): #0 _Uint32ArrayView.[] (dart:typed_data-patch/typed_data_patch.dart:3999:7)
E/flutter (11752): #1 Image.getPixel (package:image/src/image.dart:409:37)
Also I tried this way:
Image firstImage = decodeImage(new File('assets/images/test.png').readAsBytesSync());
int pixel = firstImage.getPixel(1, 1);
print('$pixel');
but have next exception:
E/flutter (11752): FileSystemException: Cannot open file, path = 'assets/images/test.png' (OS Error: No such file or directory, errno = 2)
So I am looking for a soultion that will help me to turn my assets into bitmaps so I can get color of every pixel of each image.
Try this:
import 'package:image/image.dart' as img;
...
ByteData imageBytes = await rootBundle.load('assets/images/test.png');
List<int> values = imageBytes.buffer.asUint8List();
img.Image photo;
photo = img.decodeImage(values);
int pixel = photo.getPixel(5, 0);