Flutter图片压缩插件image_compression_flutter的使用
Flutter图片压缩插件image_compression_flutter的使用
Flutter Image Compression 是一个用于移动端、桌面端和Web端的图片压缩和调整大小的库。它支持JPG、PNG、WEBP格式。
该库结合了两个包:
- image_compression:适用于MacOS, Windows, Linux 和 Web
- flutter_image_compress:适用于Android 和 iOS
压缩图片
ImageFile input; // 设置输入图片文件
Configuration config = Configuration(
outputType: ImageOutputType.webpThenJpg,
// 仅在使用ImageOutputType.jpg或ImageOutputType.png时,对于Android和iOS可以为true
useJpgPngNativeCompressor: false,
// 设置质量,范围0-100
quality: 40,
);
final param = ImageFileConfiguration(input: input, config: config);
final output = await compressor.compress(param);
print("Input size : ${input.sizeInBytes}");
print("Output size : ${output.sizeInBytes}");
完整示例代码
以下是一个完整的示例demo,展示了如何使用image_compression_flutter
来选择、显示并压缩图片:
import 'package:flutter/material.dart';
import 'package:image_compression_flutter/image_compression_flutter.dart';
import 'package:image_picker/image_picker.dart';
void main() {
runApp(MaterialApp(home: MainPage()));
}
class MainPage extends StatefulWidget {
@override
_MainPageState createState() => _MainPageState();
}
class _MainPageState extends State<MainPage> {
Configuration config = Configuration();
ImageFile? image;
ImageFile? imageOutput;
bool processing = false;
@override
Widget build(BuildContext context) {
final buttonGallery = ElevatedButton.icon(
onPressed: handleOpenGallery,
icon: Icon(Icons.photo_outlined),
label: Text('Pick an Image'),
);
final buttonCompress = Padding(
padding: const EdgeInsets.all(16),
child: ElevatedButton.icon(
onPressed: handleCompressImage,
icon: Icon(Icons.compress),
label: Text('Compress Image'),
),
);
Widget body = Center(child: buttonGallery);
if (image != null) {
final inputImage = Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Image.memory(image!.rawBytes),
);
final inputImageSizeType = ListTile(
title: Text('Image size-type :'),
subtitle: Text(
'${(image!.sizeInBytes / 1024 / 1024).toStringAsFixed(2)} MB - (${image!.width} x ${image!.height})'),
trailing: Text(image!.extension),
);
final inputImageName = ListTile(
title: Text('Image name :'),
subtitle: Text(image!.fileName),
);
body = SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Padding(
padding: const EdgeInsets.fromLTRB(16, 16, 16, 0),
child: buttonGallery,
),
ListTile(title: Text('INPUT IMAGE')),
inputImage,
inputImageSizeType,
inputImageName,
Divider(),
ListTile(title: Text('OUTPUT IMAGE')),
configOutputType,
configQuality,
nativeCompressorCheckBox,
buttonCompress,
processing
? Padding(
padding: const EdgeInsets.all(16.0),
child: LinearProgressIndicator(),
)
: SizedBox.shrink(),
if (imageOutput != null && !processing)
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Image.memory(imageOutput!.rawBytes),
),
if (imageOutput != null && !processing)
ListTile(
title: Text('Image size-type :'),
subtitle: Text(
'${(imageOutput!.sizeInBytes / 1024 / 1024).toStringAsFixed(2)} MB - (${imageOutput!.width} x ${imageOutput!.height})'),
trailing: Text(imageOutput!.contentType ?? '-'),
),
],
),
);
}
return Scaffold(
appBar: AppBar(
title: const Text('Image Compression'),
),
body: body,
);
}
Widget get nativeCompressorCheckBox {
return CheckboxListTile(
title: Text('Native compressor for JPG/PNG'),
value: config.useJpgPngNativeCompressor,
onChanged: (flag) {
setState(() {
config = Configuration(
outputType: config.outputType,
useJpgPngNativeCompressor: flag ?? false,
quality: config.quality,
);
});
},
);
}
Widget get configOutputType {
return ListTile(
title: Text('Select output type'),
subtitle: Text(config.outputType.toString()),
trailing: PopupMenuButton<ImageOutputType>(
itemBuilder: (context) {
return ImageOutputType.values
.map((e) => PopupMenuItem(
child: Text(e.toString()),
value: e,
))
.toList();
},
onSelected: (outputType) {
setState(() {
config = Configuration(
outputType: outputType,
useJpgPngNativeCompressor: config.useJpgPngNativeCompressor,
quality: config.quality,
);
});
},
),
);
}
Widget get configQuality {
return ListTile(
title: Text('Select quality (${config.quality})'),
subtitle: Slider.adaptive(
value: config.quality.toDouble(),
divisions: 100,
min: 0,
max: 100,
label: config.quality.toString(),
onChanged: (value) {
setState(() {
config = Configuration(
outputType: config.outputType,
useJpgPngNativeCompressor: config.useJpgPngNativeCompressor,
quality: value.toInt(),
);
});
},
),
);
}
handleOpenGallery() async {
final picker = ImagePicker();
final pickedFile = await picker.pickImage(source: ImageSource.gallery);
if (pickedFile != null) {
final inputImage = await pickedFile.readAsBytes();
final file = ImageFile.fromRawPath(
rawPath: inputImage,
fileName: pickedFile.name,
);
setState(() {
this.image = file;
});
}
}
handleCompressImage() async {
if (image == null) return;
setState(() {
processing = true;
});
final param = ImageFileConfiguration(input: image!, config: config);
final output = await compressor.compress(param);
setState(() {
imageOutput = output;
processing = false;
});
}
}
注意:上述代码中的flimer
应替换为image_picker
,因为flimer
不是标准的Flutter包。如果您使用的是image_picker
,请确保已经添加到您的pubspec.yaml
文件中。
更多关于Flutter图片压缩插件image_compression_flutter的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter图片压缩插件image_compression_flutter的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是一个关于如何使用 image_compression_flutter
插件来压缩图片的 Flutter 代码示例。这个插件允许你在 Flutter 应用中压缩 JPEG 和 PNG 格式的图片。
首先,你需要在 pubspec.yaml
文件中添加这个依赖:
dependencies:
flutter:
sdk: flutter
image_compression_flutter: ^1.0.0 # 请使用最新版本号
然后运行 flutter pub get
来获取依赖。
接下来,你可以在你的 Flutter 应用中使用这个插件来压缩图片。以下是一个完整的示例,包括选择图片、压缩图片以及显示压缩后的图片:
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:image_compression_flutter/image_compression_flutter.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomeScreen(),
);
}
}
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
File? _imageFile;
File? _compressedImageFile;
final ImagePicker _picker = ImagePicker();
Future<void> _pickImage() async {
final pickedFile = await _picker.pickImage(source: ImageSource.camera);
if (pickedFile != null) {
setState(() {
_imageFile = File(pickedFile.path);
});
_compressImage(_imageFile!);
}
}
Future<void> _compressImage(File file) async {
try {
final compressedFile = await ImageCompression.compressFile(
file.path,
quality: 80, // 压缩质量,范围 0-100
maxWidth: 1920, // 最大宽度
maxHeight: 1080, // 最大高度
);
setState(() {
_compressedImageFile = compressedFile;
});
} catch (e) {
print("压缩图片失败: $e");
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('图片压缩示例'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
_imageFile != null
? Image.file(
_imageFile!,
width: 300,
height: 300,
fit: BoxFit.cover,
)
: Container(),
SizedBox(height: 20),
_compressedImageFile != null
? Image.file(
_compressedImageFile!,
width: 300,
height: 300,
fit: BoxFit.cover,
)
: Container(),
SizedBox(height: 20),
ElevatedButton(
onPressed: _pickImage,
child: Text('选择图片'),
),
],
),
),
);
}
}
在这个示例中,我们使用了 image_picker
插件来选择图片。选择图片后,我们使用 image_compression_flutter
插件来压缩图片,并显示原始图片和压缩后的图片。
请确保在 android/app/src/main/AndroidManifest.xml
文件中添加必要的权限,例如相机和存储权限,如果你打算从相机选择图片:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
并在运行时请求这些权限(这里省略了权限请求的代码,你可以使用 permission_handler
插件来处理运行时权限)。
希望这个示例对你有所帮助!