简体   繁体   English

使用/访问Android和iOS(移动)相机

[英]Use/Access Android and iOS (Mobile) Camera

I make an HTML5-CSS-JS Android (and iOS) Application and i try to get access to the mobile device camera. 我制作了一个HTML5-CSS-JS Android(和iOS)应用程序,并尝试访问移动设备的摄像头。

I tried two ways to get access to camera and both ways work fine when i use them from a Browser, but when i build my .APK file and i open my App none of them is working the same way as from a Browser: 我尝试了两种方法来访问相机,并且当我从浏览器中使用它们时,两种方法都可以正常工作,但是当我构建.APK文件并打开我的应用程序时,它们都无法以与浏览器相同的方式工作:

First 1) I tried Input Tag and when i click to choose/browse it allows me only to select an Existing file NOT to capture or record a new one: 首先1)我尝试了输入标签,当我单击选择/浏览时,它只允许我选择一个现有文件,而不是捕获或记录新文件:

 <input type="file" accept="image/*" capture="camera"/> 

Then 2) I found (navigator.getUserMedia) this code on web that works also from a browser but when i open my App it seems like no working and not requesting for device hardware access at all: 然后2)我在网上也找到了(navigator.getUserMedia)此代码,该代码也可以通过浏览器使用,但是当我打开我的应用程序时,它似乎无法正常工作并且根本不要求设备硬件访问:

 navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia; if (navigator.getUserMedia) { navigator.getUserMedia({ audio: true, video: { width: 1280, height: 720 } }, function(stream) { var video = document.querySelector('video'); video.src = window.URL.createObjectURL(stream); video.onloadedmetadata = function(e) { video.play(); }; }, function(err) { console.log("The following error occured: " + err.name); } ); } else { console.log("getUserMedia not supported"); } /*****************************************************************/ function init() { if(navigator.webkitGetUserMedia) { navigator.webkitGetUserMedia({video:true}, onSuccess, onFail); } else { alert('webRTC not available'); } } function onSuccess(stream) { document.getElementById('camFeed').src = webkitURL.createObjectURL(stream); } function onFail() { alert('could not connect stream'); } function takePhoto() { var c = document.getElementById('photo'); var v = document.getElementById('camFeed'); c.getContext('2d').drawImage(v, 0, 0, 320, 240); } 
 <div style="width:352px; height:625px; margin:0 auto; background-color:#fff;" > <div> <video id="camFeed" width="320" height="240" autoplay> </video> </div> <div> <canvas id="photo" width="320" height="240"> </canvas> </div> <div style="margin:0 auto; width:82px;"> <input type="button" value="Take Photo" onclick="takePhoto();"> </div> </div> 

Anyone has an idea of how can I access camera on Android or iOS? 任何人都知道如何在Android或iOS上访问相机? I tried it with and without camera permissions but this was not the problem. 我在没有相机许可的情况下进行了尝试,但这不是问题。

Help... Thank you in Advance. 帮助...预先感谢您。 (If my question is not clear comment please.) (如果我的问题不清楚,请发表评论。)

Here is the basic code, it will allow you to record/capture the file. 这是基本代码,它将允许您记录/捕获文件。

HTML: HTML:

<input id="reviewVideoCapture" type="file" accept="video/*"/>

JS (Using JQuery): JS(使用JQuery):

var videoFile = $('#reviewVideoCapture').get(0).files[0];

Use this if you want to upload the video somewhere: 如果要将视频上传到某处,请使用此选项:

<form enctype="multipart/form-data" action="/api/photo" method="POST" >
  <input id="reviewVideoCapture" type="file" accept="video/*"/>
  <input id="submitFormButton" type="submit">
</form>   

Even though this works, I would still not recommend doing it this way. 即使此方法有效,我仍然不建议这样做。 The results on mobile are buggy and hard to work with. 移动设备上的结果存在漏洞,难以使用。 The better way is to use cordova/ionic. 更好的方法是使用cordova / ionic。 A good example on how to do this can be found here: https://github.com/yafraorg/ionictests 有关如何执行此操作的一个很好的示例,可以在这里找到: https : //github.com/yafraorg/ionictests

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM