Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we crop an image with point and size in Flutter?

Tags:

flutter

I'm writing a face detect app and I want to crop the face that have detected on screen with the bounding box of that face.

I have searched all the way but only can crop with width height and aspect ratio.

I don't want to crop manual like other image crop plugin because they also crop with width height and aspect ratio as well.

like image 425
hoangquyy Avatar asked Sep 16 '25 23:09

hoangquyy


1 Answers

You can use https://github.com/brendan-duncan/image for that.

For your case, try to use copyCrop.

https://github.com/brendan-duncan/image/blob/master/lib/src/transform/copy_crop.dart

copy_crop.dart

import '../image.dart';

/// Returns a cropped copy of [src].
Image copyCrop(Image src, int x, int y, int w, int h) {
  Image dst = Image(w, h, channels: src.channels, exif: src.exif,
      iccp: src.iccProfile);

  for (int yi = 0, sy = y; yi < h; ++yi, ++sy) {
    for (int xi = 0, sx = x; xi < w; ++xi, ++sx) {
      dst.setPixel(xi, yi, src.getPixel(sx, sy));
    }
  }

  return dst;
}

Usage

var croppedImage = Image copyCrop(Image sampleImageSrc, int pointX, int pointY, int desiredWidth, int desiredHeight);
like image 109
Joshua de Guzman Avatar answered Sep 19 '25 15:09

Joshua de Guzman