CameraProvider.dart 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import 'dart:io';
  2. import 'package:firebase_ml_vision/firebase_ml_vision.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:camera/camera.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. CameraController cameraController;
  11. TabController tabController;
  12. List<CameraDescription> cameras;
  13. int curCamera = 1;
  14. String appFolder = "";
  15. String fileName;
  16. Widget photoButton;
  17. bool ifMakeVideo = false;
  18. 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.low);
  52. // cameraController.initialize().then((_) {
  53. // cameraController.prepareForVideoRecording();
  54. // faceDetector = FirebaseVision.instance.faceDetector();
  55. // // captureFrame();
  56. // notifyListeners();
  57. // });
  58. }
  59. dispose() {
  60. cameraController.dispose();
  61. super.dispose();
  62. }
  63. changeCamera() {
  64. if (curCamera == 0) {
  65. curCamera = 1;
  66. } else {
  67. curCamera = 0;
  68. }
  69. cameraController =
  70. CameraController(cameras[curCamera], ResolutionPreset.max);
  71. cameraController.initialize().then((_) {
  72. notifyListeners();
  73. });
  74. }
  75. changePhotoWidget() {
  76. ifMakeVideo = !ifMakeVideo;
  77. notifyListeners();
  78. }
  79. @override
  80. Widget build(BuildContext context) {
  81. // TODO: implement build
  82. return null;
  83. }
  84. }