Capturing an image in HTML5 at full resolution

孤人 提交于 2019-12-04 14:37:36
Tom

You can't record a full-resolution picture using the MediaStream API, but you can use the Media Capture API.

The MediaStream API is able stream data from the camera, but as a video at a video resolution. This is why you can't make photos with a high resolution.

The alternative is to use the Media Capture API. It simply overrides the HTMLInput element by adding a capture=camera to the accept parameter. The result is that the native photo app opens to take a picture. This feature is currently (Nov 2017) only implemented in mobile browsers, so you still need WebRTC as a fallback if you need to support this feature on the desktop.

Working example

var myInput = document.getElementById('myFileInput');

function sendPic() {
    var file = myInput.files[0];

    // Send file here either by adding it to a `FormData` object 
    // and sending that via XHR, or by simply passing the file into 
    // the `send` method of an XHR instance.
    
    console.log(file);
}

myInput.addEventListener('change', sendPic, false);
<input id="myFileInput" type="file" accept="image/*" capture="camera">

I feel like the current situation of the MediaStream API is to access the camera of a desktop and not to actually use it to take photos with.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!