69 lines
2.0 KiB
Dart
69 lines
2.0 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:camera/camera.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
final imgPhotoWebProvider = StateProvider<String?>((ref) => "");
|
|
|
|
final cameraControllerProvider =
|
|
StateNotifierProvider<CameraControllerNotifier, CameraController?>((ref) {
|
|
return CameraControllerNotifier();
|
|
});
|
|
|
|
// StateNotifier to manage CameraController
|
|
class CameraControllerNotifier extends StateNotifier<CameraController?> {
|
|
CameraControllerNotifier() : super(null) {
|
|
_initializeCamera();
|
|
}
|
|
|
|
Future<void> _initializeCamera() async {
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
try {
|
|
final cameras = await availableCameras();
|
|
if (cameras.isNotEmpty) {
|
|
final controller = CameraController(cameras[0], ResolutionPreset.max);
|
|
await controller.initialize();
|
|
state = controller;
|
|
}
|
|
} catch (e) {
|
|
print('Error initializing camera: $e');
|
|
}
|
|
}
|
|
|
|
Future<void> takePicture(
|
|
// ValueNotifier<XFile?> imageFile,
|
|
ValueNotifier<String> base64ImageString,
|
|
ValueNotifier<String> imagePath,
|
|
) async {
|
|
// final flag = ValueNotifier<bool>(false);
|
|
if (state != null) {
|
|
try {
|
|
final image = await state!.takePicture();
|
|
final bytes = await image.readAsBytes();
|
|
String base64Image = base64Encode(bytes);
|
|
|
|
// imageFile.value = image;
|
|
base64ImageString.value = base64Image;
|
|
imagePath.value = image.path;
|
|
|
|
// final shared = await SharedPreferences.getInstance();
|
|
// shared.setString("base64Image", base64Image);
|
|
} catch (e) {
|
|
print('Error taking picture: $e');
|
|
// flag.value = false;
|
|
// RespErr(flag: false, message: 'Error taking picture: $e');
|
|
}
|
|
} else {
|
|
print('CameraController is not initialized.');
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
state?.dispose();
|
|
super.dispose();
|
|
}
|
|
}
|