mobile_skt/lib/util/media_util.dart

200 lines
5.6 KiB
Dart
Raw Normal View History

import 'dart:io';
2024-03-26 17:03:27 +08:00
import 'dart:typed_data';
2024-03-31 17:13:29 +08:00
import 'package:dio/dio.dart' as dio_package;
2024-03-26 17:03:27 +08:00
import 'package:dio/dio.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';
2024-03-26 15:30:43 +08:00
import 'package:sk_base_mobile/apis/index.dart';
import 'package:image_picker/image_picker.dart';
2024-03-26 11:35:39 +08:00
import 'package:sk_base_mobile/models/upload_result.model.dart';
import 'package:sk_base_mobile/services/service.dart';
class MediaUtil {
//拍照
2024-03-27 11:06:01 +08:00
Future<XFile?> getImageFromCamera(
{double? maxWidth, bool isFront = false}) async {
XFile? pickedFile;
try {
if (AppInfoService.to.isCameraing.value) {
return null;
}
AppInfoService.to.isCameraing.value = true;
2024-03-27 11:06:01 +08:00
pickedFile = await ImagePicker().pickImage(
source: ImageSource.camera,
maxWidth: maxWidth ?? 2000,
preferredCameraDevice:
isFront ? CameraDevice.front : CameraDevice.rear);
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;
}
2024-03-26 11:35:39 +08:00
Future<UploadResultModel?> uploadImg(File imgfile,
{int? bussinessRecordId, String? bussinessModule}) async {
2024-03-31 17:13:29 +08:00
dio_package.Response response = await Api.uploadImg(imgfile);
2024-03-26 11:35:39 +08:00
if (response.data != null && response.data['filename'] != null) {
return UploadResultModel.fromJson(response.data["filename"]);
}
2024-03-26 11:35:39 +08:00
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('上传照片失败');
// }
// }
2024-03-26 17:03:27 +08:00
// 保存到相册
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'
// ];
}