Created
September 7, 2019 00:02
-
-
Save roipeker/fc633084730c29f4e32045fb8fcbb21b to your computer and use it in GitHub Desktop.
very quick flutter test, to draw a frame in foreground of the camera.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:camera/camera.dart'; | |
import 'package:flutter/material.dart'; | |
class CameraFrameTest extends StatefulWidget { | |
@override | |
_CameraFrameTestState createState() => _CameraFrameTestState(); | |
} | |
class _CameraFrameTestState extends State<CameraFrameTest> { | |
CameraDescription firstCamera; | |
CameraController _controller; | |
bool cameraLoaded = false; | |
@override | |
void initState() { | |
requestCameras(); | |
super.initState(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
backgroundColor: Colors.white, | |
appBar: AppBar( | |
elevation: 1, | |
backgroundColor: Colors.grey, | |
title: Row( | |
children: <Widget>[ | |
Icon(Icons.camera_alt), | |
SizedBox(width: 5), | |
Text("Camera frame"), | |
], | |
), | |
), | |
body: getCameraPreview(), | |
); | |
} | |
final ValueNotifier<bool> onCameraReady = ValueNotifier(false); | |
getCameraPreview() { | |
return Center( | |
child: ValueListenableBuilder<bool>( | |
valueListenable: onCameraReady, | |
builder: (BuildContext ctx, bool value, Widget _) { | |
print("Called event $value"); | |
return Container( | |
decoration: BoxDecoration( | |
color: value ? Colors.grey : Colors.red, | |
), | |
child: value | |
? getCameraView() | |
: Center( | |
child: CircularProgressIndicator(), | |
), | |
); | |
}, | |
), | |
); | |
} | |
Future requestCameras() async { | |
final cameras = await availableCameras(); | |
if (cameras.isEmpty) { | |
onCameraReady.value = false; | |
return; | |
} | |
firstCamera = cameras.first; | |
_controller = CameraController( | |
firstCamera, | |
ResolutionPreset.high, | |
enableAudio: false, | |
); | |
await _controller.initialize(); | |
onCameraReady.value = true; | |
} | |
getCameraView() { | |
return Stack( | |
children: [ | |
Center( | |
child: AspectRatio( | |
aspectRatio: _controller.value.aspectRatio, | |
child: Transform.scale(scale: 1.5, child: CameraPreview(_controller)), | |
), | |
), | |
ClipPath( | |
clipper: MyCustomClipper(), | |
child: Container( | |
color: Colors.black.withOpacity(.7), | |
), | |
) | |
], | |
); | |
} | |
} | |
class MyCustomClipper extends CustomClipper<Path> { | |
@override | |
Path getClip(Size size) { | |
final center = Offset(size.width / 2, size.height / 2); | |
final innerRect = Rect.fromCircle(center: center, radius: size.width * .45); | |
final roundRect = RRect.fromRectAndRadius(innerRect, Radius.circular(20)); | |
return Path() | |
..addRRect(roundRect) | |
..addOval(Rect.fromCircle(center: Offset(60, 60), radius: 20)) // quasi emoji :P | |
..addOval(Rect.fromCircle(center: Offset(size.width - 60, 60), radius: 20)) | |
..addRect(Rect.fromLTWH(0.0, 0.0, size.width, size.height)) | |
..fillType = PathFillType.evenOdd; | |
} | |
@override | |
bool shouldReclip(CustomClipper<Path> oldClipper) => true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment