Flutter 调用相机/相册,多图选择,图片视频文件压缩上传处
需要配置的插件:
image_picker: ^0.6.7+4
multi_image_picker: ^4.6.1
flutter_image_compress: ^0.7.0
flutter_video_compress: ^0.3.7+8
代码片段:
/**
****
PhotoVideo --> 图片视频 获取 压缩 formdata 上传 服务器 ,实用类
****
**/
class PhotoVideo {
/*拍照*/
// ignore: override_on_non_overriding_member
static _takePhoto() async {
// ignore: deprecated_member_use
File image = await ImagePicker.pickImage(source: ImageSource.camera);
Navigator.pop(Router.navigatorState.currentState.context);
_saveImage(image).then((value) => {
/*
post 上传 block 传值一系列操作
*/
});
}
/*相册 单图选择*/
// ignore: unused_element
static _openGallerySingle() async {
// ignore: deprecated_member_use
var image = await ImagePicker.pickImage(source: ImageSource.gallery);
print('相册图片-->$image');
_saveImage(image).then((value) => {
/*
post 上传 block 传值一系列操作
*/
});
Navigator.pop(Router.navigatorState.currentState.context);
}
/*相册 多图选择*/
static _openGallery() async {
List<Asset> resultList = List<Asset>();
try {
resultList = await MultiImagePicker.pickImages(
maxImages: 9,
// 是否支持拍照
enableCamera: true,
materialOptions: MaterialOptions(
// 显示所有照片, false时显示相册
startInAllView: true,
allViewTitle: '所有照片',
actionBarColor: '#1ba593',
textOnNothingSelected: '没有选择照片',
selectionLimitReachedText: '图片选择超出限制,最多选择9张'),
);
} catch (e) {
print(e);
}
if (resultList.length > 0) {
_saveImages(resultList).then((value) => {
/*
post 上传 block 传值一系列操作
*/
});
}
Navigator.pop(Router.navigatorState.currentState.context);
}
/* 单图片压缩 与 flie存图*/
static Future<FormData> _saveImage(File file) async {
File imageFile = await FlutterImageCompress.compressAndGetFile(
file.absolute.path,
Directory.systemTemp.path +
'/userava' +
DateTime.now().millisecondsSinceEpoch.toString() +
'.jpg',
quality: 50,
);
print('压缩后图片文件大小:' + imageFile.lengthSync().toString());
FormData formData = FormData.fromMap({
'file': await MultipartFile.fromFile(imageFile.path,
filename: imageFile.path.substring(
imageFile.path.lastIndexOf("/") + 1, imageFile.path.length))
});
return formData;
}
/* 多图片压缩 与 flie存图*/
static Future<List> _saveImages(List<Asset> images) async {
List fileList = List();
for (int i = 0; i < images.length; i++) {
ByteData byteData = await images[i].getByteData(quality: 60);
String name = DateTime.now().millisecondsSinceEpoch.toString() + ".jpg";
List<int> imageData = byteData.buffer.asUint8List();
MultipartFile multipartFile = MultipartFile.fromBytes(
imageData,
// 文件名
filename: name,
// // 文件类型
// contentType: MediaType("image", "jpg"),
);
FormData formData = FormData.fromMap({'file': multipartFile});
if (multipartFile != null) {
fileList.add(formData);
}
}
return fileList;
}
//视频压缩 初始化
static final _flutterVideoCompress = FlutterVideoCompress();
/*拍摄视频*/
// ignore: unused_element
static _getVideo() async {
// ignore: deprecated_member_use
var image = await ImagePicker.pickVideo(
maxDuration: Duration(seconds: 10), source: ImageSource.camera);
print('视频文件大小:' + image.lengthSync().toString());
Navigator.pop(Router.navigatorState.currentState.context);
/* 视频的压缩上传 */
_upLoadVideo(image);
}
/* 视频的压缩上传 */
static _upLoadVideo(image) async {
await _flutterVideoCompress
.compressVideo(
image.path,
quality: VideoQuality.LowQuality, // 默认VideoQuality.DefaultQuality
deleteOrigin: false, // 默认(false)
)
.then((value) async => {
print('压缩后视频文件大小:' + value.toJson().toString()),
/*
post 上传 block 传值一系列操作
*/
});
}
/*选取视频*/
// ignore: unused_element
static _takeVideo() async {
// ignore: deprecated_member_use
var image = await ImagePicker.pickVideo(
source: ImageSource.gallery, preferredCameraDevice: CameraDevice.front);
print('相册选取的视频文件:$image');
/*
暂时没有更好的办法,先原文件上传
*/
Navigator.pop(Router.navigatorState.currentState.context);
}
}
复制上述代码:调用即可,相册选取的视频压缩,有待完善,后期会更新...