first commit
This commit is contained in:
61
lib/core/camera/camera_service.dart
Normal file
61
lib/core/camera/camera_service.dart
Normal file
@@ -0,0 +1,61 @@
|
||||
import 'package:camera/camera.dart';
|
||||
|
||||
class CameraService {
|
||||
CameraController? controller;
|
||||
late List<CameraDescription> _cameras;
|
||||
bool _isFront = true;
|
||||
|
||||
bool get isFront => _isFront;
|
||||
|
||||
/// Init default
|
||||
Future<void> init() async {
|
||||
_cameras = await availableCameras();
|
||||
|
||||
final front = _cameras.firstWhere(
|
||||
(c) => c.lensDirection == CameraLensDirection.front,
|
||||
orElse: () => _cameras.first,
|
||||
);
|
||||
|
||||
controller = CameraController(
|
||||
front,
|
||||
ResolutionPreset.high,
|
||||
enableAudio: false,
|
||||
);
|
||||
|
||||
await controller!.initialize();
|
||||
_isFront = true;
|
||||
}
|
||||
|
||||
/// Switch front <-> back
|
||||
Future<void> switchCamera() async {
|
||||
if (_cameras.length < 2) return;
|
||||
|
||||
final newCam = _cameras.firstWhere(
|
||||
(c) =>
|
||||
c.lensDirection ==
|
||||
(_isFront ? CameraLensDirection.back : CameraLensDirection.front),
|
||||
);
|
||||
|
||||
await controller?.dispose();
|
||||
|
||||
controller = CameraController(
|
||||
newCam,
|
||||
ResolutionPreset.high,
|
||||
enableAudio: false,
|
||||
);
|
||||
|
||||
await controller!.initialize();
|
||||
_isFront = !_isFront;
|
||||
}
|
||||
|
||||
Future<XFile> takePicture() async {
|
||||
if (controller == null || !controller!.value.isInitialized) {
|
||||
throw Exception('Camera not ready');
|
||||
}
|
||||
return controller!.takePicture();
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
controller?.dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user