Cordova-拍照上傳

增長相機插件            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

相關文章
相關標籤/搜索