157 lines
5.0 KiB
Dart
157 lines
5.0 KiB
Dart
import 'dart:async';
|
|
import 'dart:io';
|
|
|
|
import 'package:image_picker/image_picker.dart';
|
|
import '../classes/MyDrawer.dart';
|
|
|
|
import 'DisplayPictureScreen.dart';
|
|
import 'package:camera/camera.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
Future<void> main() async {
|
|
// Ensure that plugin services are initialized so that `availableCameras()`
|
|
// can be called before `runApp()`
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
|
|
// Obtain a list of the available cameras on the device.
|
|
final cameras = await availableCameras();
|
|
|
|
// Get a specific camera from the list of available cameras.
|
|
final firstCamera = cameras.first;
|
|
|
|
runApp(
|
|
MaterialApp(
|
|
theme: ThemeData.dark(),
|
|
home: Camera(
|
|
// Pass the appropriate camera to the TakePictureScreen widget.
|
|
camera: firstCamera,
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
// A screen that allows users to take a picture using a given camera.
|
|
class Camera extends StatefulWidget {
|
|
const Camera({
|
|
super.key,
|
|
required this.camera,
|
|
});
|
|
|
|
final CameraDescription camera;
|
|
|
|
@override
|
|
CameraState createState() => CameraState();
|
|
}
|
|
|
|
class CameraState extends State<Camera> {
|
|
late CameraController _controller;
|
|
late Future<void> _initializeControllerFuture;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
// To display the current output from the Camera,
|
|
// create a CameraController.
|
|
|
|
_controller = CameraController(
|
|
// Get a specific camera from the list of available cameras.
|
|
widget.camera,
|
|
// Define the resolution to use.
|
|
ResolutionPreset.medium,
|
|
);
|
|
|
|
// Next, initialize the controller. This returns a Future.
|
|
_initializeControllerFuture = _controller.initialize();
|
|
}
|
|
|
|
Future<void> pickImage() async {
|
|
final imagePicker = ImagePicker();
|
|
final pickedFile = await imagePicker.pickImage(source: ImageSource.gallery);
|
|
if (pickedFile != null) {
|
|
await Navigator.of(context).push(
|
|
MaterialPageRoute(
|
|
builder: (context) => DisplayPictureScreen(
|
|
// Pass the automatically generated path to
|
|
// the DisplayPictureScreen widget.
|
|
imagePath: pickedFile.path,
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
// Dispose of the controller when the widget is disposed.
|
|
_controller.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(title: const Text('Take a picture')),
|
|
// You must wait until the controller is initialized before displaying the
|
|
// camera preview. Use a FutureBuilder to display a loading spinner until the
|
|
// controller has finished initializing.
|
|
drawer: MyDrawer(),
|
|
|
|
body: FutureBuilder<void>(
|
|
future: _initializeControllerFuture,
|
|
builder: (context, snapshot) {
|
|
if (snapshot.connectionState == ConnectionState.done) {
|
|
// If the Future is complete, display the preview.
|
|
return CameraPreview(_controller);
|
|
} else {
|
|
// Otherwise, display a loading indicator.
|
|
return const Center(child: CircularProgressIndicator());
|
|
}
|
|
},
|
|
),
|
|
floatingActionButton: Padding(
|
|
padding: const EdgeInsets.all(8.0),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: <Widget>[
|
|
FloatingActionButton(
|
|
onPressed: pickImage,
|
|
child: Icon(Icons.photo_library),
|
|
),
|
|
SizedBox(width: 40),
|
|
FloatingActionButton(
|
|
// Provide an onPressed callback.
|
|
onPressed: () async {
|
|
// Take the Picture in a try / catch block. If anything goes wrong,
|
|
// catch the error.
|
|
try {
|
|
// Ensure that the camera is initialized.
|
|
await _initializeControllerFuture;
|
|
|
|
// Attempt to take a picture and get the file `image`
|
|
// where it was saved.
|
|
final image = await _controller.takePicture();
|
|
|
|
if (!context.mounted) return;
|
|
|
|
// If the picture was taken, display it on a new screen.
|
|
await Navigator.of(context).push(
|
|
MaterialPageRoute(
|
|
builder: (context) => DisplayPictureScreen(
|
|
// Pass the automatically generated path to
|
|
// the DisplayPictureScreen widget.
|
|
imagePath: image.path,
|
|
),
|
|
),
|
|
);
|
|
} catch (e) {
|
|
// If an error occurs, log the error to the console.
|
|
print(e);
|
|
}
|
|
},
|
|
child: const Icon(Icons.camera_alt),
|
|
)
|
|
],
|
|
)));
|
|
}
|
|
}
|