Created
February 9, 2021 11:04
-
-
Save edufolly/43d0c713e0375d4462da8a47ca360e8e to your computer and use it in GitHub Desktop.
My Camera
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:io'; | |
import 'package:camera/camera.dart'; | |
import 'package:path_provider/path_provider.dart'; | |
class MyCamera { | |
/// | |
/// | |
/// | |
static Future<bool> init() async { | |
bool _success = false; | |
CameraController cameraController = await _getController(); | |
if (cameraController != null) { | |
await cameraController.initialize(); | |
_success = true; | |
} | |
await cameraController.dispose(); | |
return _success; | |
} | |
/// | |
/// | |
/// | |
static Future<String> takePicture() async { | |
Directory appDocDir = await getApplicationDocumentsDirectory(); | |
CameraController cameraController = await _getController(); | |
if (cameraController != null) { | |
await cameraController.initialize(); | |
await Future.delayed(Duration(milliseconds: 1000)); | |
String file = | |
'${appDocDir.path}/foto_${DateTime.now().millisecondsSinceEpoch}.jpg'; | |
await cameraController.takePicture(file); | |
return file; | |
} | |
return null; | |
} | |
/// | |
/// | |
/// | |
static Future<CameraController> _getController() async { | |
List<CameraDescription> cameras = await availableCameras(); | |
CameraController cameraController; | |
for (CameraDescription camera in cameras) { | |
if (camera.lensDirection == CameraLensDirection.front && | |
cameraController == null) { | |
cameraController = CameraController(camera, ResolutionPreset.medium); | |
} | |
} | |
return cameraController; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment