需要配置的插件:
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 上傳 服務(wù)器 柬焕,實用類
****
**/
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);
? }
}
復(fù)制上述代碼:調(diào)用即可搅轿,相冊選取的視頻壓縮,有待完善富玷,后期會更新...