cropPassportMrz function

Future<File?> cropPassportMrz(
  1. File imageFile,
  2. DetectedObject object
)

Implementation

Future<File?> cropPassportMrz(File imageFile, DetectedObject object) async {
  final parse = await img.decodeImageFile(imageFile.absolute.path);
  if (parse == null) return null;

  final passportCrop = img.copyCrop(
    parse,
    x: object.boundingBox.left.toInt(),
    y: object.boundingBox.top.toInt(),
    width: (object.boundingBox.right - object.boundingBox.left).toInt(),
    height: (object.boundingBox.bottom - object.boundingBox.top).toInt(),
  );

  final mrzHeight = (passportCrop.height * 0.25).toInt();
  final mrzY = passportCrop.height - mrzHeight;

  final mrzCrop = img.copyCrop(
    passportCrop,
    x: 0,
    y: mrzY,
    width: passportCrop.width,
    height: mrzHeight,
  );

  final enhanced = img.contrast(mrzCrop, contrast: 1.2);
  final sharpened =
      img.convolution(enhanced, filter: [0, -1, 0, -1, 5, -1, 0, -1, 0]);

  List<int> cropByte = img.encodeJpg(sharpened);
  final String tempPath =
      '${imageFile.parent.path}/mrz_${DateTime.now().millisecondsSinceEpoch}.jpg';
  final File mrzFile = await File(tempPath).writeAsBytes(cropByte);
  return mrzFile;
}