auto_rec_ssc_photo.js 2.2 KB

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