applyTransforms method

Future<File> applyTransforms(
  1. XFile file
)

Implementation

Future<File> applyTransforms(XFile file) async
{
  busy = true;

  image_pack.Image? image;

  //  image is mirrored. this is a stop gap measure. should be a transform
  if ((direction ?? "") == S.fromEnum(CameraLensDirection.front))
  {
    image = image_pack.decodeJpg(await file.readAsBytes());
    image = image_pack.flipHorizontal(image!);

    // encode back to jpg
    var bytes = image_pack.encodeJpg(image);

    var name = basename(S.isNullOrEmpty(file.name) ? "${S.newId()}.jpg" : file.name);
    var uri  = UriData.fromBytes(bytes, mimeType: await S.mimetype(name));
    var url  = uri.toString();
    var type = await S.mimetype(name);
    var size = bytes.length;

    // save the image
    busy = false;
    return File(uri, url, name, type, size);
  }

  // no transformation applied
  else
  {
    // set file - 'file:' required so image widget will decode as a file path.
    var url  = file.path.startsWith("blob:") ? file.path : "file:${file.path}";
    var name = basename(S.isNullOrEmpty(file.name) ? "${S.newId()}.jpg" : file.name);
    var type = await S.mimetype(name);
    var size = await file.length();

    // save the image
    busy = false;
    return File(file, url, name, type, size);
  }
}