You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 line
2.8KB

  1. import 'dart:io';
  2. import 'package:flutter/services.dart';
  3. import 'package:image_picker/image_picker.dart';
  4. import 'package:path_provider/path_provider.dart';
  5. import 'package:path/path.dart' as path;
  6. /// Service pour gérer l'accès aux images et vidéos
  7. class ImageService {
  8. final ImagePicker _picker = ImagePicker();
  9. /// Sélectionner une image depuis la galerie
  10. Future<File?> pickImageFromGallery() async {
  11. try {
  12. final XFile? image = await _picker.pickImage(
  13. source: ImageSource.gallery,
  14. maxWidth: 1920,
  15. maxHeight: 1920,
  16. imageQuality: 90,
  17. );
  18. return image != null ? File(image.path) : null;
  19. } on PlatformException catch (e) {
  20. throw ImagePickerException('Failed to pick image: ${e.message}');
  21. }
  22. }
  23. /// Capturer une image avec la caméra
  24. Future<File?> captureImageFromCamera() async {
  25. try {
  26. final XFile? photo = await _picker.pickImage(
  27. source: ImageSource.camera,
  28. maxWidth: 1920,
  29. maxHeight: 1920,
  30. imageQuality: 90,
  31. );
  32. return photo != null ? File(photo.path) : null;
  33. } on PlatformException catch (e) {
  34. throw ImagePickerException('Failed to capture image: ${e.message}');
  35. }
  36. }
  37. /// Sélectionner une vidéo depuis la galerie
  38. Future<File?> pickVideoFromGallery() async {
  39. try {
  40. final XFile? video = await _picker.pickVideo(
  41. source: ImageSource.gallery,
  42. maxDuration: const Duration(minutes: 2),
  43. );
  44. return video != null ? File(video.path) : null;
  45. } on PlatformException catch (e) {
  46. throw ImagePickerException('Failed to pick video: ${e.message}');
  47. }
  48. }
  49. /// Capturer une vidéo avec la caméra
  50. Future<File?> captureVideoFromCamera() async {
  51. try {
  52. final XFile? video = await _picker.pickVideo(
  53. source: ImageSource.camera,
  54. maxDuration: const Duration(minutes: 2),
  55. );
  56. return video != null ? File(video.path) : null;
  57. } on PlatformException catch (e) {
  58. throw ImagePickerException('Failed to capture video: ${e.message}');
  59. }
  60. }
  61. /// Sauvegarder un fichier dans le dossier documents
  62. Future<File> saveToAppDirectory(File sourceFile, String fileName) async {
  63. try {
  64. final directory = await getApplicationDocumentsDirectory();
  65. final filePath = path.join(directory.path, fileName);
  66. return await sourceFile.copy(filePath);
  67. } catch (e) {
  68. throw ImagePickerException('Failed to save file: $e');
  69. }
  70. }
  71. /// Obtenir la taille d'un fichier en MB
  72. double getFileSizeInMB(File file) {
  73. return file.lengthSync() / (1024 * 1024);
  74. }
  75. }
  76. /// Exception personnalisée pour ImageService
  77. class ImagePickerException implements Exception {
  78. final String message;
  79. ImagePickerException(this.message);
  80. @override
  81. String toString() => 'ImagePickerException: $message';
  82. }