Created
December 25, 2019 19:43
-
-
Save mikepyts/ab2ee5e5d505becef3871e8a5c960952 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// ignore: avoid_web_libraries_in_flutter | |
import 'dart:html'; | |
import 'dart:ui' as ui; | |
import 'package:flutter/material.dart'; | |
void main() => runApp(WebcamApp()); | |
class WebcamApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) => MaterialApp( | |
home: WebcamPage(), | |
); | |
} | |
class WebcamPage extends StatefulWidget { | |
@override | |
_WebcamPageState createState() => _WebcamPageState(); | |
} | |
class _WebcamPageState extends State<WebcamPage> { | |
// Webcam widget to insert into the tree | |
Widget _webcamWidget; | |
// VideoElement | |
VideoElement _webcamVideoElement; | |
@override | |
void initState() { | |
super.initState(); | |
// Create an video element which will be provided with stream source | |
_webcamVideoElement = VideoElement(); | |
// Register an webcam | |
ui.platformViewRegistry.registerViewFactory('webcamVideoElement', (int viewId) => _webcamVideoElement); | |
// Create video widget | |
_webcamWidget = HtmlElementView(key: UniqueKey(), viewType: 'webcamVideoElement'); | |
// Access the webcam stream | |
window.navigator.getUserMedia(video: true).then((MediaStream stream) { | |
_webcamVideoElement.srcObject = stream; | |
}); | |
} | |
@override | |
Widget build(BuildContext context) => Scaffold( | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text( | |
'Webcam MediaStream:', | |
style: TextStyle(fontSize: 30, fontStyle: FontStyle.italic), | |
), | |
Container(width: 750, height: 750, child: _webcamWidget), | |
], | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: () => _webcamVideoElement.srcObject.active ? _webcamVideoElement.play() : _webcamVideoElement.pause(), | |
tooltip: 'Start stream, stop stream', | |
child: Icon(Icons.camera_alt), | |
), | |
); | |
} |
hey brother, how i take a photo with webcam?
hey brother, how i take a photo with webcam?
Did you succed ?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Will you please know me how to close camera and reinit with navigation back and come same screen?