image_util.dart
1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import 'dart:io';
import 'package:ffmpeg_kit_flutter_new/ffmpeg_kit.dart';
import 'package:ffmpeg_kit_flutter_new/return_code.dart';
import 'package:path_provider/path_provider.dart';
class ImageUtil {
/// 使用 ffmpeg_kit_flutter_new 生成指定图片的缩略图
///
/// [imagePath] 原始图片路径
/// [width] 缩略图宽度,默认值为320
/// [quality] JPEG 输出质量 (1-32,数值越小质量越高),默认值为18
/// 返回缩略图路径
static Future<String?> genTempThumbnail(String imagePath, {int width = 320, int quality = 18}) async {
try {
// 检查源文件是否存在
final imageFile = File(imagePath);
if (!await imageFile.exists()) {
print('源图片文件不存在: $imagePath');
return null;
}
// 创建临时文件路径用于存储缩略图
final tempDir = await getTemporaryDirectory();
final thumbnailPath = '${tempDir.path}/thumb_${DateTime.now().millisecondsSinceEpoch}.jpg';
// 使用 FFmpeg 生成缩略图
// 构建 FFmpeg 命令行参数
String cmd = '-i "$imagePath" ' // 指定输入文件路径
'-vf scale=$width:-1:flags=lanczos ' // 设置缩略图宽度,高度等比缩放,使用 Lanczos 算法提高质量
'-q:v $quality ' // 设置 JPEG 输出质量 (1-32,数值越小质量越高)
'-y ' // 覆盖已存在的输出文件
// '-pix_fmt yuvj420p ' // 指定像素格式以提高兼容性
'"$thumbnailPath"'; // 指定输出文件路径
// 执行 FFmpeg 命令
final session = await FFmpegKit.execute(cmd);
final returnCode = await session.getReturnCode();
// 检查执行结果
if (ReturnCode.isSuccess(returnCode)) {
return thumbnailPath;
} else {
print('生成图片缩略图失败: ${await session.getFailStackTrace()}');
return null;
}
} catch (e) {
print('生成图片缩略图出错: $e');
return null;
}
}
}