移动端调用摄像头(相机)相册

上传文件的时候,选择相机和相册兼容

相册和相机切换
:在新安卓和ios,去掉capture
:在老安卓,加上capture

<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<meta name="viewport" content="width=device-width,user-scalable=no" />
	<style type="text/css">
		label {
			position: absolute;
			left: 50%;
			top: 50%;
			margin: -21px 0 0 -101px;
			 200px;
			height: 40px;
			border: 1px solid #000;
			text-align: center;
			font: 16px/40px "宋体";
		}

		input {
			display: none;
		}
	</style>
</head>

<body>
	<!--
	在各个机型都可以点击 file 调用相册 和 摄像头拍照
		1. 在老版本的安卓中,必须加上capture,否则只能调用相册
		2. 在新版本的部分安卓中 以及IOS中 加了capture,就只能调用摄像头不能调用相册

		解决办法:
			判断ios,如果是ios就去掉capture属性
	-->
	<label>上传图片
		<input type="file" id="file" accept="image/*" capture>
	</label>
	<script>
		var file = document.querySelector('#file');
		if (getIos()) {
			file.removeAttribute("capture");
		}
		function getIos() {
			var ua = navigator.userAgent.toLowerCase();
			if (ua.match(/iPhonesOS/i) == "iphone os") {
				return true;
			} else {
				return false;
			}
		}
	</script>
</body>

</html>

原文地址:https://www.cnblogs.com/leee/p/7460741.html