feedback_photo.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. (function() {
  2. var pickSuccess;
  3. // 弹出选择照片方式
  4. function showActionSheet(num) {
  5. plus.nativeUI.actionSheet({
  6. cancel: "取消",
  7. buttons: [{
  8. title: "从相册选择"
  9. }, {
  10. title: "拍照"
  11. }]
  12. },
  13. function(event) {
  14. if (event.index == 1) {
  15. getGallery(num);
  16. } else if (event.index == 2) {
  17. getCamera();
  18. }
  19. }
  20. );
  21. };
  22. //获取相册
  23. function getGallery(num) {
  24. plus.gallery.pick(function(e) { //scb:SuccessCallBack
  25. for(var i in e.files){
  26. handlePickSucc(e.files[i]);
  27. }
  28. }, function(ecb) { //ecb:ErrorCallBack
  29. }, {
  30. filename: "_doc/gallery/",
  31. filter: "image",
  32. multiple:true,
  33. maximum:num||3,
  34. system:false,
  35. onmaxed:function(){
  36. plus.nativeUI.alert('最多只能选择3张图片');
  37. }
  38. });
  39. }
  40. function handlePickSucc(srcUrl) {
  41. var lastIdx = srcUrl.lastIndexOf("/"),
  42. imgName = srcUrl;
  43. if(lastIdx>-1) {
  44. imgName = srcUrl.slice(lastIdx+1);
  45. }
  46. plus.zip.compressImage({
  47. src: srcUrl,
  48. dst: "_doc/compressImg/"+imgName,
  49. quality: 20,
  50. overwrite: true
  51. }, function(succ) {
  52. var urlSucc = succ.target;
  53. // var size = succ.size;
  54. // var width = succ.width;
  55. // var height = succ.height;
  56. if(pickSuccess && $.isFunction(pickSuccess)) {
  57. pickSuccess(urlSucc);
  58. }
  59. }, function(err) {
  60. mui.toast("压缩失败: " + err.message);
  61. });
  62. }
  63. // 调用系统摄像头
  64. function getCamera() {
  65. var cmr = plus.camera.getCamera();
  66. cmr.captureImage(function(path) {
  67. /**
  68. * 拍照成功后,图片本保存在本地,这时候我们需要调用本地文件
  69. * http://www.html5plus.org/doc/zh_cn/io.html#plus.io.resolveLocalFileSystemURL
  70. */
  71. plus.io.resolveLocalFileSystemURL(path, function(entry) {
  72. /*
  73. * 将获取目录路径转换为本地路径URL地址
  74. * http://www.html5plus.org/doc/zh_cn/io.html#plus.io.DirectoryEntry.toLocalURL
  75. */
  76. var fileUrl = entry.toLocalURL();
  77. console.log(fileUrl);
  78. handlePickSucc(fileUrl);
  79. });
  80. }, function(error) {
  81. if(error.code==11 && error.message == "null") {
  82. plus.nativeUI.toast("您尚未授权拍照权限,无法使用拍照功能。");
  83. }
  84. // mui.toast(error.message)
  85. }, {
  86. filename: "_doc/camera/",
  87. index: 1 //ios指定主摄像头
  88. });
  89. }
  90. window.getAutoRecCompressImageLocalPath = function(cb,num) {
  91. pickSuccess = cb;
  92. showActionSheet(num);
  93. }
  94. })();