auto_rec_ssc_photo.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. var pickSuccess,//加载成功后回调
  2. len = 0,//上传图片个数
  3. f = [],//图片路径
  4. n = 0;//控制循环加载变量
  5. // 弹出选择照片方式
  6. function showActionSheet(num) {
  7. plus.nativeUI.actionSheet({
  8. cancel: "取消",
  9. buttons: [{
  10. title: "从相册选择"
  11. }, {
  12. title: "拍照"
  13. }]
  14. },
  15. function(event) {
  16. if (event.index == 1) {
  17. getGallery(num);
  18. } else if (event.index == 2) {
  19. getCamera();
  20. }
  21. }
  22. );
  23. };
  24. //获取相册
  25. function getGallery(num) {
  26. plus.gallery.pick(function(e) { //scb:SuccessCallBack
  27. len = e.files.length;
  28. f = e.files;
  29. n= 0;
  30. loadImg();
  31. }, function(ecb) { //ecb:ErrorCallBack
  32. }, {
  33. filename: "_doc/gallery/",
  34. filter: "image",
  35. multiple:true,
  36. maximum:num||9,
  37. system:false,
  38. onmaxed:function(){
  39. plus.nativeUI.alert('最多只能选择9张图片');
  40. }
  41. });
  42. }
  43. //加载多张图片
  44. function loadImg() {
  45. Promise.all([handlePickSucc(f[n])]).then(function (res) {
  46. n++;
  47. if (res[0].status == 200) {
  48. if(pickSuccess && $.isFunction(pickSuccess)) {
  49. pickSuccess(res[0].urlSucc);
  50. }
  51. if (n < len) {
  52. loadImg();
  53. }
  54. } else {
  55. mui.toast(res[0].msg);
  56. }
  57. });
  58. }
  59. //图片压缩
  60. function handlePickSucc(srcUrl) {
  61. return new Promise(function (resolve, reject) {
  62. var lastIdx = srcUrl.lastIndexOf("/"),
  63. imgName = srcUrl;
  64. if(lastIdx>-1) {
  65. imgName = srcUrl.slice(lastIdx+1);
  66. }
  67. plus.zip.compressImage({
  68. src: srcUrl,
  69. dst: "_doc/compressImg/"+imgName,
  70. quality: 20,
  71. overwrite: true
  72. }, function(succ) {
  73. var urlSucc = succ.target;
  74. resolve({
  75. status: 200,
  76. urlSucc: urlSucc
  77. });
  78. }, function(err) {
  79. resolve({
  80. status: -1,
  81. msg: "压缩失败: " + err.message
  82. });
  83. });
  84. });
  85. }
  86. //加载拍照图片
  87. function loadCameraPic(fileUrl) {
  88. Promise.all([handlePickSucc(fileUrl)]).then(function (res) {
  89. if (res[0].status == 200) {
  90. if(pickSuccess && $.isFunction(pickSuccess)) {
  91. pickSuccess(res[0].urlSucc);
  92. }
  93. } else {
  94. mui.toast(res[0].msg);
  95. }
  96. });
  97. }
  98. // 调用系统摄像头
  99. function getCamera() {
  100. var cmr = plus.camera.getCamera();
  101. cmr.captureImage(function(path) {
  102. /**
  103. * 拍照成功后,图片本保存在本地,这时候我们需要调用本地文件
  104. * http://www.html5plus.org/doc/zh_cn/io.html#plus.io.resolveLocalFileSystemURL
  105. */
  106. plus.io.resolveLocalFileSystemURL(path, function(entry) {
  107. /*
  108. * 将获取目录路径转换为本地路径URL地址
  109. * http://www.html5plus.org/doc/zh_cn/io.html#plus.io.DirectoryEntry.toLocalURL
  110. */
  111. var fileUrl = entry.toLocalURL();
  112. loadCameraPic(fileUrl);
  113. });
  114. }, function(error) {
  115. if(error.code==11 && error.message == "null") {
  116. plus.nativeUI.toast("您尚未授权拍照权限,无法使用拍照功能。");
  117. }
  118. // mui.toast(error.message)
  119. }, {
  120. filename: "_doc/camera/",
  121. index: 1 //ios指定主摄像头
  122. });
  123. }
  124. window.getAutoRecCompressImageLocalPath = function(cb,num) {
  125. pickSuccess = cb;
  126. showActionSheet(num);
  127. }