Cordova-拍照上傳
小編:管理員 110閱讀 2022.09.13
增加相機插件 cordova plugin add cordova-plugin-camera (如果刪除add改為remove )
增加文件上傳插件 cordova plugin add cordova-plugin-file-transfer
<!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集成包
<?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.xml
(2)alert scanning failed:write settings:false,把targetSdkVersion從原來的25改成22 <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="22" />
(adsbygoogle = window.adsbygoogle || []).push({});
相關推薦
- Cordova 什么是Cordova? Cordova是用于使用HTML,CSS和JS構建移動應用的平臺。我們可以認為Cordova是一個容器,用于將我們的網絡應用程序與本機移動功能連接。默認情況下,Web應用程序不能使用本機移動功能。這就是Cordova進來的地方。它為網絡應用和移動設備之間的連…
- Hibernate Criterion 在查詢方法設計上能夠靈活的依據Criteria的特點來方便地進行查詢條件的組裝.Hibernate設計了CriteriaSpecification作為Criteria的父接口,以下提供了Criteria和DetachedCriteria.Criteria和DetachedCriteria的主要差別在于創建的形式不一樣,Criteria是在線的,所…