test.html 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
  6. <meta name="apple-mobile-web-app-capable" content="yes">
  7. <meta name="apple-mobile-web-app-status-bar-style" content="black">
  8. <!--标准mui.css-->
  9. <link rel="stylesheet" href="../../css/mui.min.css">
  10. <!-- FontAwesome字体图标 -->
  11. <link type="text/css" href="../../js/font-awesome/css/font-awesome.min.css" rel="stylesheet" />
  12. <!--App自定义的css-->
  13. <link rel="stylesheet" type="text/css" href="../../css/common.css" />
  14. <style>
  15. .my-btn {
  16. width: 80px;
  17. height: 34px;
  18. margin: 10px auto;
  19. }
  20. </style>
  21. <style type="text/css">
  22. body {
  23. background-color: #efeff4;
  24. }
  25. .mui-content {}
  26. .mui-content a {
  27. color: #8F8F94;
  28. }
  29. .mui-content a.active {
  30. color: #007aff;
  31. }
  32. .mui-title {
  33. font-family: simhei;
  34. }
  35. .btn_1 {
  36. position: absolute;
  37. bottom: 100px;
  38. left: 10px;
  39. right: 10px;
  40. }
  41. .btn_2 {
  42. position: absolute;
  43. bottom: 20px;
  44. left: 10px;
  45. right: 10px;
  46. }
  47. .mui-btn-block {
  48. width: 90%;
  49. margin: 0 auto;
  50. }
  51. body {
  52. overflow: hidden;
  53. }
  54. .showimg {
  55. margin: 20px 10px auto 10px;
  56. text-align: center;
  57. }
  58. </style>
  59. </head>
  60. <body>
  61. <header class="mui-bar mui-bar-nav">
  62. <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
  63. <h1 class="mui-title">上传身份证照片</h1>
  64. <a class="mui-pull-right mui-icon mui-icon-upload" onclick="imgUpload()"></a>
  65. </header>
  66. <div class="mui-content">
  67. <div class="showimg" id="showimg">
  68. </div>
  69. <button type="button" class="mui-btn mui-btn-primary mui-btn-block btn_1" onclick="galleryImgs()">从相册中选择图片</button>
  70. <br>
  71. <button type="button" class="mui-btn mui-btn-success mui-btn-block btn_2" onclick="cameraImages()">拍照</button>
  72. </div>
  73. </body>
  74. <script src="../../js/mui.min.js"></script>
  75. <script type="text/javascript" src="../../js/jquery/jquery.min.js"></script>
  76. <script type="text/javascript" src="../../js/jquery/jquery.cookie.js"></script>
  77. <script type="text/javascript" src="../../js/wms/appRequest.js"></script>
  78. <script src="../../js/photo/binaryajax.js" type="text/javascript" charset="utf-8"></script>
  79. <script src="../../js/photo/exif.js" type="text/javascript" charset="utf-8"></script>
  80. <script src="../../js/photo/canvasResize.js" type="text/javascript" charset="utf-8"></script>
  81. <script type="text/javascript">
  82. //$.noConflict()//jq 释放 $
  83. //mui初始化
  84. mui.init({
  85. swipeBack: true //启用右滑关闭功能
  86. });
  87. (function($, doc) {
  88. $.init();
  89. $.plusReady(function() {
  90. document.addEventListener("netchange", checkNetwork, false);
  91. function checkNetwork() {
  92. if(plus.networkinfo.getCurrentType() == plus.networkinfo.CONNECTION_NONE) {
  93. mui.toast("网络异常,请检查网络设置!");
  94. }
  95. }
  96. });
  97. }(mui, document));
  98. // 全局数组对象,添加文件,用于压缩上传使用
  99. var f1 = new Array();
  100. //上传单张图片
  101. function galleryImg() {
  102. //每次拍摄或选择图片前清空数组对象
  103. f1.splice(0, f1.length);
  104. document.getElementsByClassName("showimg")[0].innerHTML = null;
  105. // 从相册中选择图片
  106. mui.toast("从相册中选择一张图片");
  107. plus.gallery.pick(function(path) {
  108. showImg(path);
  109. }, function(e) {
  110. mui.toast("取消选择图片");
  111. }, {
  112. filter: "image",
  113. multiple: false
  114. });
  115. }
  116. //从相册选择多张图片
  117. function galleryImgs() {
  118. //每次拍摄或选择图片前清空数组对象
  119. f1.splice(0, f1.length);
  120. document.getElementsByClassName("showimg")[0].innerHTML = null;
  121. // 从相册中选择图片
  122. mui.toast("一次最多选择6张图片");
  123. plus.gallery.pick(function(e) {
  124. if(e.files.length < 1) {
  125. mui.toast('请最少选择一张图片');
  126. return false;
  127. }
  128. if(e.files.length > 6) {
  129. mui.toast('不能超过6张图片');
  130. return false;
  131. }
  132. for(var i in e.files) {
  133. showImg(e.files[i]);
  134. }
  135. }, function(e) {
  136. mui.toast("取消选择图片");
  137. }, {
  138. filter: "image",
  139. multiple: true
  140. });
  141. }
  142. // 拍照添加文件
  143. function cameraImages() {
  144. //每次拍摄或选择图片前清空数组对象
  145. f1.splice(0, f1.length);
  146. document.getElementsByClassName("showimg")[0].innerHTML = null;
  147. var cmr = plus.camera.getCamera();
  148. cmr.captureImage(function(p) {
  149. plus.io.resolveLocalFileSystemURL(p, function(entry) {
  150. var localurl = entry.toLocalURL(); //把拍照的目录路径,变成本地url路径,例如file:///........之类的。
  151. showImg(localurl);
  152. });
  153. }, function(e) {
  154. mui.toast("很抱歉,调用摄像头失败 ");
  155. });
  156. }
  157. //显示预览图片
  158. function showImg(url) {
  159. // 兼容以“file:”开头的情况
  160. if(0 != url.toString().indexOf("file://")) {
  161. url = "file://" + url;
  162. }
  163. var div = document.getElementsByClassName("showimg")[0];
  164. var img = new Image();
  165. img.src = url; // 传过来的图片路径在这里用。
  166. img.onclick = function() {
  167. plus.runtime.openFile(url);
  168. };
  169. img.onload = function() {
  170. var tmph = img.height;
  171. var tmpw = img.width;
  172. var isHengTu = tmpw > tmph;
  173. var max = Math.max(tmpw, tmph);
  174. var min = Math.min(tmpw, tmph);
  175. var bili = min / max;
  176. if(max > 1200) {
  177. max = 1200;
  178. min = Math.floor(bili * max);
  179. }
  180. tmph = isHengTu ? min : max;
  181. tmpw = isHengTu ? max : min;
  182. img.style.border = "1px solid rgb(200,199,204)";
  183. img.style.margin = "10px";
  184. img.style.width = "150px";
  185. img.style.height = "150px";
  186. img.onload = null;
  187. plus.io.resolveLocalFileSystemURL(url, function(entry) {
  188. entry.file(function(file) {
  189. console.log(file.size + '--' + file.name);
  190. canvasResize(file, {
  191. width: tmpw,
  192. height: tmph,
  193. crop: false,
  194. quality: 50, //压缩质量
  195. rotate: 0,
  196. callback: function(data, width, height) {
  197. f1.push(data);
  198. img.src = data;
  199. div.appendChild(img);
  200. plus.nativeUI.closeWaiting();
  201. }
  202. });
  203. });
  204. },
  205. function(e) {
  206. plus.nativeUI.closeWaiting();
  207. console.log(e.message);
  208. });
  209. };
  210. };
  211. //提交
  212. function imgUpload() {
  213. mui.toast('后台联调时启用上传功能');
  214. return;
  215. var wt = plus.nativeUI.showWaiting();
  216. var url = '后台地址';
  217. var dataType = 'json';
  218. //发送数据
  219. var data = {
  220. files1: f1 //base64数据
  221. };
  222. mui.post(url, data, success, dataType);
  223. }
  224. //成功响应的回调函数
  225. var success = function(response) {
  226. plus.nativeUI.closeWaiting();
  227. if(response != null) {
  228. alert("上传成功");
  229. }
  230. }
  231. </script>
  232. </html>