Xer*_*oid 122
你可以试试这个:
<input type="file" capture="camera" accept="image/*" id="cameraInput" name="cameraInput">
Run Code Online (Sandbox Code Playgroud)
但它必须是iOS 6+才能工作.这将为您提供一个很好的对话,让您选择拍照或从您的相册上传一个,即
可以在此处找到一个示例:在 没有PhoneGap的情况下捕获相机/图片数据
Sim*_*ast 32
截至2015年,它现在正常运作.
<input type="file">
Run Code Online (Sandbox Code Playgroud)
这将要求用户上传任何文件.在iOS 8.x上,这可以是相机视频,相机照片或照片库中的照片/视频.
<input type="file" accept="image/*">
Run Code Online (Sandbox Code Playgroud)
如上所述,但仅限于从相机或库中上传照片,没有视频.
use*_*691 25
在iOS6中,Apple通过<input type="file">
标签支持此功能.我找不到苹果的开发者文档中一个有益的联系,但有一个例子在这里.
它看起来像叠加层和更高级的功能尚不可用,但这应该适用于很多用例.
编辑:w3c有一个规格,iOS6 Safari似乎实现了一个子集.该capture
属性明显缺失.
sin*_*dam 10
我认为这个有效.录制视频或音频;
<input type="file" accept="video/*;capture=camcorder">
<input type="file" accept="audio/*;capture=microphone">
Run Code Online (Sandbox Code Playgroud)
或(新方法)
<device type="media" onchange="update(this.data)"></device>
<video autoplay></video>
<script>
function update(stream) {
document.querySelector('video').src = stream.url;
}
</script>
Run Code Online (Sandbox Code Playgroud)
如果不是,可能适用于ios6,可以在获取用户媒体上找到更多细节
11/2020 更新:Google Developer 链接(目前)已失效。带有更多解释的原始文章仍然可以在web.archive.org找到。
这个问题已经有几年的历史了,但那时已经发展了一些额外的可能性,例如直接访问相机、显示预览和捕获快照(例如用于二维码扫描)。
这篇Google 开发者文章深入解释了如何将图像/相机数据导入 Web 应用程序的所有(?)方法,从“随处工作”(甚至在桌面浏览器中)到“仅在现代、最新的浏览器上工作” - 与带有相机的移动设备约会”。以及许多有用的提示。
解释方法:
请求 URL:最简单但最不令人满意。
文件输入(这里的大多数其他帖子都涵盖了):然后可以通过侦听输入元素上的 onchange 事件,然后读取事件目标的 files 属性,将数据附加到 a 或使用 JavaScript 进行操作。
<input type="file" accept="image/*" id="file-input">
<script>
const fileInput = document.getElementById('file-input');
fileInput.addEventListener('change', (e) => doSomethingWithFiles(e.target.files));
</script>
Run Code Online (Sandbox Code Playgroud)
该files
属性是一个 FileList 对象。
<div id="target">You can drag an image file here</div>
<script>
const target = document.getElementById('target');
target.addEventListener('drop', (e) => {
e.stopPropagation();
e.preventDefault();
doSomethingWithFiles(e.dataTransfer.files);
});
target.addEventListener('dragover', (e) => {
e.stopPropagation();
e.preventDefault();
e.dataTransfer.dropEffect = 'copy';
});
</script>
Run Code Online (Sandbox Code Playgroud)
您可以FileList
从事件dataTransfer.files
的属性中获取一个对象drop
。
<textarea id="target">Paste an image here</textarea>
<script>
const target = document.getElementById('target');
target.addEventListener('paste', (e) => {
e.preventDefault();
doSomethingWithFiles(e.clipboardData.files);
});
</script>
Run Code Online (Sandbox Code Playgroud)
e.clipboardData.files
又是一个FileList
对象。
const supported = 'mediaDevices' in navigator;
提示用户同意。然后显示实时预览并将快照复制到画布。<video id="player" controls autoplay></video>
<button id="capture">Capture</button>
<canvas id="canvas" width=320 height=240></canvas>
<script>
const player = document.getElementById('player');
const canvas = document.getElementById('canvas');
const context = canvas.getContext('2d');
const captureButton = document.getElementById('capture');
const constraints = {
video: true,
};
captureButton.addEventListener('click', () => {
// Draw the video frame to the canvas.
context.drawImage(player, 0, 0, canvas.width, canvas.height);
});
// Attach the video stream to the video element and autoplay.
navigator.mediaDevices.getUserMedia(constraints)
.then((stream) => {
player.srcObject = stream;
});
</script>
Run Code Online (Sandbox Code Playgroud)
不要忘记停止视频流
player.srcObject.getVideoTracks().forEach(track => track.stop());
Run Code Online (Sandbox Code Playgroud)
11/2020 更新:Google Developer 链接(目前)已失效。带有更多解释的原始文章仍然可以在web.archive.org找到。
归档时间: |
|
查看次数: |
204053 次 |
最近记录: |