compress_handler.dart
4.25 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import 'dart:io';
import 'package:appframe/services/dispatcher.dart';
import 'package:dio/dio.dart';
import 'package:flutter_image_compress/flutter_image_compress.dart';
import 'package:path/path.dart' as path;
import 'package:path_provider/path_provider.dart';
import 'package:video_compress/video_compress.dart';
/// 压缩图片
///
class CompressImageHandler extends MessageHandler {
@override
Future<dynamic> handleMessage(dynamic params) async {
if (params is! Map<String, dynamic>) {
throw Exception('参数错误');
}
final url = params['url'] as String?;
if (url == null || url.isEmpty) {
throw Exception('参数错误');
}
final quality = params['quality'] as int?;
if (quality == null) {
throw Exception('参数错误');
}
var compressedWidth = params['compressedWidth'] as int?;
// if (compressedWidth == null) {
// throw Exception('参数错误');
// }
var compressedHeight = params['compressedHeight'] as int?;
// if (compressedHeight == null) {
// throw Exception('参数错误');
// }
if (compressedWidth == null && compressedHeight == null) {
throw Exception('参数错误');
}
if (compressedWidth == null) {
compressedWidth = compressedHeight;
} else if (compressedHeight == null) {
compressedHeight = compressedWidth;
}
// 获取后缀名
String ext = path.extension(url);
final Directory tempDir = await getTemporaryDirectory();
String srcPath;
// 如果是远程文件,先进行下载
if (url.startsWith('http')) {
srcPath = path.join(tempDir.path, '${DateTime.now().millisecondsSinceEpoch}$ext');
// Dio 下载文件
final resp = await Dio().download(url, srcPath);
if (resp.statusCode != 200) {
throw Exception('远程文件下载失败');
}
} else {
srcPath = url;
}
var originFile = File(srcPath);
if (!originFile.existsSync()) {
throw Exception('文件不存在');
}
final String uniqueFileName = '${DateTime.now().millisecondsSinceEpoch}$ext';
final String tempPath = path.join(tempDir.path, uniqueFileName);
// 压缩处理
var croppedFile = await FlutterImageCompress.compressAndGetFile(
originFile.absolute.path,
tempPath,
quality: quality,
minWidth: compressedWidth!,
minHeight: compressedHeight!,
);
if (croppedFile == null) {
throw Exception('图片压缩失败');
}
return {"tempFilePath": "/temp${croppedFile.path}", "size": await croppedFile.length()};
}
}
/// 压缩视频
///
class CompressVideoHandler extends MessageHandler {
@override
Future<dynamic> handleMessage(dynamic params) async {
if (params is! Map<String, dynamic>) {
throw Exception('参数错误');
}
final url = params['url'] as String?;
if (url == null || url.isEmpty) {
throw Exception('参数错误');
}
final quality = params['quality'] as String?;
if (quality == null || quality.isEmpty) {
throw Exception('参数错误');
}
final resolution = params['resolution'] as double?;
if (resolution == null || resolution > 1 || resolution <= 0) {
throw Exception('参数错误');
}
String srcPath;
if (url.startsWith('http')) {
String ext = path.extension(url);
final Directory tempDir = await getTemporaryDirectory();
srcPath = path.join(tempDir.path, '${DateTime.now().millisecondsSinceEpoch}$ext');
// Dio 下载文件
final resp = await Dio().download(url, srcPath);
if (resp.statusCode != 200) {
throw Exception('远程文件下载失败');
}
} else {
srcPath = url;
}
VideoQuality videoQuality;
switch (quality) {
case 'low':
videoQuality = VideoQuality.LowQuality;
break;
case 'middle':
videoQuality = VideoQuality.MediumQuality;
break;
case 'high':
videoQuality = VideoQuality.HighestQuality;
break;
default:
throw Exception('参数错误');
}
final mediaInfo = await VideoCompress.compressVideo(
srcPath,
quality: videoQuality,
deleteOrigin: false,
includeAudio: true,
);
return {"tempFilePath": "/temp${mediaInfo!.path}", "size": mediaInfo.filesize};
}
}