增长相机插件 cordova plugin add cordova-plugin-camera (若是删除add改成remove )javascript
增长文件上传插件 cordova plugin add cordova-plugin-file-transferphp
<!DOCTYPE html> <html> <head> <title>take Photo</title> <meta http-equiv="Content-type" content="text/html; charset=utf-8"> <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width"> <script type="text/javascript" charset="utf-8" src="cordova.js"></script> <script type="text/javascript" charset="utf-8"> document.addEventListener("deviceready", onDeviceReady, false); //Cordova加载完成会触发 function onDeviceReady() { document.getElementById("phonebutton").addEventListener("click", function() { navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 80, sourceType: Camera.PictureSourceType.CAMERA, destinationType: Camera.DestinationType.FILE_URI, targetWidth: 520, targetHeight: 520 }); }); //拍照成功 function onPhotoDataSuccess(imageUri) { var smallImage = document.getElementById('smallImage'); smallImage.style.display = 'block'; smallImage.src = imageUri; upLoadImg(imageUri) } //拍照失败 function onFail(message) { alert('拍照失败: ' + message); } } // file-Transfer插件,上传图片文件 function upLoadImg(imageURI){ //alert("ok"); var options = new FileUploadOptions(); //options.chunkedMode = false; options.fileKey = "file"; options.fileName = imageURI.substring(imageURI.lastIndexOf('/')+1); options.mimeType = "image/jpeg"; options.httpMethod = "POST"; // console.log("options======="); // console.log(options); var fileTransfer = new FileTransfer(); var successCallback = function(r){ alert("Code = " + r.responseCode); alert("Response = " + r.response); alert("Sent = " + r.bytesSent); } var errorCallback = function (error) { alert("An error has occurred: Code = " + error.code); alert("upload error source " + error.source); alert("upload error target " + error.target); } fileTransfer.upload( imageURI, //本地文件路径 encodeURI("http://192.168.1.199/uploaddemo/upload.php"), //服务器上传的路径 successCallback, //成功的回调 errorCallback, //失败的回调 options); //配置项 } </script> </head> <body style="padding-top:50px"> <button id="phonebutton" style="font-size:23px;">take a picture13</button> <br> <img style="display:none;width:260px;height:260px;" id="smallImage" src="" /> </body> <!-- 关于拍摄图片的大小 若是设置targetWidth: 520, targetHeight: 520,最终拍摄的图片依然是长方形,只会把最大的设置为520px,另外一个按比例显示,如最终的图片是(390*520px) --> </html>
后台使用wamp集成包 html
<?php // 容许上传的图片后缀 header("Content-type: text/html; charset=utf-8"); $allowedExts = array("gif", "jpeg", "jpg", "png"); $temp = explode(".", $_FILES["file"]["name"]); echo $_FILES["file"]["size"]; $extension = end($temp); // 获取文件后缀名 if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/pjpeg") || ($_FILES["file"]["type"] == "image/x-png") || ($_FILES["file"]["type"] == "image/png")) && ($_FILES["file"]["size"] < 204800) // 小于 200 kb && in_array($extension, $allowedExts)) { if ($_FILES["file"]["error"] > 0) { echo "错误:: " . $_FILES["file"]["error"] . "<br>"; } else { echo "上传文件名: " . $_FILES["file"]["name"] . "<br>"; echo "文件类型: " . $_FILES["file"]["type"] . "<br>"; echo "文件大小: " . ($_FILES["file"]["size"] / 1024) . " kB<br>"; echo "文件临时存储的位置: " . $_FILES["file"]["tmp_name"] . "<br>"; // 判断当期目录下的 upload 目录是否存在该文件 // 若是没有 upload 目录,你须要建立它,upload 目录权限为 777 if (file_exists("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " 文件已经存在。 "; } else { // 若是 upload 目录不存在该文件则将文件上传到 upload 目录下 $name=iconv("UTF-8", "gbk",$_FILES["file"]["name"]); move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $name); echo "文件存储在: " . "upload/" . $_FILES["file"]["name"]; } } } else { echo "非法的文件格式"; } //在使用move_uploaded_file时,路径upload/,必定要先创建upload的文件夹,不然出错 ?>
(1)打包老是出错,按下面解决方法ok
Open plugins/cordova-plugin-barcode-scanner/plugin.xml and delete all records xmlns:android=""
Open platforms/android/android.json and delete all xmlns:android=\"\"
Do the same in platforms/android/AndroidManifest.xmljava
(2)alert scanning failed:write settings:false,把targetSdkVersion从原来的25改为22
<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="22" />android