Crop and save an image in Flutter without ui

0

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

crop flutter image
2021-10-26 13:23:42
1

0

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));
  }
}
2021-10-27 06:10:19

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................