移動端調用攝像頭(相機)相冊


上傳文件的時候,選擇相機和相冊兼容

相冊和相機切換
:在新安卓和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;
			width: 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(/iPhone\sOS/i) == "iphone os") {
				return true;
			} else {
				return false;
			}
		}
	</script>
</body>

</html>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM