能不能通过js代码打开摄像头

JavaScript032

能不能通过js代码打开摄像头,第1张

html5中的video这个标签是引入视频的,通过navigator.getUserMedia去获取摄像头的视频流,所以要在事件里用关闭的代码都不能执行关闭摄像头,只有关闭网页,摄像头才关闭。

以下为html5打开摄像头代码:

<!DOCTYPE html>

<html>

<head>

<meta content="text/htmlcharset=UTF-8" http-equiv="content-type">

<title>Smart Home - Camera</title>

<link href="css/main.css" rel="stylesheet" type="text/css">

<script src="js/jq.js"></script>

<script type="text/javascript">

/*

*/

function init(t){

accessLocalWebCam("camera_box")

}

// Normalizes window.URL

window.URL = window.URL || window.webkitURL || window.msURL || window.oURL

// Normalizes navigator.getUserMedia

navigator.getUserMedia = navigator.getUserMedia || 

navigator.webkitGetUserMedia|| navigator.mozGetUserMedia || 

navigator.msGetUserMedia

function isChromiumVersionLower() {

var ua = navigator.userAgent

var testChromium = ua.match(/AppleWebKit\/.* Chrome\/([\d.]+).* Safari\//)

return (testChromium &&(parseInt(testChromium[1].split('.')[0]) <19))

}

function successsCallback(stream) {

document.getElementById('camera_errbox').style.display = 'none'

document.getElementById('camera_box').src = (window.URL 

&&window.URL.createObjectURL) ? 

window.URL.createObjectURL(stream) : stream

}

function errorCallback(err) {

}

function accessLocalWebCam(id) {

try {

// Tries it with spec syntax

navigator.getUserMedia({ video: true }, successsCallback, errorCallback)

} catch (err) {

// Tries it with old spec of string syntax

navigator.getUserMedia('video', successsCallback, errorCallback)

}

}

</script>

<style type="text/css">

#camera_errbox{

width:320pxheight:autoborder:1px solid #333333padding:10px

color:#ffftext-align:leftmargin:20px auto

font-size:14px

}

#camera_errbox b{

padding-bottom:15px

}

</style>

</head>

<body onLoad="init(this)" oncontextmenu="return false" onselectstart="return false">

<div>

<div id="mainbox">

<div id="bt_goback"></div>

<div></div><div id="t_iconbox" 

class="icon_12"></div><div id="t_text">

<div id="el_title">Camera</div>

<div id="el_descr"></div>

</div>

<div></div>

<div><span 

class="sp_title_text">Camera</span><div class="sp_oc 

sp_oc_1"></div></div>

<dl id="el_actionbox" style="text-align:center">

<video id="camera_box" autoplay="" src=""></video>

<div id="camera_errbox">

<b>请点击“允许”按钮,授权网页访问您的摄像头!</b>

<div>若您并未看到任何授权提示,则表示您的浏览器不支持Media Capture或您的机器没有连接摄像头设备。</div>

</div>

</dl>

</div>

</div>

</body>

</html>

-——代码结束

难道车是prefab,游戏开始的时候才初始化的?你把车直接拖入场景看看,另外附一段第3人称C#脚本,只需把车的tag改成Player,赋到camera就行了

using UnityEngine

using System.Collections

public class ThirdPersonCamera : MonoBehaviour

{

public float distanceAway // distance from the back of the craft

public float distanceUp // distance above the craft

public float smooth // how smooth the camera movement is

private GameObject hovercraft // to store the hovercraft

private Vector3 targetPosition // the position the camera is trying to be in

Transform follow

void Start(){

follow = GameObject.FindWithTag ("Player").transform

}

void LateUpdate ()

{

// setting the target position to be the correct offset from the hovercraft

targetPosition = follow.position + Vector3.up * distanceUp - follow.forward * distanceAway

// making a smooth transition between it's current position and the position it wants to be in

transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * smooth)

// make sure the camera is looking the right way!

transform.LookAt(follow)

}

}

需要加载cordova.js

方法:

document.addEventListener("deviceready", onDeviceReady, false)

function onDeviceReady() {

pictureSource = navigator.camera.PictureSourceType

destinationType = navigator.camera.DestinationType

}

//相册

function fromCamera()

{

var source = pictureSource.PHOTOLIBRARY

navigator.camera.getPicture(function (imageData) {

setimg(imageData)

}, function (message) {

if (source == pictureSource.CAMERA)

alert('加载照相机出错!' + message)

else

alert('加载相册出错!' + message)

}, {

quality: 50,

destinationType: destinationType.FILE_URI,

sourceType: source

})

}

//拍照

function EditImgPz()

{

navigator.camera.getPicture(function (imageData) {

setimg(imageData)

}, function (message) {

alert(message)

}, {

quality: 50,

destinationType: navigator.camera.DestinationType.FILE_URI,

sourceType: Camera.PictureSourceType.CAMERA,

allowEdit: true,

encodingType: Camera.EncodingType.JPEG,

popoverOptions: CameraPopoverOptions,

saveToPhotoAlbum: true

})

}