Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// 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

This comment has been minimized.

Copy link

@TejaDroid TejaDroid commented Oct 23, 2020

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

@lumuniz

This comment has been minimized.

Copy link

@lumuniz lumuniz commented Feb 2, 2021

hey brother, how i take a photo with webcam?

@danhab05

This comment has been minimized.

Copy link

@danhab05 danhab05 commented Jul 29, 2021

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