Advertisement
AnoTest

mainCameraTakingPicture

Jan 7th, 2022
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.70 KB | None | 0 0
  1. import 'dart:async';
  2. import 'dart:io';
  3.  
  4. import 'package:camera/camera.dart';
  5. import 'package:flutter/material.dart';
  6.  
  7. Future<void> main() async {
  8.   WidgetsFlutterBinding.ensureInitialized();
  9.  
  10.   final cameras = await availableCameras();
  11.  
  12.   final firstCamera = cameras.first;
  13.  
  14.   runApp(
  15.     MaterialApp(
  16.       theme: ThemeData.dark(),
  17.       home: TakePictureScreen(
  18.         camera: firstCamera,
  19.       ),
  20.     ),
  21.   );
  22. }
  23.  
  24. // A screen that allows users to take a picture using a given camera.
  25. class TakePictureScreen extends StatefulWidget {
  26.   const TakePictureScreen({
  27.     Key? key,
  28.     required this.camera,
  29.   }) : super(key: key);
  30.  
  31.   final CameraDescription camera;
  32.  
  33.   @override
  34.   TakePictureScreenState createState() => TakePictureScreenState();
  35. }
  36.  
  37. class TakePictureScreenState extends State<TakePictureScreen> {
  38.   late CameraController _controller;
  39.   late Future<void> _initializeControllerFuture;
  40.  
  41.   @override
  42.   void initState() {
  43.     super.initState();
  44.  
  45.     _controller = CameraController(
  46.       widget.camera,
  47.       ResolutionPreset.medium,
  48.     );
  49.  
  50.     _initializeControllerFuture = _controller.initialize();
  51.   }
  52.  
  53.   @override
  54.   void dispose() {
  55.     _controller.dispose();
  56.     super.dispose();
  57.   }
  58.  
  59.   @override
  60.   Widget build(BuildContext context) {
  61.     return Scaffold(
  62.       appBar: AppBar(title: const Text('Take a picture')),
  63.       body: FutureBuilder<void>(
  64.         future: _initializeControllerFuture,
  65.         builder: (context, snapshot) {
  66.           if (snapshot.connectionState == ConnectionState.done) {
  67.             return CameraPreview(_controller);
  68.           } else {
  69.             return const Center(child: CircularProgressIndicator());
  70.           }
  71.         },
  72.       ),
  73.       floatingActionButton: FloatingActionButton(
  74.         onPressed: () async {
  75.           try {
  76.             await _initializeControllerFuture;
  77.  
  78.             final image = await _controller.takePicture();
  79.  
  80.             await Navigator.of(context).push(
  81.               MaterialPageRoute(
  82.                 builder: (context) =>
  83.                     DisplayPictureScreen(imagePath: image.path),
  84.               ),
  85.             );
  86.           } catch (e) {
  87.             print(e);
  88.           }
  89.         },
  90.         child: const Icon(Icons.camera_alt),
  91.       ),
  92.     );
  93.   }
  94. }
  95.  
  96. // A widget that displays the picture taken by the user.
  97. class DisplayPictureScreen extends StatelessWidget {
  98.   final String imagePath;
  99.  
  100.   const DisplayPictureScreen({Key? key, required this.imagePath})
  101.       : super(key: key);
  102.  
  103.   @override
  104.   Widget build(BuildContext context) {
  105.     return Scaffold(
  106.       appBar: AppBar(title: const Text('Display the Picture')),
  107.       body: Image.file(File(imagePath)),
  108.     );
  109.   }
  110. }
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement