Flutter图片裁剪压缩插件flutter_image_crop_compress的使用
Flutter图片裁剪压缩插件flutter_image_crop_compress的使用
flutter_image_crop_compress 插件介绍
flutter_image_crop_compress 是一个用于 Flutter 的的插件,它可以对图片进行裁剪和压缩,并提供了一个可定制的裁剪控件 Crop
。这个插件非常灵活,可以放置在任何地方,甚至可以在对话框或底部弹窗中使用。裁剪方法也可以自定义,例如固定图像并移动裁剪区域、固定裁剪区域并缩放/平移图像,或者两者都配置。
使用示例代码
import 'package:flutter/material.dart';
import 'package:flutter_image_crop_compress/flutter_image_crop_compress.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
[@override](/user/override)
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
File? file;
bool isProcessing = false;
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
if (isProcessing)
const Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
CircularProgressIndicator(),
Text(
"Processing image",
textAlign: TextAlign.center,
),
SizedBox(height: 22)
],
),
Text(
file != null ? "Image Picked" : 'Pick image',
),
const SizedBox(height: 22),
file != null
? Image.file(
file!,
fit: BoxFit.fill,
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height / 2.5,
)
: const SizedBox(),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
imagePicker(context);
},
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
Future<void> imagePicker(BuildContext context, {bool isGallery = true}) async {
isProcessing = true;
updateState();
File? fileImage = await pickImage(context, isGallery: isGallery);
if (fileImage != null && context.mounted) {
await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ImageCompressCropPage(
appBar: AppBar(
key: const Key("appBarCrop"),
title: const Text("Edit Image"),
),
imageFile: fileImage,
addAnimation: () => loadingAnimation(context),
onTap: (File image) {
file = image;
updateState();
},
),
));
}
isProcessing = false;
updateState();
}
updateState() {
if (mounted) {
setState(() {});
}
}
static Future<File?> pickImage(BuildContext context, {bool isGallery = true}) async {
try {
File? fileImage;
final picker = ImagePicker();
XFile? pickedFile;
if (isGallery {
pickedFile = await picker.pickImage(
source: ImageSource.gallery, imageQuality: 80);
} else {
pickedFile = await picker.pickImage(
source: ImageSource.camera, imageQuality: 80);
}
if ( (pickedFile != null && pickedFile.path.isNotEmpty) {
fileImage = File(pickedFile.path);
} else {
fileImage = null;
}
return fileImage;
} catch (e) {
return null;
}
}
Future<void> loadingAnimation(BuildContext context) async => showDialog(
context: context,
builder: ( context) => const CircularProgress(color: Colors.red));
}
}
更多关于Flutter图片裁剪压缩插件flutter_image_crop_compress的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复
更多关于Flutter图片裁剪压缩插件flutter_image_crop_compress的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是一个关于如何使用flutter_image_crop_compress
插件的详细代码示例。这个插件允许你在Flutter应用中实现图片的裁剪和压缩功能。
首先,确保你已经在pubspec.yaml
文件中添加了依赖项:
dependencies:
flutter:
sdk: flutter
flutter_image_crop_compress: ^3.0.1 # 请根据需要替换为最新版本
然后,运行flutter pub get
来获取依赖项。
接下来,在你的Dart文件中使用flutter_image_crop_compress
插件。以下是一个完整的示例,展示如何从图像选择器获取图像,裁剪并压缩它,然后显示裁剪后的图像。
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:flutter_image_crop_compress/flutter_image_crop_compress.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: ImageCropperScreen(),
);
}
}
class ImageCropperScreen extends StatefulWidget {
@override
_ImageCropperScreenState createState() => _ImageCropperScreenState();
}
class _ImageCropperScreenState extends State<ImageCropperScreen> {
File? _imageFile;
late ImagePicker _picker = ImagePicker();
Future<void> _pickImage() async {
final XFile? pickedFile = await _picker.pickImage(source: ImageSource.gallery);
if (pickedFile != null) {
final File tempFile = File(pickedFile.path);
File? croppedFile = await FlutterImageCompress.compressAndGetFile(
tempFile.absolute.path,
quality: 80,
);
if (croppedFile != null) {
CropResult? result = await ImageCropper().cropImage(
sourcePath: croppedFile.absolute.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,
),
);
if (result != null) {
setState(() {
_imageFile = File(result.path);
});
}
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Image Crop and Compress'),
),
body: Center(
child: _imageFile == null
? ElevatedButton(
onPressed: _pickImage,
child: Text('Pick and Crop Image'),
)
: Image.file(_imageFile!),
),
);
}
}
解释
- 依赖项:在
pubspec.yaml
中添加flutter_image_crop_compress
和image_picker
依赖项。 - 选择图片:使用
ImagePicker
从设备的图库中选择图片。 - 压缩图片:使用
FlutterImageCompress.compressAndGetFile
方法对选中的图片进行压缩。 - 裁剪图片:使用
ImageCropper().cropImage
方法对压缩后的图片进行裁剪。 - 显示图片:如果裁剪成功,使用
Image.file
显示裁剪后的图片。
注意事项
- 在实际项目中,请确保处理可能出现的错误和异常情况,例如用户取消选择图片或压缩/裁剪失败。
- 根据需要调整压缩质量和裁剪的宽高比预设。
这个示例展示了基本的图片裁剪和压缩功能,你可以根据具体需求进行扩展和定制。