Flutter图片裁剪插件fepe_image_cropper的使用

Flutter 图片裁剪插件 fepe_image_cropper 的使用

fepe_image_cropper 是一个用于 Android 和 iOS 的 Flutter 插件,支持对图片进行裁剪。该插件基于两个不同的本地库(uCropTOCropViewController),因此在不同平台上具有不同的用户界面。

简介

fepe_image_cropper 并不直接在 Dart 代码中处理图像,而是通过平台通道将 Dart API 暴露给 Flutter 应用程序,使其能够与两个强大的本地库(uCropTOCropViewController)通信,以裁剪和旋转图像。因此,所有的功劳都归功于这些库。

uCrop - Yalantis

该项目旨在提供终极且灵活的图像裁剪体验。它由 Yalantis 开发。

TOCropViewController - TimOliver

TOCropViewController 是一个开源的 UIViewController 子类,用于从 UIImage 对象中裁剪部分,并执行基本旋转。非常适合编辑个人资料图片或在线分享照片的一部分。其设计考虑了 iOS 照片应用程序编辑器,因此其行为应为 iOS 用户所熟悉。

如何安装

Android

在你的 AndroidManifest.xml 文件中添加 UCropActivity

<activity
    android:name="com.yalantis.ucrop.UCropActivity"
    android:screenOrientation="portrait"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>

注意:

从 v1.2.0 开始,你需要将你的 Android 项目迁移到 v2 嵌入(详情见这里)。

iOS

无需配置。

使用方法

必须参数

  • sourcePath: 图像文件的绝对路径。

可选参数

  • maxWidth: 裁剪后图像的最大宽度。
  • maxHeight: 裁剪后图像的最大高度。
  • aspectRatio: 控制裁剪边界的比例。如果设置了此值,则裁剪器锁定,用户无法更改裁剪边界的比例。
  • aspectRatioPresets: 控制裁剪菜单视图中的比例列表。在 Android 上,你可以通过设置 AndroidUiSettings.initAspectRatio 来设置开始裁剪时的初始比例。
  • cropStyle: 控制裁剪边框的样式,可以是矩形或圆形样式(默认为 CropStyle.rectangle)。
  • compressFormat: 结果图像的格式,png 或 jpg(默认为 ImageCompressFormat.jpg)。
  • compressQuality: 控制图像压缩质量的值 [0 - 100]。
  • androidUiSettings: 控制 Android 上的 UI 定制。详见 Android 定制
  • iosUiSettings: 控制 iOS 上的 UI 定制。详见 iOS 定制

注意:

结果文件保存在 iOS 的 NSTemporaryDirectory 和 Android 的应用缓存目录中,因此以后可能会丢失。如果你需要永久存储,请负责将其保存到某个永久位置。

自定义

Android

fepe_image_cropper 提供了一个名为 AndroidUiSettings 的帮助类,用于自定义 uCrop 库中的 UI。

属性 描述 类型
toolbarTitle 工具栏标题的期望文本 String
toolbarColor 工具栏的期望颜色 Color
statusBarColor 状态栏的期望颜色 Color
toolbarWidgetColor 工具栏文本和按钮的期望颜色(默认为较深的橙色) Color
backgroundColor 应应用于根视图的期望背景颜色 Color
activeControlsWidgetColor 激活和选定小部件及进度轮中间线的期望解析颜色(默认为白色) Color
dimmedLayerColor 裁剪边界周围阴影区域的期望颜色 Color
cropFrameColor 裁剪边框的期望颜色 Color
cropGridColor 裁剪网格/指南线的期望颜色 Color
cropFrameStrokeWidth 裁剪边框线条的期望像素宽度 int
cropGridRowCount 裁剪网格行数 int
cropGridColumnCount 裁剪网格列数 int
cropGridStrokeWidth 裁剪网格线条的期望像素宽度 int
showCropGrid 如果希望在图像上看到裁剪网格/指南线,则设置为 true bool
lockAspectRatio 如果希望将裁剪边界的纵横比锁定为固定值(默认锁定),则设置为 true bool
hideBottomControls 如果希望隐藏底部控件(默认显示),则设置为 true bool
initAspectRatio 当启动裁剪器时应用的期望纵横比(来自给定的纵横比预设列表) CropAspectRatioPreset

iOS

fepe_image_cropper 提供了一个名为 IOSUiSettings 的帮助类,用于自定义 TOCropViewController 库中的 UI。

属性 描述 类型
minimumAspectRatio 最小裁剪纵横比。如果设置,用户将被阻止将裁剪矩形设置为低于该参数定义的纵横比 double
rectX 初始裁剪矩形的 x 坐标 double
rectY 初始裁剪矩形的 y 坐标 double
rectWidth 初始裁剪矩形的宽度 double
rectHeight 初始裁剪矩形的高度 double
showActivitySheetOnDone 如果为 true,则当用户点击 ‘Done’ 时,将出现一个 UIActivityController 之前视图控制器结束 bool
showCancelConfirmationDialog 当用户点击 ‘Cancel’ 并有未完成的更改时,显示确认对话框(默认为 false) bool
rotateClockwiseButtonHidden 当禁用时,一个额外的旋转按钮会显示在工具栏上,用于以 90 度段顺时针旋转画布(默认为 false) bool
hidesNavigationBar 如果此控制器嵌入到 UINavigationController 中,默认情况下导航栏被隐藏。设置为 false 以显示导航栏。这必须在控制器呈现之前设置 bool
rotateButtonsHidden 当启用时,隐藏旋转按钮以及当 showClockwiseRotationButton 设置为 YES 时可见的替代旋转按钮(默认为 false) bool
resetButtonHidden 当启用时,隐藏工具栏上的 ‘Reset’ 按钮(默认为 false) bool
aspectRatioPickerButtonHidden 当启用时,隐藏工具栏上的 ‘Aspect Ratio Picker’ 按钮(默认为 false) bool
resetAspectRatioEnabled 如果为 true,点击重置按钮也会将纵横比重置回图像的默认比例。否则,重置只会缩放到当前纵横比。如果此设置为 false,并且 aspectRatioLockEnabled 设置为 true,则纵横比按钮将自动从工具栏中隐藏(默认为 true) bool
aspectRatioLockDimensionSwapEnabled 如果为 true,设置自定义纵横比,并且 aspectRatioLockEnabled 设置为 true,裁剪框将根据肖像或风景尺寸图像交换其尺寸。此值还控制图像旋转时尺寸是否可以交换(默认为 false) bool
aspectRatioLockEnabled 如果为 true,虽然仍可以调整大小,但裁剪框将锁定为其当前纵横比。如果此设置为 true,并且 resetAspectRatioEnabled 设置为 false,则纵横比按钮将自动从工具栏中隐藏(默认为 false) bool
title 视图控制器顶部显示的标题文本 String
doneButtonTitle ‘Done’ 按钮的标题。设置此值将覆盖默认值,即 “Done” 的本地化字符串 String
cancelButtonTitle ‘Cancel’ 按钮的标题。设置此值将覆盖默认值,即 “Cancel” 的本地化字符串 String

示例

import 'package:fepe_image_cropper/image_cropper.dart';

File croppedFile = await ImageCropper.cropImage(
      sourcePath: imageFile.path,
      aspectRatioPresets: [
        CropAspectRatioPreset.square,
        CropAspectRatioPreset.ratio3x2,
        CropAspectRatioPreset.original,
        CropAspectRatioPreset.ratio4x3,
        CropAspectRatioPreset.ratio16x9
      ],
      androidUiSettings: AndroidUiSettings(
          toolbarTitle: 'Cropper',
          toolbarColor: Colors.deepOrange,
          toolbarWidgetColor: Colors.white,
          initAspectRatio: CropAspectRatioPreset.original,
          lockAspectRatio: false),
      iosUiSettings: IOSUiSettings(
        minimumAspectRatio: 1.0,
      )
    );

更多关于Flutter图片裁剪插件fepe_image_cropper的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter图片裁剪插件fepe_image_cropper的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


flutter_image_cropper 是一个非常流行的 Flutter 插件,用于在应用程序中裁剪图像。它提供了丰富的功能,如自定义裁剪比例、旋转图像、缩放图像等。下面是如何在 Flutter 项目中使用 flutter_image_cropper 插件的详细步骤。

1. 添加依赖

首先,你需要在 pubspec.yaml 文件中添加 flutter_image_cropper 插件的依赖。

dependencies:
  flutter:
    sdk: flutter
  image_cropper: ^2.0.0+1

然后运行 flutter pub get 来获取依赖。

2. 配置平台特定的设置

Android

android/app/src/main/AndroidManifest.xml 文件中添加以下权限:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

iOS

ios/Runner/Info.plist 文件中添加以下权限:

<key>NSPhotoLibraryUsageDescription</key>
<string>We need access to your photo library to select images.</string>
<key>NSCameraUsageDescription</key>
<string>We need access to your camera to take photos.</string>

3. 使用 ImageCropper

下面是一个简单的示例,展示如何使用 ImageCropper 插件来裁剪图像。

import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:image_cropper/image_cropper.dart';
import 'dart:io';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Image Cropper Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: ImageCropperExample(),
    );
  }
}

class ImageCropperExample extends StatefulWidget {
  [@override](/user/override)
  _ImageCropperExampleState createState() => _ImageCropperExampleState();
}

class _ImageCropperExampleState extends State<ImageCropperExample> {
  File? _image;

  Future<void> _pickImage() async {
    final picker = ImagePicker();
    final pickedFile = await picker.pickImage(source: ImageSource.gallery);

    if (pickedFile != null) {
      _cropImage(File(pickedFile.path));
    }
  }

  Future<void> _cropImage(File imageFile) async {
    final croppedFile = await ImageCropper().cropImage(
      sourcePath: imageFile.path,
      aspectRatioPresets: [
        CropAspectRatioPreset.square,
        CropAspectRatioPreset.ratio3x2,
        CropAspectRatioPreset.original,
        CropAspectRatioPreset.ratio4x3,
        CropAspectRatioPreset.ratio16x9,
      ],
      androidUiSettings: AndroidUiSettings(
        toolbarTitle: 'Crop Image',
        toolbarColor: Colors.deepOrange,
        toolbarWidgetColor: Colors.white,
        initAspectRatio: CropAspectRatioPreset.original,
        lockAspectRatio: false,
      ),
      iosUiSettings: IOSUiSettings(
        minimumAspectRatio: 1.0,
      ),
    );

    if (croppedFile != null) {
      setState(() {
        _image = File(croppedFile.path);
      });
    }
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Image Cropper Example'),
      ),
      body: Center(
        child: _image == null
            ? Text('No image selected.')
            : Image.file(_image!),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _pickImage,
        tooltip: 'Pick Image',
        child: Icon(Icons.add_a_photo),
      ),
    );
  }
}
回到顶部