✂️

Dart/Imageで写真を真四角に切り抜く

2022/03/11に公開

Dart/Imageクラスに便利なクラスがあったものの、あまり記事とかなかったのでシェア。

/// Flutter Imageと競合するのでDart/Imageにラベル付けする
import 'package:image/image.dart' as img;

///

static Future<String> _cropSquare(
    String srcFilePath,
    String dstFilePath,
    int cropBound,
  ) async {
    final file = File(srcFilePath);
    final bytes = await file.readAsBytes();
    img.Image src = img.decodeImage(bytes)!;

    img.Image croppedImage = img.copyResizeCropSquare(src, cropBound);

    await File(dstFilePath).writeAsBytes(
      img.encodeJpg(croppedImage),
    );

    return dstFilePath;
}

Discussion