google preperation 1

This commit is contained in:
2026-10-26 20:48:16 +01:00
parent 9a65ff7ad4
commit 3b50cb0fe9
47 changed files with 4969 additions and 4580 deletions
+196 -266
View File
@@ -7,6 +7,7 @@ import 'yolo_detector.dart';
import 'preview_screen.dart';
import '../utils/translations.dart';
import '../utils/target_colors.dart';
import '../widgets/camera_sheet.dart';
class CameraScreen extends StatefulWidget {
const CameraScreen({super.key});
@@ -26,9 +27,10 @@ class _CameraScreenState extends State<CameraScreen> {
String _selectedLanguage = 'English';
late Color _backgroundColor;
// Brightness and contrast controls
double _brightness = 0.0; // -1.0 to 1.0
double _contrast = 1.0; // 0.0 to 2.0
// Real camera controls (they change the photo itself)
double _exposure = 0, _minExposure = 0, _maxExposure = 0;
double _zoom = 1, _minZoom = 1, _maxZoom = 1;
bool _torch = false;
@override
void initState() {
@@ -40,24 +42,9 @@ class _CameraScreenState extends State<CameraScreen> {
_loadBulletColor(); // 👈
_loadLanguage();
_loadSelectedTargetColor();
_loadBrightnessContrast();
YOLODetector.create().then((detector) => _yoloDetector = detector);
}
Future<void> _loadBrightnessContrast() async {
final prefs = await SharedPreferences.getInstance();
setState(() {
_brightness = prefs.getDouble('brightness') ?? 0.0;
_contrast = prefs.getDouble('contrast') ?? 1.0;
});
}
Future<void> _saveBrightnessContrast() async {
final prefs = await SharedPreferences.getInstance();
await prefs.setDouble('brightness', _brightness);
await prefs.setDouble('contrast', _contrast);
}
String _t(String key) {
return AppTranslations.translate(key, _selectedLanguage);
}
@@ -71,63 +58,63 @@ class _CameraScreenState extends State<CameraScreen> {
Future<void> _loadSelectedTargetColor() async {
final prefs = await SharedPreferences.getInstance();
final selectedTarget = prefs.getString('selectedTarget') ?? 'assets/target.png';
final selectedTarget =
prefs.getString('selectedTarget') ?? 'assets/target.png';
setState(() {
_backgroundColor = TargetColors.getBackgroundColor(selectedTarget);
});
}
Future<void> _loadBulletColor() async {
final prefs = await SharedPreferences.getInstance();
final selected = prefs.getString('bulletColor') ?? 'Blue';
setState(() {
_bulletColor = _getColorFromName(selected);
});
}
Color _getColorFromName(String colorName) {
switch (colorName.toLowerCase()) {
case 'blue':
return Colors.blue;
case 'green':
return Colors.green;
case 'yellow':
return Colors.yellow;
case 'purple':
return Colors.purple;
case 'cyan':
return Colors.cyan;
case 'teal':
return Colors.teal;
case 'indigo':
return Colors.indigo;
case 'pink':
return Colors.pink;
case 'lime':
return Colors.lime;
case 'mint':
return Colors.greenAccent;
case 'orange':
return Colors.orange;
case 'violet':
return Colors.deepPurpleAccent;
case 'amber':
return Colors.amber;
case 'brown':
return Colors.brown;
case 'grey':
return Colors.grey;
default:
return Colors.blue; // fallback
final prefs = await SharedPreferences.getInstance();
final selected = prefs.getString('bulletColor') ?? 'Blue';
setState(() {
_bulletColor = _getColorFromName(selected);
});
}
}
Color _getColorFromName(String colorName) {
switch (colorName.toLowerCase()) {
case 'blue':
return Colors.blue;
case 'green':
return Colors.green;
case 'yellow':
return Colors.yellow;
case 'purple':
return Colors.purple;
case 'cyan':
return Colors.cyan;
case 'teal':
return Colors.teal;
case 'indigo':
return Colors.indigo;
case 'pink':
return Colors.pink;
case 'lime':
return Colors.lime;
case 'mint':
return Colors.greenAccent;
case 'orange':
return Colors.orange;
case 'violet':
return Colors.deepPurpleAccent;
case 'amber':
return Colors.amber;
case 'brown':
return Colors.brown;
case 'grey':
return Colors.grey;
default:
return Colors.blue; // fallback
}
}
Future<void> _loadBulletCount() async {
final prefs = await SharedPreferences.getInstance();
setState(() {
_numberOfBullets = prefs.getInt('bulletCount') ?? 5; // Load stored bullet count, default to 5
_numberOfBullets = prefs.getInt('bulletCount') ??
5; // Load stored bullet count, default to 5
});
}
@@ -146,7 +133,13 @@ Color _getColorFromName(String colorName) {
enableAudio: false,
);
_initializeControllerFuture = _controller!.initialize();
_initializeControllerFuture = _controller!.initialize().then((_) async {
// Ranges differ per phone; read them once the camera is ready
_minExposure = await _controller!.getMinExposureOffset();
_maxExposure = await _controller!.getMaxExposureOffset();
_minZoom = await _controller!.getMinZoomLevel();
_maxZoom = (await _controller!.getMaxZoomLevel()).clamp(_minZoom, 8.0);
});
if (mounted) setState(() {});
}
@@ -193,7 +186,7 @@ Color _getColorFromName(String colorName) {
builder: (context) => PreviewScreen(
imagePath: croppedFile.path,
detections: detections,
bulletCount: _numberOfBullets, // Pass the bullet count
bulletCount: _numberOfBullets, // Pass the bullet count
),
),
);
@@ -202,83 +195,40 @@ Color _getColorFromName(String colorName) {
}
}
// Method to show the dialog for selecting the number of bullets
void _showBulletDialog() {
showDialog(
context: context,
builder: (BuildContext context) {
return Dialog(
backgroundColor: Colors.grey[900],
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
child: Padding(
padding: const EdgeInsets.all(24.0), // Increased padding for better spacing
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.only(bottom: 16.0),
child: Text(
_t('Select the number of bullets'),
textAlign: TextAlign.center,
style: const TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
),
Center(
child: Wrap(
alignment: WrapAlignment.center,
spacing: 12,
runSpacing: 12,
children: List.generate(5, (index) {
final bulletNum = index + 1;
final isSelected = _numberOfBullets == bulletNum;
return ElevatedButton(
onPressed: () {
_selectBulletCount(bulletNum);
Navigator.of(context).pop();
},
style: ElevatedButton.styleFrom(
backgroundColor:
isSelected ? _bulletColor : Colors.grey[800],
foregroundColor: Colors.white,
padding:
const EdgeInsets.symmetric(horizontal: 20, vertical: 12),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
child: Text(
'$bulletNum',
style: TextStyle(
fontSize: 18,
fontWeight:
isSelected ? FontWeight.bold : FontWeight.normal,
),
),
);
}),
),
),
],
showCameraSheet(
context,
builder: (sheet) => StatefulBuilder(
builder: (context, setSheetState) => Column(
mainAxisSize: MainAxisSize.min,
children: [
CameraSheetHeader(
title: _t('Select the number of bullets'), subtitle: '1 – 10'),
SheetStepper(
value: _numberOfBullets,
min: 1,
max: 10,
accent: _bulletColor,
onChanged: (n) {
_selectBulletCount(n);
setSheetState(() {});
},
),
),
);
},
const SizedBox(height: 8),
],
),
),
);
}
void _showHelpDialog() {
showDialog(
context: context,
builder: (BuildContext context) {
return Dialog(
backgroundColor: Colors.grey[900],
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
shape:
RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
child: Padding(
padding: const EdgeInsets.all(24.0),
child: Column(
@@ -287,16 +237,23 @@ Color _getColorFromName(String colorName) {
children: [
Text(
_t('Help'),
style: const TextStyle(color: Colors.white, fontSize: 24, fontWeight: FontWeight.bold),
style: const TextStyle(
color: Colors.white,
fontSize: 24,
fontWeight: FontWeight.bold),
),
const SizedBox(height: 20),
_buildHelpItem(Icons.camera_alt, _t('Tap camera button to capture and detect bullets')),
_buildHelpItem(Icons.camera_alt,
_t('Tap camera button to capture and detect bullets')),
const SizedBox(height: 12),
_buildHelpItem(Icons.filter_9_plus, _t('Bottom left number: Set expected bullet count (1-5)')),
_buildHelpItem(Icons.filter_9_plus,
_t('Bottom left number: Set expected bullet count (1-5)')),
const SizedBox(height: 12),
_buildHelpItem(Icons.brightness_6, _t('Sun icon: Adjust brightness and contrast for better visibility')),
_buildHelpItem(Icons.tune,
_t('Settings icon: exposure, zoom and flashlight')),
const SizedBox(height: 12),
_buildHelpItem(Icons.crop_square, _t('Align target inside the square frame')),
_buildHelpItem(Icons.crop_square,
_t('Align target inside the square frame')),
const SizedBox(height: 20),
Center(
child: ElevatedButton(
@@ -332,116 +289,88 @@ Color _getColorFromName(String colorName) {
);
}
void _showBrightnessContrastDialog() {
showDialog(
context: context,
builder: (BuildContext context) {
return StatefulBuilder(
builder: (context, setDialogState) {
return Dialog(
backgroundColor: Colors.grey[900],
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
child: Padding(
padding: const EdgeInsets.all(24.0),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
_t('Brightness & Contrast'),
style: const TextStyle(color: Colors.white, fontSize: 20, fontWeight: FontWeight.bold),
),
const SizedBox(height: 24),
// Brightness slider
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'${_t('Brightness')}: ${(_brightness * 100).toInt()}',
style: const TextStyle(color: Colors.white, fontSize: 16),
),
Slider(
value: _brightness,
min: -1.0,
max: 1.0,
divisions: 40,
activeColor: _bulletColor,
onChanged: (value) {
setState(() {
_brightness = value;
});
setDialogState(() {});
_saveBrightnessContrast();
},
),
],
),
const SizedBox(height: 16),
// Contrast slider
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'${_t('Contrast')}: ${(_contrast * 100).toInt()}%',
style: const TextStyle(color: Colors.white, fontSize: 16),
),
Slider(
value: _contrast,
min: 0.5,
max: 2.0,
divisions: 30,
activeColor: _bulletColor,
onChanged: (value) {
setState(() {
_contrast = value;
});
setDialogState(() {});
_saveBrightnessContrast();
},
),
],
),
const SizedBox(height: 16),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ElevatedButton(
onPressed: () {
setState(() {
_brightness = 0.0;
_contrast = 1.0;
});
setDialogState(() {});
_saveBrightnessContrast();
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.grey[800],
foregroundColor: Colors.white,
),
child: Text(_t('Reset')),
),
ElevatedButton(
onPressed: () {
Navigator.of(context).pop();
},
style: ElevatedButton.styleFrom(
backgroundColor: _bulletColor,
foregroundColor: Colors.white,
),
child: Text(_t('Done')),
),
],
),
],
void _showCameraControls() {
showCameraSheet(
context,
builder: (sheet) => StatefulBuilder(
builder: (context, setSheetState) {
Future<void> apply(
VoidCallback change, Future<void> Function() camera) async {
setState(change);
setSheetState(() {});
try {
await camera();
} catch (_) {} // control not supported on this phone
}
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
CameraSheetHeader(
title: _t('Image Settings'),
trailing: TextButton.icon(
style: TextButton.styleFrom(foregroundColor: Colors.white70),
icon: const Icon(Icons.restart_alt_rounded, size: 18),
label: Text(_t('Reset')),
onPressed: () => apply(() {
_exposure = 0;
_zoom = _minZoom;
}, () async {
await _controller!.setExposureOffset(0);
await _controller!.setZoomLevel(_minZoom);
}),
),
),
);
},
);
},
if (_maxExposure > _minExposure)
SheetSlider(
icon: Icons.exposure_rounded,
label: _t('Exposure'),
valueText:
'${_exposure >= 0 ? '+' : ''}${_exposure.toStringAsFixed(1)}',
value: _exposure,
min: _minExposure,
max: _maxExposure,
accent: _bulletColor,
onChanged: (v) => apply(() => _exposure = v,
() => _controller!.setExposureOffset(v)),
),
if (_maxZoom > _minZoom)
SheetSlider(
icon: Icons.zoom_in_rounded,
label: _t('Zoom'),
valueText: '${_zoom.toStringAsFixed(1)}×',
value: _zoom,
min: _minZoom,
max: _maxZoom,
accent: _bulletColor,
onChanged: (v) => apply(
() => _zoom = v, () => _controller!.setZoomLevel(v)),
),
const SizedBox(height: 8),
SizedBox(
width: double.infinity,
child: SheetToggle(
icon: _torch
? Icons.flashlight_on_rounded
: Icons.flashlight_off_rounded,
label: _t('Flashlight'),
active: _torch,
accent: _bulletColor,
onTap: () => apply(
() => _torch = !_torch,
() => _controller!
.setFlashMode(_torch ? FlashMode.torch : FlashMode.off),
),
),
),
],
);
},
),
);
}
// Update the bullet count and save it
void _selectBulletCount(int number) {
setState(() {
_numberOfBullets = number; // Update the selected number
@@ -519,8 +448,9 @@ Color _getColorFromName(String colorName) {
mainAxisAlignment: MainAxisAlignment.end,
children: [
IconButton(
icon: const Icon(Icons.brightness_6, color: Colors.white, size: 24),
onPressed: _showBrightnessContrastDialog,
icon:
const Icon(Icons.tune, color: Colors.white, size: 24),
onPressed: _showCameraControls,
),
],
),
@@ -540,35 +470,35 @@ Color _getColorFromName(String colorName) {
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
final screenSize = MediaQuery.of(context).size;
return Stack(
children: [
Positioned.fill(
child: Container(
color: Colors.black, // covers any empty space
child: FittedBox(
fit: BoxFit.cover,
child: SizedBox(
width: _controller!.value.previewSize!.height,
height: _controller!.value.previewSize!.width,
child: CameraPreview(_controller!),
return Stack(
children: [
Positioned.fill(
child: Container(
color: Colors.black, // covers any empty space
child: FittedBox(
fit: BoxFit.cover,
child: SizedBox(
width: _controller!.value.previewSize!.height,
height: _controller!.value.previewSize!.width,
child: CameraPreview(_controller!),
),
),
),
),
),
Center(
child: Container(
width: screenSize.width * 0.9,
height: screenSize.width * 0.9,
decoration: BoxDecoration(
border: Border.all(
color: _bulletColor.withValues(alpha: 0.9),
width: 3,
Center(
child: Container(
width: screenSize.width * 0.9,
height: screenSize.width * 0.9,
decoration: BoxDecoration(
border: Border.all(
color: _bulletColor.withValues(alpha: 0.9),
width: 3,
),
),
),
),
),
],
);
],
);
} else {
return const Center(child: CircularProgressIndicator());
}