93 lines
2.4 KiB
Dart
93 lines
2.4 KiB
Dart
import 'dart:io';
|
|
import 'package:camera/camera.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
class CameraPageV1 extends StatefulWidget {
|
|
const CameraPageV1({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<CameraPageV1> createState() => _CameraPageState();
|
|
}
|
|
|
|
class _CameraPageState extends State<CameraPageV1> {
|
|
CameraController? controller;
|
|
String imagePath = "";
|
|
List<CameraDescription>? cameras;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
initializeCamera();
|
|
}
|
|
|
|
Future<void> initializeCamera() async {
|
|
// Initialize cameras
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
try {
|
|
cameras = await availableCameras();
|
|
if (cameras != null && cameras!.isNotEmpty) {
|
|
// Use the first available camera
|
|
controller = CameraController(cameras![0], ResolutionPreset.max);
|
|
await controller?.initialize();
|
|
if (mounted) {
|
|
setState(() {});
|
|
}
|
|
}
|
|
} catch (e) {
|
|
print('Error initializing camera: $e');
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
controller?.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
if (controller == null || !controller!.value.isInitialized) {
|
|
return Center(child: CircularProgressIndicator());
|
|
}
|
|
|
|
return Scaffold(
|
|
body: SafeArea(
|
|
child: Center(
|
|
child: Column(
|
|
children: [
|
|
SizedBox(height: 50),
|
|
Container(
|
|
width: 200,
|
|
height: 200,
|
|
child: AspectRatio(
|
|
aspectRatio: controller!.value.aspectRatio,
|
|
child: CameraPreview(controller!),
|
|
),
|
|
),
|
|
TextButton(
|
|
onPressed: () async {
|
|
try {
|
|
final image = await controller!.takePicture();
|
|
setState(() {
|
|
imagePath = image.path;
|
|
});
|
|
} catch (e) {
|
|
print('Error taking picture: $e');
|
|
}
|
|
},
|
|
child: Text("Take Photo"),
|
|
),
|
|
if (imagePath.isNotEmpty)
|
|
Container(
|
|
width: 300,
|
|
height: 300,
|
|
child: Image.file(File(imagePath)),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|