Crop and save an image in Flutter without ui

Viewed 531

I want to make an app that can crop image in a specific aspect ratio(device ratio). But, i don't want any ui to show crop options.

Ok, here is an example,

If user tap on an image from the image list in the app the selected Image automatically (background process) crop (device ratio) & saved on the device without showing any crop related ui. How can i do this! Any function in dart by whom can crop an image without showing anything.

enter image description here

1 Answers

You can use copyCrop() with image:

Image copyCrop(Image src, int x, int y, int w, int h);

import 'dart:io';
import 'package:image/image.dart';
void main(List<String> argv) {
  String path = argv[0];
  Directory dir = Directory(path);
  List files = dir.listSync();
  List<int> trimRect;
  for (var f in files) {
    if (f is! File) {
      continue;
    }
    List<int> bytes = f.readBytesSync();
    Image image = decodeImage(bytes);
    if (image == null) {
      continue;
    }
    if (trimRect == null) {
      trimRect = findTrim(image, mode: TrimMode.transparent);
    }
    Image trimmed = copyCrop(image, trimRect[0], trimRect[1], 
                             trimRect[2], trimRect[3]);
    String name = f.path.split(RegExp(r'(/|\\)')).last;
    File('$path/trimmed-$name').writeBytesSync(encodeNamedImage(image, f.path));
  }
}
Related