CameraProvider.dart 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import 'dart:io';
  2. import 'package:flutter/material.dart';
  3. import 'package:camera/camera.dart';
  4. import 'package:path/path.dart' as p;
  5. import 'package:path_provider/path_provider.dart';
  6. import 'package:uuid/uuid.dart';
  7. class CameraProvider with ChangeNotifier {
  8. CameraController cameraController;
  9. List<CameraDescription> cameras;
  10. int curCamera = 0;
  11. String appFolder = "";
  12. String fileName;
  13. CameraProvider() {
  14. getCameras();
  15. }
  16. changeFileName(){
  17. String id=Uuid().v4().toString();
  18. fileName=p.join(appFolder,'$id.png');
  19. notifyListeners();
  20. }
  21. getCameras() async {
  22. Directory appDocDir = await getApplicationDocumentsDirectory();
  23. if(!Directory(appDocDir.path).existsSync()){
  24. appDocDir.createSync();
  25. }
  26. appFolder = appDocDir.path;
  27. cameras = await availableCameras();
  28. cameraController =
  29. CameraController(cameras[curCamera], ResolutionPreset.max);
  30. cameraController.initialize().then((_) {
  31. notifyListeners();
  32. });
  33. }
  34. changeCamera() {
  35. if (curCamera == 0) {
  36. curCamera = 1;
  37. } else {
  38. curCamera = 0;
  39. }
  40. cameraController =
  41. CameraController(cameras[curCamera], ResolutionPreset.max);
  42. cameraController.initialize().then((_) {
  43. notifyListeners();
  44. });
  45. }
  46. }