Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mikepyts/ab2ee5e5d505becef3871e8a5c960952 to your computer and use it in GitHub Desktop.
Save mikepyts/ab2ee5e5d505becef3871e8a5c960952 to your computer and use it in GitHub Desktop.
// 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),
),
);
}
@TejaDroid
Copy link

Will you please know me how to close camera and reinit with navigation back and come same screen?

@lumuniz
Copy link

lumuniz commented Feb 2, 2021

hey brother, how i take a photo with webcam?

@danhab05
Copy link

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