資料ページ Taking still photos with WebRTC 2016/3末時点のWebRTCブラウザ対応状況まとめ ※ デモページは、WEBアプリ用のテンプレートを Bootstrap を使用してスマホ対応で作成しています ▼ デモページ MediaDevices - Web API インターフェイス | MDN Taking still photos with WebRTC - Web APIs | MDN ▼ 初回( Google Chrome ) ▼ カメラが無い場合 ソースコード 当初、古い API を実装していたのですが、いろいろ調べるうちに新しい API に変わっていた事に気がついて、両方のコードが実装されています。 WebRTC の処理部分は 119行 〜 183行です。 ※ 処理画面部分は 220行 〜 256行 ※ 画面構築しているテンプレート部分は、64行 〜 86行 と 98行 〜 116行
<!DOCTYPE html> <html lang="ja"> <head> <meta content="width=device-width initial-scale=1.0 minimum-scale=1.0 maximum-scale=1.0 user-scalable=no" name="viewport"> <meta charset="utf-8"> <title>カメラ表示</title> <script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> <link id="link" rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/themes/base/jquery-ui.css"> <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css"> <script src="https://cdnjs.cloudflare.com/ajax/libs/jQuery.mmenu/5.5.3/core/js/jquery.mmenu.min.all.js"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jQuery.mmenu/5.5.3/core/css/jquery.mmenu.all.css"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.1.3/toastr.min.css"> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.1.3/toastr.min.js"></script> <link rel="stylesheet" href="../std/mmenu.css"> <link rel="stylesheet" href="../std/basic.css"> <style> .fields { width: 85px; font-size: 12px; vertical-align: middle!important; } legend { font-size: 18px; padding-left: 6px; } /* カメラ用 */ #camera { width: 400px; height: 300px; object-fit: fill; } .table-responsive td, .table-responsive th { white-space: nowrap; } </style> <script> jQuery.isMobile = (/android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(navigator.userAgent.toLowerCase())); toastr.options={"closeButton":false,"debug":false,"newestOnTop":false,"progressBar":false,"positionClass":"toast-bottom-center","preventDuplicates":false,"onclick":null,"showDuration":"300","hideDuration":"1000","timeOut":"3000","extendedTimeOut":"1000","showEasing":"swing","hideEasing":"linear","showMethod":"fadeIn","hideMethod":"fadeOut"}; if ( !$.isMobile ) { toastr.options.positionClass = "toast-top-center"; } var datepicker_option = { dateFormat: 'yy/mm/dd', dayNamesMin: ['日', '月', '火', '水', '木', '金', '土'], monthNames: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'], showMonthAfterYear: true, yearSuffix: '年', changeYear: true, showAnim: 'fadeIn', yearRange: "c-70:c" } $(function(){ var curlink = $("#title").text(); $("#title").html("<a href=\"./\" style=\"color:#fff\">" + curlink + "</a>"); }); var options = { row1 : { title : "カメラ" }, row_last : { title :"メッセージ" }, error : function(message){ $("#row_last").next().text( message ); toastr.error(message); }, info : function(message){ $("#row_last").next().text( message ); toastr.success(message); }, cerror : function( message ){ message = message + "<br>代替として動画を表示します" $("#row_last").next().html( message ); toastr.error( message ); $("#camera") .prop({ "loop" : true, "muted" : true, "controls" : true, "src" : "https://lightbox.sakura.ne.jp/demo/mp4/waterfall-free-video9.mp4" }) .css("border", "solid 1px #000"); } }; // ************************************* // カメラ用データ // ************************************* var camera; var canvas; $(function(){ // 1) options による行とフィールドの設定 // 2) Bootstrap 用 form-control クラスの追加 $(".fields").each(function(){ if ( options[ $(this).prop("id") ] ) { $(this).html( options[ $(this).prop("id") ].title ); // 個別 css if ( options[ $(this).prop("id") ].css ) { $(this).next().find("input,select").css( options[ $(this).prop("id") ].css ); } // 入力チェック用属性 if ( options[ $(this).prop("id") ].attr ) { $(this).next().find("input,select").attr( options[ $(this).prop("id") ].attr ); } } $(this).next().find("input,select").addClass("form-control"); }); // スマホでロード時の処理のチラつき防止用 $("#wrapper").css({"visibility":"visible", "margin-bottom" : "0px" }); // 初期フォーカス(row1_fld があれば機能します) setTimeout( function(){$('#row1_fld').focus();}, 100 ); // video 内 camera camera = $("#camera").get(0); // ************************************* // 最新 API // ************************************* if ( navigator.mediaDevices ) { if ( $.isMobile ) { $("#camera").css("width","100%"); } navigator.mediaDevices.getUserMedia({video: true}) .then(function(stream){ camera.src = window.URL.createObjectURL(stream); }) .catch(function(err){ // ブラウザで使用を拒否した場合等( 動画で代替 ) options.cerror(err.name); }); // デバイス一覧 $("#result").append("<tr><th>label</th><th>deviceId</th><th>kind</th></tr>"); navigator.mediaDevices.enumerateDevices() .then(function(devices) { devices.forEach(function(device) { $("#result").append("<tr><td>" + device.label + "</td><td>" + device.deviceId +"</td><td>"+ device.kind + "</td></tr>"); }); }) .catch(function(err) { options.error(err.name + ": " + err.message); }); } // ************************************* // 旧 API // ************************************* else { // 旧 WebRTCチェック用 var api = [ "webkitGetUserMedia", "mozGetUserMedia","msGetUserMedia" ] $.each(api,function(idx){ if (navigator.getUserMedia = navigator.getUserMedia || navigator[api[idx]]) { return false; } }); // WebRTC 使用可能 if ( navigator.getUserMedia ) { if ( $.isMobile ) { $("#camera").css("width","100%"); } // カメラの表示 navigator.getUserMedia({video: true}, function(stream) { camera.src = window.URL.createObjectURL(stream); }, function(err){ // ブラウザで使用を拒否した場合等( 動画で代替 ) options.cerror(err.name); } ); } else { // WebRTC 使用不可( 動画で代替 ) options.cerror("WebRTC を使用できません"); } } // ************************************** // mmenu // ************************************** $("#mmenu_left").mmenu({ navbar: { title: "メニュー" }, offCanvas: { position : "left", zposition : "next" } }); }); </script> </head> <body> <div id="wrapper"> <script> // スマホでロード時の処理のチラつき防止用 $("#wrapper").css( {"visibility": "hidden", "margin-bottom" : "1000px" } ); </script> <div id="head"> <a id="hamburger" href="#mmenu_left"> <span class="top-bar"></span> <span class="middle-bar"></span> <span class="bottom-bar"></span> </a> <div id="title">カメラ表示</div> </div> <div id="body"> <form id="frm" class="form-inline"> <fieldset> <legend></legend> <table class="table table-condensed"> <tr> <td class="fields" id="row1"></td> <td> <video id="camera" autoplay></video> </td> </tr> <tr> <td class="fields error" id="row_last"></td> <td></td> </tr> </table> </fieldset> <fieldset> <legend>結果</legend> <div class="table-responsive"> <table id="result" class="table table-condensed"> </table> </div> </fieldset> </form> </div> <div id="comment"> ようこそ jQuery + Bootstrap(css) + mmenu + WebRTC(カメラ) </div> </div> <div id="mmenu_left"> <ul> <li class="mm_user_title">ページ選択</li> <li><a class="mm_link_left" href="#" onclick="location='index.php';void(0)">リセット</a></li> <li><a class="mm_link_left" href="http://getbootstrap.com/css/" onclick="location='index.php';void(0)" target="_blank" >Bootstrap(css)</a></li> <li><a class="mm_link_left" href="http://api.jquery.com/" onclick="location='index.php';void(0)" target="_blank" >jQuery ドキュメント</a></li> <li><a class="mm_link_left" href="https://developer.mozilla.org/ja/docs/Web/API/MediaDevices" onclick="location='index.php';void(0)" target="_blank" >MediaDevices (MDN)</a></li> </ul> </div> </body> </html>
関連する記事 WebRTC による WEBカメラ表示を canvas にコピーして画像に変換し、サーバへアップロードする
|
【APIの最新記事】
- WebRTC による WEBカメラ表示( 2018/11/15 ) : iPhone + Safari は iOS11 のみで確認
- PHP で Mastodon にアプリを登録して投稿する手順
- Google スプレッドシートの内容を JSON として localhost で取得する手順
- WebRTC による WEBカメラ表示を canvas にコピーして画像に変換し、サーバへアップロードする
- Amazon API の 503エラー の対処について
- ドコモ デベロッパーサポートで API使いたいと思ってサインアップしようとしたら・・・・。法人情報は後から登録しましょう
- GitHub の yahoojapan/yconnect-php-sdk( PHP ) を使用して、Yahoo! にログインさせてユーザ情報を取得する( OPENID )
- 手動で Facebook API の 60日間の アクセストークンを取得する
- Facebook Graph API Explorer でカスタムプライバシー設定をした投稿を行う
- C#(.NET) : Google Spreadsheets API version 3.0でGoogleスプレッドシートを参照
- Twitpic の画像URLを取得する API が変わっているようなのですが