196 lines
5.5 KiB
Dart
196 lines
5.5 KiB
Dart
import 'dart:io';
|
|
import 'dart:typed_data';
|
|
import 'package:dio/dio.dart' as Dio;
|
|
import 'package:dio/dio.dart';
|
|
import 'package:image_gallery_saver/image_gallery_saver.dart';
|
|
import 'package:sk_base_mobile/apis/index.dart';
|
|
import 'package:image_picker/image_picker.dart';
|
|
import 'package:sk_base_mobile/models/upload_result.model.dart';
|
|
import 'package:sk_base_mobile/services/service.dart';
|
|
|
|
class MediaUtil {
|
|
//拍照
|
|
Future<XFile?> getImageFromCamera({double? maxWidth}) async {
|
|
XFile? pickedFile;
|
|
try {
|
|
if (AppInfoService.to.isCameraing.value) {
|
|
return null;
|
|
}
|
|
AppInfoService.to.isCameraing.value = true;
|
|
pickedFile = await ImagePicker()
|
|
.pickImage(source: ImageSource.camera, maxWidth: maxWidth ?? 2000);
|
|
AppInfoService.to.isCameraing.value = false;
|
|
} catch (e) {
|
|
AppInfoService.to.isCameraing.value = false;
|
|
}
|
|
return pickedFile;
|
|
}
|
|
|
|
//相册选择 单张
|
|
Future<XFile?> getImageFromGallery() async {
|
|
if (AppInfoService.to.isCameraing.value) {
|
|
return null;
|
|
}
|
|
AppInfoService.to.isCameraing.value = true;
|
|
XFile? pickedFile = await ImagePicker().pickImage(
|
|
source: ImageSource.gallery,
|
|
);
|
|
AppInfoService.to.isCameraing.value = false;
|
|
return pickedFile;
|
|
}
|
|
|
|
Future<UploadResultModel?> uploadImg(File imgfile,
|
|
{int? bussinessRecordId, String? bussinessModule}) async {
|
|
Dio.Response response = await Api.uploadImg(imgfile);
|
|
if (response.data != null && response.data['filename'] != null) {
|
|
return UploadResultModel.fromJson(response.data["filename"]);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
// // 上传一张照片
|
|
// Future<String> uploadMediaWithProgress(
|
|
// File imgfile, Function onProgress) async {
|
|
// Response response = await Api.uploadMediaWithProgress(imgfile, onProgress);
|
|
// if (response.data != null && response.data['code'] == 200) {
|
|
// return response.data['data']['url'];
|
|
// } else {
|
|
// throw Exception('上传照片失败');
|
|
// }
|
|
// }
|
|
|
|
// // 上传多张照片
|
|
// Future<List<String>> uploadMultiImgs(List<File> imgfiles) async {
|
|
// Response response = await Api.uploadMultiImgs(imgfiles);
|
|
// List<String> urlList = [];
|
|
// if (response.data != null && response.data['code'] == 200) {
|
|
// String url = response.data['data']['url'];
|
|
// urlList = url.split(',');
|
|
// return urlList;
|
|
// } else {
|
|
// throw Exception('上传照片失败');
|
|
// }
|
|
// }
|
|
|
|
// 保存到相册
|
|
Future saveImageToPhotoLib({required String imgUrl, String? filename}) async {
|
|
var response = await DioService.dio
|
|
.get(imgUrl, options: Options(responseType: ResponseType.bytes));
|
|
final result = await ImageGallerySaver.saveImage(
|
|
Uint8List.fromList(response.data),
|
|
quality: 60);
|
|
return result;
|
|
}
|
|
|
|
// ///选取视频
|
|
// Future<XFile?> getVideoFromGallery() async {
|
|
// if (AppInfoModel().isCameraing) {
|
|
// return null;
|
|
// }
|
|
// XFile? pickedFile =
|
|
// await ImagePicker().pickVideo(source: ImageSource.gallery);
|
|
// if (pickedFile != null) {
|
|
// var path = pickedFile.path;
|
|
// if (Platform.isAndroid) {
|
|
// var newPath = path.replaceAll('.jpg', '.mp4');
|
|
// File(path).renameSync(newPath);
|
|
// return XFile(newPath);
|
|
// }
|
|
// }
|
|
// return pickedFile;
|
|
// }
|
|
|
|
// /*拍摄视频*/
|
|
// Future takeVideoFromCamera() async {
|
|
// var image = await ImagePicker().getVideo(source: ImageSource.camera);
|
|
// print('拍摄视频:' + image.toString());
|
|
// }
|
|
|
|
// /// 生成视频缩略图
|
|
// Future<String> generateLogoUrl(String videoUrl) async {
|
|
// File file = await generateLogoFile(videoUrl: videoUrl);
|
|
// return await MediaUtil().uploadImg(file);
|
|
// }
|
|
|
|
// /// 生成视频缩略图
|
|
// Future<File> generateLogoFile({String? videoUrl, String? videoPath}) async {
|
|
// Directory fileDic = await getApplicationDocumentsDirectory();
|
|
|
|
// String? path = await VideoThumbnail.thumbnailFile(
|
|
// video: videoUrl ?? videoPath!,
|
|
// imageFormat: ImageFormat.JPEG,
|
|
// thumbnailPath: fileDic.path,
|
|
// // maxWidth: 200,
|
|
// // 64, // specify the height of the thumbnail, let the width auto-scaled to keep the source aspect ratio
|
|
// quality: 100,
|
|
// );
|
|
// return File(path!);
|
|
// }
|
|
|
|
// /// 获取文件类型Video/audio/image
|
|
// MediaTypeEnum getMediaTypeBySuffix(String? url) {
|
|
// if (url == null || url == '') {
|
|
// return MediaTypeEnum.IMAGE;
|
|
// }
|
|
// String suffix = url.split('.')[url.split('.').length - 1].toLowerCase();
|
|
// if (videoSuffix.contains(suffix)) {
|
|
// return MediaTypeEnum.VIDEO;
|
|
// } else if (audioSuffix.contains(suffix)) {
|
|
// return MediaTypeEnum.AUDIO;
|
|
// }
|
|
// return MediaTypeEnum.IMAGE;
|
|
// }
|
|
|
|
// List<String> videoSuffix = [
|
|
// 'avi',
|
|
// 'wmv',
|
|
// 'mpg',
|
|
// 'mpeg',
|
|
// 'mov',
|
|
// 'rm',
|
|
// 'ram',
|
|
// 'swf',
|
|
// 'flv',
|
|
// 'mp4'
|
|
// ];
|
|
// List<String> audioSuffix = [
|
|
// 'aac',
|
|
// 'mp3',
|
|
// 'cda',
|
|
// 'wav',
|
|
// 'wma',
|
|
// 'ra',
|
|
// 'rma',
|
|
// 'ape',
|
|
// 'asf',
|
|
// 'mid',
|
|
// 'midi',
|
|
// 'rmi',
|
|
// 'xmi',
|
|
// 'ogg',
|
|
// 'ape',
|
|
// 'aiff',
|
|
// 'au'
|
|
// ];
|
|
// List<String> imageSuffix = [
|
|
// 'webp',
|
|
// 'bmp',
|
|
// 'pcx',
|
|
// 'tif',
|
|
// 'gif',
|
|
// 'jpeg',
|
|
// 'tga',
|
|
// 'exif',
|
|
// 'fpx',
|
|
// 'svg',
|
|
// 'psd',
|
|
// 'cdr',
|
|
// 'png',
|
|
// 'ogg',
|
|
// 'ape',
|
|
// 'aiff',
|
|
// 'au',
|
|
// 'jpg'
|
|
// ];
|
|
}
|