camera.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. (function (exports) {
  2. // 弹出选择照片方式
  3. function showActionSheet() {
  4. return new Promise(function (resolve, reject) {
  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(resolve, reject);
  16. } else if (event.index == 2) {
  17. getCamera(resolve, reject);
  18. }
  19. }
  20. );
  21. })
  22. };
  23. //获取相册
  24. function getGallery(resolve, reject) {
  25. plus.gallery.pick(function (path) { //scb:SuccessCallBack
  26. plus.io.resolveLocalFileSystemURL(path, function(entry) {
  27. var fileUrl = entry.toLocalURL();
  28. resolve({file:entry,fileUrl:fileUrl})
  29. }, function(e) {
  30. reject()
  31. plus.nativeUI.toast("读取拍照文件错误:" + e.message);
  32. });
  33. }, function (error) { //ecb:ErrorCallBack
  34. if (error.code == 11 && error.message == "null") {
  35. plus.nativeUI.toast("您尚未授权拍照权限,无法使用拍照功能。");
  36. reject()
  37. }
  38. });
  39. }
  40. // 调用系统摄像头
  41. function getCamera(resolve, reject) {
  42. var cmr = plus.camera.getCamera();
  43. cmr.captureImage(function (path) {
  44. plus.io.resolveLocalFileSystemURL(path, function (entry) {
  45. var fileUrl = entry.toLocalURL();
  46. resolve({file:entry,fileUrl:fileUrl})
  47. });
  48. }, function (error) {
  49. if (error.code == 11 && error.message == "null") {
  50. plus.nativeUI.toast("您尚未授权拍照权限,无法使用拍照功能。");
  51. reject()
  52. }
  53. }, {
  54. filename: "_doc/camera/",
  55. index: 1 //ios指定主摄像头
  56. });
  57. }
  58. //上传图片
  59. function UploadImg(url, params) {
  60. return new Promise(function (resolve, reject) {
  61. var task = plus.uploader.createUpload(httpRequest.server + url, {
  62. method: "POST",
  63. blocksize: 204800,
  64. priority: 100
  65. },
  66. function (t, status) {
  67. // 上传完成
  68. if (status == 200) {
  69. resolve(t);
  70. } else {
  71. reject(t)
  72. }
  73. }
  74. );
  75. task.addFile(params.file, {
  76. key: "file"
  77. });
  78. var oauthInfo = JSON.parse(plus.storage.getItem("oauthInfo"));
  79. task.addData("creator", params.creator);
  80. task.addData("objectId", params.objectId);
  81. task.addData("accessToken ", oauthInfo.accessToken);
  82. task.start();
  83. })
  84. }
  85. cameraChoose = {
  86. showActionSheet: showActionSheet,
  87. UploadImg: UploadImg
  88. }
  89. exports.cameraChoose = cameraChoose
  90. })(window)