vid.dart 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import 'dart:io';
  2. import 'dart:typed_data';
  3. import 'package:dio/dio.dart' as dio;
  4. import 'package:vid/us/vid_us_image.dart';
  5. import 'package:vid/us/vid_us_image_data.dart';
  6. /// Vid文件帮助工具
  7. class VidFileHelper {
  8. /// 从文件获取单帧图像
  9. ///
  10. /// [file] vid单帧图像文件对象
  11. static Future<VidUsImage?> getImageFromFile(File file) async {
  12. final data = await getDataFromFile(file);
  13. if (data != null) {
  14. final frame = data.getImage(0);
  15. return frame;
  16. }
  17. return null;
  18. }
  19. /// 从文件中获取Vid信息
  20. ///
  21. /// [file] vid文件对象
  22. static Future<VidUsImageData?> getDataFromFile(File file) async {
  23. try {
  24. final fileData = await file.readAsBytes();
  25. final vidData = VidUsImageData(fileData);
  26. return vidData;
  27. } catch (e) {
  28. // logger.e
  29. return null;
  30. }
  31. }
  32. /// 从链接获取单帧图像
  33. ///
  34. /// [url] vid单帧图像链接
  35. static Future<VidUsImage?> getImageFromNetwork(String url) async {
  36. final data = await getDataFromNetwork(url);
  37. if (data != null) {
  38. final frame = data.getImage(0);
  39. return frame;
  40. }
  41. return null;
  42. }
  43. /// 从链接获取Vid信息
  44. ///
  45. /// [url] vid文件链接
  46. static Future<VidUsImageData?> getDataFromNetwork(String url) async {
  47. try {
  48. final httpClient = dio.Dio(dio.BaseOptions(
  49. responseType: dio.ResponseType.bytes,
  50. sendTimeout: 10 * 1000,
  51. ));
  52. final response = await httpClient.get(url);
  53. httpClient.close();
  54. final bytes = response.data as Uint8List;
  55. final vidData = VidUsImageData(bytes);
  56. return vidData;
  57. } catch (e) {
  58. // logger
  59. return null;
  60. }
  61. }
  62. }