html5+js: resolution or size of <input type=file capture=camera>

强颜欢笑 提交于 2019-12-09 13:07:46

问题


How can I set the maximum resolution or maximum size of a photo uploaded from a mobile phone via a <input type=file capture=camera>?


回答1:


There are currently two W3C backed ways of taking pictures (image capture with JavaScript / HTML):

[1] HTML Media Capture

It uses capture and accept="image/*" on an input tag to specify the intend. As per the spec, this way does NOT allow you to specify the size of a capture.

[2] Media Capture and Streams Allows fully programmatic access to the camera so that you can implement your own capture dialogue (for video and still images). Furthermore it allows to specify constraints like this:

mandatory: {
  width: { min: 640 },
  height: { min: 480 }
},
  optional: [
{ width: 650 },
{ width: { min: 650 }},
{ frameRate: 60 },
{ width: { max: 800 }},
{ facingMode: "user" }
  ]
}    

Global Browser support for the second way is 50% so only usable in closed environments: http://caniuse.com/#feat=stream

[1] http://www.w3.org/TR/html-media-capture/

[2] http://dev.w3.org/2011/webrtc/editor/getusermedia.html#constrainable-interface




回答2:


As noted, <input type=file accept="image/*"> doesn't let you specify size or other options. This might push you towards getUserMedia(). But getUserMedia(), while it lets you grab a frame of a video stream, doesn't seem to have any of the refinements for taking good stills - flash, autofocus.

What works well for us is to grab the still with <input type=file> then resize it in javascript with canvas, then recover it as a data url. You could use this answer to resize with bilinear interpolation. This caused us performance issues on some phones (iphone 5s). If you're on a phone, I would recommend doing the first step (halving the canvas, reducing file-size by a factor of 4, very simple, do it twice if you want), and doing anything else, if you really need to, on the server.



来源:https://stackoverflow.com/questions/17004981/html5js-resolution-or-size-of-input-type-file-capture-camera

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