使用Dart访问网络摄像头

3

有没有办法用Dart访问计算机上的网络摄像头。我已经尝试过文档和博客,但似乎没有人在此之前提出过这个问题。

谢谢, 肯尼斯

3个回答

4

访问摄像头并不一定是一个特定于Dart的问题,而是是否有相应的HTML5 API可以通过Dart进行访问。

MediaStreamLocalMediaStream可能是您正在寻找的内容。


0

我认为这可能有助于使用Flutter/Dart访问网络摄像头

import 'package:flutter/material.dart';
import 'dart:html';
import 'dart:ui' as ui;

class WebCam extends StatefulWidget {
  const WebCam({Key? key}) : super(key: key);

  @override
  _WebCamState createState() => _WebCamState();
}

class _WebCamState extends State<WebCam> {
  // Webcam widget to insert into the tree
  late Widget _webcamWidget;
  // VideoElement
  late VideoElement _webcamVideoElement;
  @override
  void initState() {
    super.initState();
    // Create a video element which will be provided with stream source
    _webcamVideoElement = VideoElement();
    // Register an webcam
    // ignore: undefined_prefixed_name
    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) {
    return Scaffold(
      body: SingleChildScrollView(
        child: 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: () {
          print(_webcamVideoElement.srcObject!.active!);
          if (_webcamVideoElement.srcObject!.active!) {
            _webcamVideoElement.play();
          } else {
            _webcamVideoElement.pause();
          }
        },
        tooltip: 'Start stream, stop stream',
        child: Icon(Icons.play_circle),
      ),
    );
  }
}

0

我尝试了以下方法,但没有成功:

VideoElement webcamVideo = query('#webcamVideo');
window.navigator.webkitGetUserMedia({'video': true}, (e) { 
    print(e);
    webcamVideo.src = new DOMURL().createObjectURL(e);
});

问题在于,在Dart SDK版本10311中,createObjectURL()在DOMURL接口中明显缺失,尽管它在_DOMURLImpl中可用(编辑:作为静态方法)。

谢谢,请给这个 bug 点个赞:http://code.google.com/p/dart/issues/detail?id=4231 - Seth Ladd

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接