新手操作指南Demo - 使用双汇UI代码

cropper.html 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>裁剪头像</title>
  6. <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
  7. <link href="../css/mui.min.css" rel="stylesheet" />
  8. <link href="../js/cropper/cropper.css" rel="stylesheet" />
  9. <style type="text/css">
  10. body {
  11. background-color: #000000;
  12. }
  13. #cropper-example-1 {
  14. background-color: #000000;
  15. height: 93%;
  16. width: 100%;
  17. position: absolute;
  18. }
  19. .divbut {
  20. width: 100%;
  21. text-align: center;
  22. position: fixed;
  23. z-index: 2;
  24. bottom: 0px;
  25. background-color: #000000;
  26. height: 7.5%;
  27. line-height: 50px;
  28. }
  29. .divbut>div:first-child {
  30. float: left;
  31. width: 20%;
  32. }
  33. .divbut>div:last-child {
  34. float: right;
  35. width: 20%;
  36. }
  37. img#im {
  38. height: 100%;
  39. width: 100%;
  40. }
  41. </style>
  42. </head>
  43. <body>
  44. <div id="cropper-example-1" class="mui-hidden">
  45. <img id="im" alt="Picture" />
  46. </div>
  47. <div class="divbut">
  48. <div>
  49. <p id="quxiao" class="mui-icon mui-icon-closeempty"></p>
  50. </div>
  51. <div>
  52. <p id="xuanqu" class="mui-icon mui-icon-checkmarkempty"></p>
  53. </div>
  54. </div>
  55. <img src="" alt="" class="mui-hidden" id="im_exif" />
  56. <script type="text/javascript" src="../js/jquery-1.8.js"></script>
  57. <script src="../js/mui.min.js"></script>
  58. <script type="text/javascript" src="js/exif.js"></script>
  59. <script src="../js/cropper/cropper.min.js"></script>
  60. <script src="../js/app.js"></script>
  61. <script>
  62. (function(c) {
  63. var Cro = function() {}
  64. c.extend(Cro.prototype, {
  65. orientation: null,
  66. urldata: null,
  67. view: null,
  68. num: 0,
  69. sbx: null,
  70. sby: null,
  71. n: 0,
  72. onReady: function() {
  73. var that = this;
  74. mui.init();
  75. that.bindEvent();
  76. that.view = plus.webview.currentWebview();
  77. var img = document.getElementById("im_exif");
  78. img.src = that.view.path;
  79. img.addEventListener("load", function() {
  80. //exif调整图片的横竖
  81. EXIF.getData(this, function() {
  82. var orientation = EXIF.getAllTags(this).Orientation;
  83. $("#im").attr("src", that.loadcopyImg(img, orientation));
  84. document.getElementById("cropper-example-1").classList.remove("mui-hidden"); //显示裁剪区域
  85. that.cropperImg();
  86. });
  87. })
  88. },
  89. cropperImg: function() {
  90. var that = this;
  91. $('#cropper-example-1 > img').cropper({
  92. aspectRatio: 1 / 1,
  93. autoCropArea: 1,
  94. strict: true,
  95. background: false,
  96. guides: false,
  97. highlight: false,
  98. dragCrop: false,
  99. movable: false,
  100. resizable: false,
  101. crop: function(data) {
  102. that.urldata = that.base64(data);
  103. }
  104. });
  105. },
  106. loadcopyImg: function(img, opt) {
  107. var that = this;
  108. var canvas = document.createElement("canvas");
  109. var square = 500;
  110. var imageWidth, imageHeight;
  111. if(img.width > img.height) {
  112. imageHeight = square;
  113. imageWidth = Math.round(square * img.width / img.height);
  114. } else {
  115. imageHeight = square; //this.width;
  116. imageWidth = Math.round(square * img.width / img.height);
  117. }
  118. canvas.height = imageHeight;
  119. canvas.width = imageWidth;
  120. if(opt == 6) {
  121. that.num = 90;
  122. } else if(opt == 3) {
  123. that.num = 180;
  124. } else if(opt == 8) {
  125. that.num = 270;
  126. }
  127. if(that.num == 360) {
  128. that.num = 0;
  129. }
  130. var ctx = canvas.getContext("2d");
  131. ctx.translate(imageWidth / 2, imageHeight / 2);
  132. ctx.rotate(that.num * Math.PI / 180);
  133. ctx.translate(-imageWidth / 2, -imageHeight / 2);
  134. ctx.drawImage(img, 0, 0, imageWidth, imageHeight);
  135. var dataURL = canvas.toDataURL("image/jpeg", 1);
  136. return dataURL;
  137. },
  138. bindEvent: function() {
  139. var that = this;
  140. document.getElementById("quxiao").addEventListener("tap", function() {
  141. mui.back(); //取消就直接返回
  142. });
  143. document.getElementById("xuanqu").addEventListener("tap", function() {
  144. //触发上一个页面刷新图片事件
  145. var preView = plus.webview.getWebviewById('user/headinfo');
  146. mui.fire(preView, 'updateHeadImg', {
  147. 'img': that.urldata
  148. }); //不能保存图片,需要判断上传性,所以选择传值的方式,传递图片,格式为json
  149. mui.back();
  150. });
  151. },
  152. base64: function(data) {
  153. var that = this;
  154. var img = document.getElementById("im");
  155. var canvas = document.createElement("canvas");
  156. //像素
  157. canvas.height = 400;
  158. canvas.width = 400;
  159. var bx = data.x;
  160. var by = data.y;
  161. var ctx = canvas.getContext("2d");
  162. ctx.drawImage(img, bx, by, data.width, data.height, 0, 0, 400, 400);
  163. var dataURL = canvas.toDataURL("image/jpeg", 0.5); //第二个参数是质量
  164. return dataURL;
  165. }
  166. });
  167. var cro = new Cro();
  168. c.plusReady(function() {
  169. cro.onReady();
  170. })
  171. })(mui)
  172. </script>
  173. </body>
  174. </html>