html5选择摄像头 android - Enable rear camera with HTML5 - Stack Overflow

标签: | 发表时间:2019-03-03 11:20 | 作者:
出处:https://stackoverflow.com

Check out  https://simpl.info/getusermedia/sources/ that shows how you can select sources using

   MediaStreamTrack.getSources(gotSources);

You can then select the source and pass it in as optional into getUserMedia

   var constraints = {
  audio: {
    optional: [{sourceId: audioSource}]
  },
  video: {
    optional: [{sourceId: videoSource}]
  }
};
navigator.getUserMedia(constraints, successCallback, errorCallback);

It is now fully available in Stable Chrome and mobile (As of v30)

详见源码:

https://github.com/samdutton/simpl/blob/gh-pages/getusermedia/sources/js/main.js

html:

<div id="container">

<h1><a href=" ../../index.html" title="simpl.info home page">simpl.info</a> MediaStreamTrack.getSources</h1>

<div class="select">
<label for="audioSource">Audio source: </label><select id="audioSource"></select>
</div>

<div class="select">
<label for="videoSource">Video source: </label><select id="videoSource"></select>
</div>

<video muted autoplay></video>

<script src=" js/main.js"></script>

<p>This demo requires Chrome 30 or later.</p>

<p>For more information, see <a href=" https://www.html5rocks.com/en/tutorials/getusermedia/intro/" title="Media capture article by Eric Bidelman on HTML5 Rocks">Capturing Audio &amp; Video in HTML5</a> on HTML5 Rocks.</p>

<a href=" https://github.com/samdutton/simpl/blob/gh-pages/getusermedia/sources/js/main.js" title="View source for this page on GitHub" id="viewSource">View source on GitHub</a>

</div>


Last time i developped that code, soo here is the version which i use : you call directly the function whichCamera in your code and you specefic which camera "user","environment" or "computer"'if you are runing in a computer)

      `//----------------------------------------------------------------------
//  whichCamera(Type)
//    For smartphone or tablet :
//     Start the type={user,environment} camera.
//    For computer it's simple :
//      type = "computer".
//----------------------------------------------------------------------
var streamSrc, cameraType;
function whichCamera(type){

  var cameraFacing;
  cameraType = type;
  if( type == "user")
    cameraFacing = 0;
  else if( type == "environment")
    cameraFacing = 1;
  else if( type == "computer"){
    cameraFacing = 2;
  }
  console.log(type+" index : "+cameraFacing);

  //  Here we list all media devices, in order to choose between
  //  the front and the rear camera.
  //      videoDevices[0] : user Camera
  //      videoDevices[1] : environment Camera
  //  Then set the video resolution.
  navigator.mediaDevices.enumerateDevices()
  .then(devices => {
    var videoDevices, videoDeviceIndex, constraints;
    //  Initialize the array wich will contain all video resources IDs.
    //  Most of devices have two video resources (Front & Rear Camera).
    videoDevices = [0,0];
    //  Simple index to browse the videa resources array (videoDevices).
    videoDeviceIndex = 0;
    //  devices.forEach(), this function will detect all media resources (Audio, Video) of the device
    //  where we run the application.
    devices.forEach(function(device) {
      console.log(device.kind + ": " + device.label +
        " id = " + device.deviceId);
      // If the kind of the media resource is video,
      if (device.kind == "videoinput") {
        //  then we save it on the array videoDevices.
        videoDevices[videoDeviceIndex++] =  device.deviceId;
        console.log(device.deviceId+" = "+videoDevices[videoDeviceIndex-1]);
      }
    });
    console.log("Camera facing ="+cameraFacing+" ID = "+videoDevices[videoDeviceIndex-1]);

    // Here we specified which camera we start,
    //  videoDevices[0] : Front Camera
    //  videoDevices[1] : Back Camera
    if( cameraFacing != "computer"){
      constraints = { deviceId: { exact: videoDevices[cameraFacing]  }};
      return navigator.mediaDevices.getUserMedia({ video:
                                                          constraints,
                                                          width: { min: 1280, ideal: 1600, max: 1920 },
                                                          height: { min: 720, ideal: 1200, max: 1080 }
                                                  }
                                                );
    }else
      return navigator.mediaDevices.getUserMedia({ video: true });
    })
    //  Then we retrieve the link to the video stream.
    .then(stream => {
      if (window.webkitURL) {
        video.src = window.webkitURL.createObjectURL(stream);
        localMediaStream = stream;
        console.log(localMediaStream +" = "+ stream)
      } else if (video.mozSrcObject !== undefined) {
        video.mozSrcObject = stream;
        console.log(video.mozSrcObject +" = "+ stream)
      } else if (video.srcObject !== undefined) {
        video.srcObject = stream;
        console.log(video.srcObject +" = "+ stream)
      } else {
        video.src = stream;
        console.log(video.src +" = "+ stream)
      }
      streamSrc = stream;
    })
    .catch(e => console.error(e));

}

相关 [html5 选择 摄像头] 推荐:

html5选择摄像头 android - Enable rear camera with HTML5 - Stack Overflow

- -

HTML5调用android手机摄像头拍照

- - 移动开发 - ITeye博客
HTML5 The Media Capture API提供了对摄像头的可编程访问,用户可以直接用getUserMedia获得摄像头提供的视频流. 但实际上用html5调用手机摄像头存在很多问题:. 1)谷歌的发布的Chrome到了21版本后,才新增了一个用于高质量视频音频通讯的getUserMedia API,该API允许Web应用程序访问摄像头和麦克风,其他手机浏览器只有opera支持html5调用本地拍照功能.

使用HTML5技术控制电脑或手机上的摄像头

- - WebHek
移动设备和桌面电脑上的客户端API起初并不是同步的. 最初总是移动设备上先拥有某些功能和相应的API,但慢慢的,这些API会出现在桌面电脑上. 其中一个应用接口技术就是 getUserMedia API,它能让应用开发者访问用户的摄像头或内置相机. 下面就让我展示一下如何通过浏览器来访问你的摄像头,并提取截屏图形.

HTML5:新闻网站的新选择

- - HTML5研究小组
HTML5是HTML语言的更新版,被业内公认为是近十年来 Web 标准最大的飞跃,将成为下一代的Web语言. 随着新技术的发展,移动阅读已经成为一种不可逆转的趋势. 智能手机、平板电脑则上升为人们接触媒体、阅读新闻的首选. 在这样的背景下,新闻网站面临两难选择:. A选项——继续把原有网站做好,吸引用户浏览.

HTML5和Flash - 如何选择合适的工具

- ashuai - cnBeta.COM
Adobe系统公司日前透露了一些旗舰Flash平台的信息:能让开发人员同时使用HTML5建立丰富的应用. Adobe官方想澄清一件事:该公司并没有放弃Flash For HTML5,也不是把其中一个领先于其他. 事实上,在洛杉矶的Adobe MAX 2011开发者大会第二日上强调,该公司制定了在浏览器和应用程序的几个实例,体现Adobe Flash和HTML5如何携手合作,提供极富表现力的体验.

列举HTML5游戏最易创收的选择渠道

- - GamerBoom.com 游戏邦
上篇有关创收主题的文章发布自今已有6个月. 就HTML5游戏的迅猛发展速度而言,现在是时候该撰写新内容. 注意,本文瞄准的是最容易创收的方式. * Google Adsense——浮动利率,但这主要取决于游戏页面的内容. 尝试插入HTML5相关的关键字,因为不少公司在HTML5相关的广告上投入更多资金.

Web VS Native–LinkedIn做出的选择:iPad App95%使用HTML5开发

- - Web App Trend
Jolie O’Dell 是VentureBeat的记者. LinkedIn iPad版本应用其中的95%使用HTML5开发,这在业内激起千层浪,下面是VentureBeat记者对LinkedIn移动开发团队主管Kiran Prasad进行的采访. 首先,我们请读者们先试着猜一下LinkedIn为新iPad开发的应用中的移动Web技术占到多大的比例.

Flash or HTML5,网站开发人员该如何选择?

- - mo-Android
Flash还是HTML5,对我们网页制作人员来说是个很艰难的选择,Apple iPhone/iPad不支持Flash,Adobe也宣布放弃Flash在移动平台,转而开始全面支持HTML5,但是HTML5真的就那么理想吗. HTML5也还是存在很多问题的:. 1、HTML5标准目前还是起草阶段,各大浏览器厂商支持的视频格式也不统一:IE(MP4),Firefox(WebM/Ogg),Chrome(MP4/WebM/Ogg).

技术干货分享:如何选择 HTML5 游戏引擎

- - 开源中国社区最新新闻
原生手游市场已是红海,腾讯、网易等寡头独霸天下,H5游戏市场或将成为下一个风口. 据笔者所知,很多H5游戏开发团队由于选择引擎不慎导致项目甚至团队夭折. 如何选择适合团队和项目的引擎,笔者通过学习和项目实践,总结微薄经验,供大家参考,非技术人员也可以将本篇内容作为引擎选择的重要关注点. 选择H5游戏引擎的思考维度.

MIT监控摄像头抓住Aaron Swartz

- vieplivee - Solidot
据《连线》的报导,上周被起诉的黑客Aaron Swartz是在MIT摄像头的帮助下被抓住的. Aaron Swartz因为下载了480万篇学术论文而面临最高35年徒刑和100万美元罚款. MIT的警卫是于1月4日首次听到技术人员报告有笔记本和外置硬盘藏在网络柜内,三名警察在上午进入房间取出了笔记本寻找指纹,然后将笔记本放回去,并安装了摄像头监控房间.