wemersonrv

Camera orientation

Nov 30th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 4.35 KB | None | 0 0
  1. import 'package:flutter/foundation.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:camera/camera.dart';
  4. import 'package:flutter/services.dart';
  5.  
  6. class CameraView extends StatefulWidget {
  7.   @override
  8.   _CameraViewState createState() => _CameraViewState();
  9. }
  10.  
  11. class _CameraViewState extends State<CameraView> {
  12.   CameraController _cameraController;
  13.   bool _isLoading;
  14.  
  15.   void _initializeCamera() async {
  16.     setState(() {
  17.       _isLoading = true;
  18.     });
  19.  
  20.     final List<CameraDescription> cameras = await availableCameras();
  21.     final CameraDescription description = cameras.firstWhere(
  22.         (camera) => camera.lensDirection == CameraLensDirection.back);
  23.  
  24.     _cameraController = CameraController(
  25.       description,
  26.       defaultTargetPlatform == TargetPlatform.iOS
  27.           ? ResolutionPreset.low
  28.           : ResolutionPreset.medium,
  29.       enableAudio: false,
  30.     );
  31.  
  32.     await _cameraController.initialize();
  33.     setState(() {
  34.       _isLoading = false;
  35.     });
  36.   }
  37.  
  38.   @override
  39.   void initState() {
  40.     super.initState();
  41.  
  42.     SystemChrome.setEnabledSystemUIOverlays(<SystemUiOverlay>[]);
  43.     SystemChrome.setPreferredOrientations(
  44.       <DeviceOrientation>[DeviceOrientation.landscapeLeft],
  45.     );
  46.  
  47.     _initializeCamera();
  48.   }
  49.  
  50.   @override
  51.   void dispose() {
  52.     _cameraController.dispose().then((_) {
  53.       // Caso precise desativare alguma coisa interno na camera
  54.     });
  55.  
  56.     SystemChrome.setPreferredOrientations(<DeviceOrientation>[]);
  57.     SystemChrome.setEnabledSystemUIOverlays(<SystemUiOverlay>[
  58.       SystemUiOverlay.top,
  59.       SystemUiOverlay.bottom,
  60.     ]);
  61.  
  62.     super.dispose();
  63.   }
  64.  
  65.   @override
  66.   Widget build(BuildContext context) {
  67.     final Size querySizes = MediaQuery.of(context).size;
  68.     final double captureHeight = 144;
  69.     final double vSpacing = (querySizes.height - captureHeight) / 2;
  70.     final double hSpacing = 30;
  71.     final Color overlayColor = Colors.green.withOpacity(0.3);
  72.  
  73.     return Material(
  74.       child: _isLoading
  75.           ? Center(
  76.               child: Text(
  77.                 "Iniciando Câmera...",
  78.                 style: TextStyle(
  79.                   color: Colors.green,
  80.                   fontSize: 30,
  81.                 ),
  82.               ),
  83.             )
  84.           : Stack(
  85.               alignment: FractionalOffset.center,
  86.               children: <Widget>[
  87.                 Positioned.fill(
  88.                   child: AspectRatio(
  89.                     aspectRatio: _cameraController.value.aspectRatio,
  90.                     child: CameraPreview(_cameraController),
  91.                   ),
  92.                 ),
  93.                 Column(
  94.                   children: <Widget>[
  95.                     Container(
  96.                       height: vSpacing,
  97.                       width: double.infinity,
  98.                       color: overlayColor,
  99.                     ),
  100.                     Row(
  101.                       mainAxisAlignment: MainAxisAlignment.center,
  102.                       children: <Widget>[
  103.                         Container(
  104.                           width: hSpacing,
  105.                           height: captureHeight,
  106.                           color: overlayColor,
  107.                         ),
  108.                         Expanded(
  109.                           child: Container(
  110.                             alignment: Alignment.center,
  111.                             height: captureHeight,
  112.                             decoration: BoxDecoration(
  113.                               border: Border.all(
  114.                                 width: 2,
  115.                                 color: Colors.white.withGreen(255),
  116.                               ),
  117.                               // borderRadius: BorderRadius.circular(6),
  118.                               color: Colors.white.withOpacity(0),
  119.                             ),
  120.                           ),
  121.                         ),
  122.                         Container(
  123.                           width: hSpacing,
  124.                           height: captureHeight,
  125.                           color: overlayColor,
  126.                         ),
  127.                       ],
  128.                     ),
  129.                     Container(
  130.                       height: vSpacing,
  131.                       width: double.infinity,
  132.                       color: overlayColor,
  133.                     ),
  134.                   ],
  135.                 ),
  136.               ],
  137.             ),
  138.     );
  139.   }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment