native_vision_helper.dart 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import 'dart:io';
  2. import 'package:image/image.dart' as img;
  3. import 'package:flutter/services.dart';
  4. import 'package:path_provider/path_provider.dart';
  5. class NativeVisionHelper {
  6. static Future<String> getModelPath(String modelName) async {
  7. final tempDir = await getTemporaryDirectory();
  8. final modelCachePath = "${tempDir.path}/$modelName";
  9. final file = File(modelCachePath);
  10. final exists = await file.exists();
  11. if (!exists) {
  12. final modelData =
  13. await rootBundle.load("packages/native_vision/assets/$modelName");
  14. final buffer = modelData.buffer;
  15. final result = await file.writeAsBytes(
  16. buffer.asUint8List(modelData.offsetInBytes, modelData.lengthInBytes));
  17. }
  18. return file.path;
  19. }
  20. // static Future<File?> fixExifRotation(String imagePath) async {
  21. // final originalFile = File(imagePath);
  22. // List<int> imageBytes = await originalFile.readAsBytes();
  23. // final originalImage = img.decodeImage(imageBytes);
  24. // if (originalImage == null) return null;
  25. // final height = originalImage.height;
  26. // final width = originalImage.width;
  27. // // Let's check for the image size
  28. // if (height >= width) {
  29. // // I'm interested in portrait photos so
  30. // // I'll just return here
  31. // return originalFile;
  32. // }
  33. // // We'll use the exif package to read exif data
  34. // // This is map of several exif properties
  35. // // Let's check 'Image Orientation'
  36. // final exifData = await readExifFromBytes(imageBytes);
  37. // img.Image fixedImage;
  38. // final imgOrientation = exifData['Image Orientation'];
  39. // // rotate
  40. // if (imgOrientation != null &&
  41. // imgOrientation.printable.contains('Horizontal')) {
  42. // fixedImage = img.copyRotate(originalImage, 90);
  43. // } else if (imgOrientation != null &&
  44. // imgOrientation.printable.contains('180')) {
  45. // fixedImage = img.copyRotate(originalImage, -90);
  46. // } else {
  47. // fixedImage = img.copyRotate(originalImage, 0);
  48. // }
  49. // // Here you can select whether you'd like to save it as png
  50. // // or jpg with some compression
  51. // // I choose jpg with 100% quality
  52. // final fixedFile =
  53. // await originalFile.writeAsBytes(img.encodeJpg(fixedImage));
  54. // return fixedFile;
  55. // }
  56. }