Cordova

Stella981
• 阅读 650

增加相机插件            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

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Stella981 Stella981
2年前
PhoneGap设置Icon
参考:http://cordova.apache.org/docs/en/latest/config\_ref/images.html通过config.xml中的<icon标签来设置Icon<iconsrc"res/ios/icon.png"platform"ios"width"57"height"57"densi
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Stella981 Stella981
2年前
Eclipse插件开发_学习_00_资源帖
一、官方资料 1.eclipseapi(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fhelp.eclipse.org%2Fmars%2Findex.jsp%3Ftopic%3D%252Forg.eclipse.platform.doc.isv%252Fguide%2
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
4个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这