相机功能无法与cordova.js / camera.js一起使用

时间:2016-04-29 13:19:44

标签: javascript android cordova camera

我在我的javascript项目中使用cordova处理相机功能。我在我的项目中使用了cordova.js和camera.js文件。但是当我调用camera.js的getPicture()函数时,它在我的设备中无效。你可以在我的html中找到我试过的以下代码。请帮助我实现这个目标。提前致谢。

<!DOCTYPE html>
<html lang="en">
<head>
  
    <script src="cordova.js" type="text/javascript" ></script>
    <script src="Camera.js" type="text/javascript" ></script>
    <script language="javascript">

   		var pictureSource;
   		var destinationType;

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

		function onDeviceReady() {
			pictureSource=navigator.camera.PictureSourceType;
			destinationType=navigator.camera.DestinationType;
		}

		function onPhotoDataSuccess(imageData) {
			var smallImage = document.getElementById('smallImage');
			smallImage.style.display = 'block';
			smallImage.src = "data:image/jpeg;base64," + imageData;
		}

		function capturePhoto() {
			navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50,
				destinationType: destinationType.DATA_URL });
		}


		function getPhoto(source) {
			navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50,
				destinationType: destinationType.FILE_URI,
				sourceType: source });
		}
		function onFail(message) {
			alert('Failed because: ' + message);
		}
    
    
    document.getElementById('cam1').innerHTML='<a href="javascript:void(0)" onclick="capturePhoto()"><img src="../images/camera_up.gif" alt="img" width="23" height="20" border="0" id="smallImage" /></a>';
    </script>

1 个答案:

答案 0 :(得分:1)

尝试通过命令控制台安装插件,找到您的项目,然后使用此命令。

  • Cordova版本5.0 +
  

cordova插件添加cordova-plugin-camera

  • 旧版本
  

cordova插件添加org.apache.cordova.camera

此命令会自动在config.xml中添加引用

删除此引用:

<script src="Camera.js" type="text/javascript" ></script>