Skip to content

Instantly share code, notes, and snippets.

@CalvinGonsalves
Last active January 11, 2021 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CalvinGonsalves/1a8c7a674e3c6647f3e76866bb893076 to your computer and use it in GitHub Desktop.
Save CalvinGonsalves/1a8c7a674e3c6647f3e76866bb893076 to your computer and use it in GitHub Desktop.
HtmlElementView does not display video from webcam after popping the dialog and returning back to access web cam
import 'dart:async';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'dart:html' as html;
import 'dart:ui' as ui;
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Uint8List byteArray;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Container(
child: byteArray != null ? Image.memory(byteArray) : null,
),
),
floatingActionButton: FloatingActionButton(
onPressed: () async {
byteArray = await showDialog<Uint8List>(
context: context,
builder: (BuildContext context) {
return FractionallySizedBox(
heightFactor: 0.5,
widthFactor: 0.5,
child: WebCam(),
);
});
setState(() {});
},
// tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
class WebCam extends StatefulWidget {
@override
_WebCamState createState() => _WebCamState();
}
class _WebCamState extends State<WebCam> {
html.VideoElement _webcamVideoElement;
@override
void initState() {
super.initState();
// Create a video element which will be provided with stream source
_webcamVideoElement = new html.VideoElement();
// Register a webcam
// ignore: undefined_prefixed_name
ui.platformViewRegistry.registerViewFactory(
'webcamVideoElement', (int viewId) => _webcamVideoElement);
// Access the webcam stream
getMedia();
}
getMedia() async {
html.window.navigator.getUserMedia(video: true).then((streamHandle) async {
_webcamVideoElement.srcObject = streamHandle;
await _webcamVideoElement.play();
}).catchError((onError) {
print(onError);
});
}
@override
void dispose() {
if (_webcamVideoElement.srcObject != null &&
_webcamVideoElement.srcObject.active) {
_webcamVideoElement.pause();
var tracks = _webcamVideoElement.srcObject.getTracks();
_webcamVideoElement.srcObject = null;
tracks.forEach((track) {
track.stop();
});
}
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: [
Container(
child: new HtmlElementView(
key: UniqueKey(),
viewType: 'webcamVideoElement',
),
),
// RaisedButton(
// child: Text('Play/Pause'),
// onPressed: () async {
// if (_webcamVideoElement.paused) {
// await getMedia();
// } else {
// print(_webcamVideoElement.srcObject.active);
// _webcamVideoElement.pause();
// var tracks = _webcamVideoElement.srcObject.getTracks();
// _webcamVideoElement.srcObject = null;
// tracks.forEach((track) {
// track.stop();
// });
// }
// },
// ),
Align(
alignment: Alignment.topRight,
child: RaisedButton(
child: Text('Capture'),
onPressed: () async {
_webcamVideoElement.pause();
//drawing canvas and converting to blob
html.CanvasElement canvas = new html.CanvasElement(
width: _webcamVideoElement.videoWidth,
height: _webcamVideoElement.videoHeight);
html.CanvasRenderingContext2D ctx = canvas.context2D;
ctx.drawImage(_webcamVideoElement, 0, 0);
final blob = await canvas.toBlob('image/jpeg', 0.8);
final completer = Completer<Uint8List>();
final reader = html.FileReader();
reader.readAsArrayBuffer(blob);
reader.onLoad.listen((_) => completer.complete(reader.result));
Uint8List byteArray = await completer.future;
//stopping tracks
var tracks = _webcamVideoElement.srcObject.getTracks();
_webcamVideoElement.srcObject = null;
tracks.forEach((track) {
track.stop();
});
Navigator.pop(context, byteArray);
},
),
),
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment