From e5a2b1e3e200c44622a4b29faa14f5ce0bd9b216 Mon Sep 17 00:00:00 2001 From: kang-breakfly Date: Wed, 11 May 2022 16:11:53 +0800 Subject: [PATCH 1/5] Add files via upload --- images_picker.dart | 204 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 204 insertions(+) create mode 100644 images_picker.dart diff --git a/images_picker.dart b/images_picker.dart new file mode 100644 index 0000000..23f359f --- /dev/null +++ b/images_picker.dart @@ -0,0 +1,204 @@ +import 'dart:async'; +import 'dart:io'; +import 'package:flutter/services.dart'; + +class ImagesPicker { + static const MethodChannel _channel = + const MethodChannel('chavesgu/images_picker'); + + static Future?> pick({ + int count = 1, + PickType pickType = PickType.image, + bool gif = true, + int maxTime = 120, + CropOption? cropOpt, + int? maxSize, + double? quality, + Language language = Language.System, + }) async { + assert(count > 0, 'count must > 0'); + if (quality != null) { + assert(quality > 0, 'quality must > 0'); + assert(quality <= 1, 'quality must <= 1'); + } + if (maxSize != null) { + assert(maxSize > 0, 'maxSize must > 0'); + } + try { + List? res = await _channel.invokeMethod('pick', { + "count": count, + "pickType": pickType.toString(), + "gif": gif, + "maxTime": maxTime, + "maxSize": maxSize ?? null, + "quality": quality ?? -1, + "cropOption": cropOpt != null + ? { + "quality": quality ?? 1, + "cropType": cropOpt.cropType.toString(), + "aspectRatioX": cropOpt.aspectRatio?.aspectRatioX, + "aspectRatioY": cropOpt.aspectRatio?.aspectRatioY, + } + : null, + "language": language.toString(), + }); + if (res != null) { + List output = res.map((image) { + Media media = Media( + path: image["path"], + size: ((image["size"] ?? 0) / 1024).toDouble(), + thumbPath: image["thumbPath"], + duration: image["duration"] ?? 0, + ); + return media; + }).toList(); + return output; + } + return null; + } catch (e) { + return null; + } + } + + static Future?> openCamera({ + PickType pickType = PickType.image, + int maxTime = 15, + CropOption? cropOpt, + int? maxSize, + double? quality, + Language language = Language.System, + }) async { + if (quality != null) { + assert(quality > 0, 'quality must > 0'); + assert(quality <= 1, 'quality must <= 1'); + } + if (maxSize != null) { + assert(maxSize > 0, 'maxSize must > 0'); + } + try { + List? res = await _channel.invokeMethod('openCamera', { + "pickType": pickType.toString(), + "maxTime": maxTime, + "maxSize": maxSize ?? null, + "quality": quality ?? -1, + "cropOption": cropOpt != null + ? { + "quality": quality ?? 1, + "cropType": cropOpt.cropType.toString(), + "aspectRatioX": cropOpt.aspectRatio?.aspectRatioX, + "aspectRatioY": cropOpt.aspectRatio?.aspectRatioY, + } + : null, + "language": language.toString(), + }); + if (res != null) { + List output = res.map((image) { + Media media = Media( + path: image["path"], + size: ((image["size"] ?? 0) / 1024).toDouble(), + thumbPath: image["thumbPath"], + duration: image["duration"] ?? 0, + ); + return media; + }).toList(); + return output; + } + return null; + } catch (e) { + return null; + } + } + + static Future saveImageToAlbum(File file, {String? albumName}) async { + try { + return await _channel.invokeMethod('saveImageToAlbum', { + "path": file.path, + "albumName": albumName, + }); + } on PlatformException catch (e) { + print(e); + return false; + } + } + + static Future saveVideoToAlbum(File file, {String? albumName}) async { + try { + return await _channel.invokeMethod('saveVideoToAlbum', { + "path": file.path, + "albumName": albumName, + }); + } on PlatformException catch (e) { + print(e); + return false; + } + } +} + +enum PickType { + image, + video, + all, +} + +enum CropType { + rect, + circle, +} + +enum Language { + System, + Chinese, + ChineseTraditional, + English, + Japanese, + French, + Korean, + German, + Vietnamese, +} + +class CropAspectRatio { + final int aspectRatioX; + final int aspectRatioY; + + const CropAspectRatio(this.aspectRatioX, this.aspectRatioY) + : assert(aspectRatioX > 0, 'aspectRatioX must > 0'), + assert(aspectRatioY > 0, 'aspectRatioY must > 0'); + + static const custom = null; + static const wh2x1 = CropAspectRatio(2, 1); + static const wh1x2 = CropAspectRatio(1, 2); + static const wh3x4 = CropAspectRatio(3, 4); + static const wh4x3 = CropAspectRatio(4, 3); + static const wh16x9 = CropAspectRatio(16, 9); + static const wh9x16 = CropAspectRatio(9, 16); +} + +class CropOption { + final CropType cropType; + final CropAspectRatio? aspectRatio; + + CropOption({ + this.aspectRatio = CropAspectRatio.custom, + this.cropType = CropType.rect, + }); +} + +class Media { + ///视频缩略图图片路径 + ///Video thumbnail image path + String? thumbPath; + + ///视频路径或图片路径 + ///Video path or image path + String path; + + /// 文件大小 + double size; + + // 时长 仅视频有效 + double? duration; + + Media( + {required this.path, this.thumbPath, required this.size, this.duration}); +} From 6ed88ea26951a17d20701d340c7fe3f92eff2a0a Mon Sep 17 00:00:00 2001 From: kang-breakfly Date: Wed, 11 May 2022 16:12:19 +0800 Subject: [PATCH 2/5] Delete images_picker.dart --- images_picker.dart | 204 --------------------------------------------- 1 file changed, 204 deletions(-) delete mode 100644 images_picker.dart diff --git a/images_picker.dart b/images_picker.dart deleted file mode 100644 index 23f359f..0000000 --- a/images_picker.dart +++ /dev/null @@ -1,204 +0,0 @@ -import 'dart:async'; -import 'dart:io'; -import 'package:flutter/services.dart'; - -class ImagesPicker { - static const MethodChannel _channel = - const MethodChannel('chavesgu/images_picker'); - - static Future?> pick({ - int count = 1, - PickType pickType = PickType.image, - bool gif = true, - int maxTime = 120, - CropOption? cropOpt, - int? maxSize, - double? quality, - Language language = Language.System, - }) async { - assert(count > 0, 'count must > 0'); - if (quality != null) { - assert(quality > 0, 'quality must > 0'); - assert(quality <= 1, 'quality must <= 1'); - } - if (maxSize != null) { - assert(maxSize > 0, 'maxSize must > 0'); - } - try { - List? res = await _channel.invokeMethod('pick', { - "count": count, - "pickType": pickType.toString(), - "gif": gif, - "maxTime": maxTime, - "maxSize": maxSize ?? null, - "quality": quality ?? -1, - "cropOption": cropOpt != null - ? { - "quality": quality ?? 1, - "cropType": cropOpt.cropType.toString(), - "aspectRatioX": cropOpt.aspectRatio?.aspectRatioX, - "aspectRatioY": cropOpt.aspectRatio?.aspectRatioY, - } - : null, - "language": language.toString(), - }); - if (res != null) { - List output = res.map((image) { - Media media = Media( - path: image["path"], - size: ((image["size"] ?? 0) / 1024).toDouble(), - thumbPath: image["thumbPath"], - duration: image["duration"] ?? 0, - ); - return media; - }).toList(); - return output; - } - return null; - } catch (e) { - return null; - } - } - - static Future?> openCamera({ - PickType pickType = PickType.image, - int maxTime = 15, - CropOption? cropOpt, - int? maxSize, - double? quality, - Language language = Language.System, - }) async { - if (quality != null) { - assert(quality > 0, 'quality must > 0'); - assert(quality <= 1, 'quality must <= 1'); - } - if (maxSize != null) { - assert(maxSize > 0, 'maxSize must > 0'); - } - try { - List? res = await _channel.invokeMethod('openCamera', { - "pickType": pickType.toString(), - "maxTime": maxTime, - "maxSize": maxSize ?? null, - "quality": quality ?? -1, - "cropOption": cropOpt != null - ? { - "quality": quality ?? 1, - "cropType": cropOpt.cropType.toString(), - "aspectRatioX": cropOpt.aspectRatio?.aspectRatioX, - "aspectRatioY": cropOpt.aspectRatio?.aspectRatioY, - } - : null, - "language": language.toString(), - }); - if (res != null) { - List output = res.map((image) { - Media media = Media( - path: image["path"], - size: ((image["size"] ?? 0) / 1024).toDouble(), - thumbPath: image["thumbPath"], - duration: image["duration"] ?? 0, - ); - return media; - }).toList(); - return output; - } - return null; - } catch (e) { - return null; - } - } - - static Future saveImageToAlbum(File file, {String? albumName}) async { - try { - return await _channel.invokeMethod('saveImageToAlbum', { - "path": file.path, - "albumName": albumName, - }); - } on PlatformException catch (e) { - print(e); - return false; - } - } - - static Future saveVideoToAlbum(File file, {String? albumName}) async { - try { - return await _channel.invokeMethod('saveVideoToAlbum', { - "path": file.path, - "albumName": albumName, - }); - } on PlatformException catch (e) { - print(e); - return false; - } - } -} - -enum PickType { - image, - video, - all, -} - -enum CropType { - rect, - circle, -} - -enum Language { - System, - Chinese, - ChineseTraditional, - English, - Japanese, - French, - Korean, - German, - Vietnamese, -} - -class CropAspectRatio { - final int aspectRatioX; - final int aspectRatioY; - - const CropAspectRatio(this.aspectRatioX, this.aspectRatioY) - : assert(aspectRatioX > 0, 'aspectRatioX must > 0'), - assert(aspectRatioY > 0, 'aspectRatioY must > 0'); - - static const custom = null; - static const wh2x1 = CropAspectRatio(2, 1); - static const wh1x2 = CropAspectRatio(1, 2); - static const wh3x4 = CropAspectRatio(3, 4); - static const wh4x3 = CropAspectRatio(4, 3); - static const wh16x9 = CropAspectRatio(16, 9); - static const wh9x16 = CropAspectRatio(9, 16); -} - -class CropOption { - final CropType cropType; - final CropAspectRatio? aspectRatio; - - CropOption({ - this.aspectRatio = CropAspectRatio.custom, - this.cropType = CropType.rect, - }); -} - -class Media { - ///视频缩略图图片路径 - ///Video thumbnail image path - String? thumbPath; - - ///视频路径或图片路径 - ///Video path or image path - String path; - - /// 文件大小 - double size; - - // 时长 仅视频有效 - double? duration; - - Media( - {required this.path, this.thumbPath, required this.size, this.duration}); -} From b7e66d9ade46394180c6cc5fd8a6b0fb93ecc9b0 Mon Sep 17 00:00:00 2001 From: kang-breakfly Date: Wed, 11 May 2022 16:12:46 +0800 Subject: [PATCH 3/5] Add files via upload --- lib/images_picker.dart | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/images_picker.dart b/lib/images_picker.dart index a5b50cc..23f359f 100644 --- a/lib/images_picker.dart +++ b/lib/images_picker.dart @@ -48,6 +48,7 @@ class ImagesPicker { path: image["path"], size: ((image["size"] ?? 0) / 1024).toDouble(), thumbPath: image["thumbPath"], + duration: image["duration"] ?? 0, ); return media; }).toList(); @@ -96,6 +97,7 @@ class ImagesPicker { path: image["path"], size: ((image["size"] ?? 0) / 1024).toDouble(), thumbPath: image["thumbPath"], + duration: image["duration"] ?? 0, ); return media; }).toList(); @@ -194,9 +196,9 @@ class Media { /// 文件大小 double size; - Media({ - required this.path, - this.thumbPath, - required this.size, - }); + // 时长 仅视频有效 + double? duration; + + Media( + {required this.path, this.thumbPath, required this.size, this.duration}); } From c27734f84b6ed04907166a73ccd41a48f9082d1e Mon Sep 17 00:00:00 2001 From: kang-breakfly Date: Wed, 11 May 2022 16:13:56 +0800 Subject: [PATCH 4/5] Add files via upload --- .../java/com/chavesgu/images_picker/ImagesPickerPlugin.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/android/src/main/java/com/chavesgu/images_picker/ImagesPickerPlugin.java b/android/src/main/java/com/chavesgu/images_picker/ImagesPickerPlugin.java index 169be90..4ff75f4 100644 --- a/android/src/main/java/com/chavesgu/images_picker/ImagesPickerPlugin.java +++ b/android/src/main/java/com/chavesgu/images_picker/ImagesPickerPlugin.java @@ -258,12 +258,15 @@ public void run() { map.put("path", path); String thumbPath; + double duration = 0; if (media.getMimeType().contains("image")) { thumbPath = path; } else { thumbPath = createVideoThumb(path); + duration = media.getDuration()/1000.0; } map.put("thumbPath", thumbPath); + map.put("duration", duration); int size = getFileSize(path); map.put("size", size); From e50f786d4483482e7af1d2adf2e7293d360045a1 Mon Sep 17 00:00:00 2001 From: kang-breakfly Date: Wed, 11 May 2022 16:14:30 +0800 Subject: [PATCH 5/5] Add files via upload --- ios/Classes/SwiftImagesPickerPlugin.swift | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/ios/Classes/SwiftImagesPickerPlugin.swift b/ios/Classes/SwiftImagesPickerPlugin.swift index 90ec9ec..a338138 100644 --- a/ios/Classes/SwiftImagesPickerPlugin.swift +++ b/ios/Classes/SwiftImagesPickerPlugin.swift @@ -83,7 +83,12 @@ public class SwiftImagesPickerPlugin: NSObject, FlutterPlugin { let videoUrl = avasset as! AVURLAsset; let url = videoUrl.url; // TODO: mov to mp4 - resArr.append(self.resolveVideo(url: url)); + let dir =self.resolveVideo(url: url); + // let audioDuration = videoUrl.duration; + // let audioDurationSeconds = CMTimeGetSeconds(audioDuration); + //dir.updateValue(audioDurationSeconds, forKey: "duration"); + + resArr.append(dir); group.leave(); }) } else { @@ -298,7 +303,12 @@ public class SwiftImagesPickerPlugin: NSObject, FlutterPlugin { let urlStr = url.absoluteString; let path = (urlStr as NSString).substring(from: 7); dir.updateValue(path, forKey: "path"); - + + let video = AVURLAsset(url:url); + let audioDuration = video.duration; + + let audioDurationSeconds = CMTimeGetSeconds(audioDuration); + dir.updateValue(audioDurationSeconds, forKey: "duration"); // 获取视频封面图 if let thumb = self.getVideoThumbPath(url: path) { let thumbData = thumb.jpegData(compressionQuality: 1); // 转Data