compareImages function

Future<double> compareImages({
  1. required dynamic src1,
  2. required dynamic src2,
  3. Algorithm? algorithm,
})

Compare images from src1 and src2 with a specified algorithm. If algorithm is not specified, the default (PixelMatching()) is supplied.

Returns a Future of double corresponding to the difference between src1 and src2

src1 and src2 may be any combination of the supported types:

  • Uri - parsed URI, such as a URL (dart:core Uri class)
  • File - reference to a file on the file system (dart:io File class)
  • List- list of integers (bytes representing the image)
  • Image - image buffer (Image class)

Implementation

Future<double> compareImages({
  required var src1,
  required var src2,
  Algorithm? algorithm,
}) async {
  algorithm ??= PixelMatching(); // default algorithm
  src1 = await _getImageFromDynamic(src1);
  src2 = await _getImageFromDynamic(src2);

  var result = algorithm.compare(src1, src2);

  // Ignore floating point error
  if (result < 0) {
    result = 0;
  } else if (result > 1) {
    result = 1;
  }

  return result;
}