CameraProvider.dart 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import 'dart:io';
  2. import 'package:camera/camera.dart';
  3. import 'package:firebase_ml_vision/firebase_ml_vision.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:path/path.dart' as p;
  6. import 'package:path_provider/path_provider.dart';
  7. import 'package:uuid/uuid.dart';
  8. class CameraProvider extends State<StatefulWidget>
  9. with ChangeNotifier, TickerProviderStateMixin {
  10. late CameraController cameraController;
  11. late TabController tabController;
  12. late List<CameraDescription> cameras;
  13. int curCamera = 1;
  14. String appFolder = "";
  15. String fileName = "";
  16. late Widget photoButton;
  17. bool ifMakeVideo = false;
  18. late FaceDetector faceDetector;
  19. CameraProvider() {
  20. tabController = TabController(length: 6, vsync: this);
  21. getCameras();
  22. }
  23. changeFileName(afterFix) {
  24. String id = Uuid().v4().toString();
  25. fileName = p.join(appFolder, '$id.$afterFix');
  26. notifyListeners();
  27. }
  28. captureFrame() {
  29. cameraController.startImageStream((CameraImage image) {
  30. cameraController.stopImageStream();
  31. detectImage(image);
  32. captureFrame();
  33. });
  34. }
  35. detectImage(image) async {
  36. final List<Face> faces = await faceDetector.processImage(
  37. FirebaseVisionImage.fromBytes(image.planes[0].bytes, null));
  38. print(faces);
  39. if (faces.length > 0) {
  40. print(faces[0].headEulerAngleY);
  41. }
  42. }
  43. getCameras() async {
  44. Directory appDocDir = await getApplicationDocumentsDirectory();
  45. if (!Directory(appDocDir.path).existsSync()) {
  46. appDocDir.createSync();
  47. }
  48. appFolder = appDocDir.path;
  49. cameras = await availableCameras();
  50. cameraController =
  51. CameraController(cameras[curCamera], ResolutionPreset.high);
  52. try {
  53. await cameraController.initialize();
  54. } catch (e) {
  55. print(e);
  56. }
  57. // cameraController.startImageStream(onAvailable);
  58. notifyListeners();
  59. // cameraController.initialize().then((_) {
  60. // cameraController.prepareForVideoRecording();
  61. // faceDetector = FirebaseVision.instance.faceDetector();
  62. // // captureFrame();
  63. // notifyListeners();
  64. // });
  65. }
  66. dispose() {
  67. cameraController.dispose();
  68. super.dispose();
  69. }
  70. changeCamera() {
  71. if (curCamera == 0) {
  72. curCamera = 1;
  73. } else {
  74. curCamera = 0;
  75. }
  76. cameraController =
  77. CameraController(cameras[curCamera], ResolutionPreset.max);
  78. cameraController.initialize().then((_) {
  79. notifyListeners();
  80. });
  81. }
  82. changePhotoWidget() {
  83. ifMakeVideo = !ifMakeVideo;
  84. notifyListeners();
  85. }
  86. @override
  87. Widget build(BuildContext context) {
  88. return Container();
  89. }
  90. }