image-cropper.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <template>
  2. <view class="content">
  3. <view class="cropper-wrapper" :style="{ height: cropperOpt.height + 'px' }">
  4. <canvas
  5. class="cropper"
  6. :disable-scroll="true"
  7. @touchstart="touchStart"
  8. @touchmove="touchMove"
  9. @touchend="touchEnd"
  10. :style="{ width: cropperOpt.width, height: cropperOpt.height, backgroundColor: 'rgba(0, 0, 0, 0.8)' }"
  11. canvas-id="cropper"
  12. id="cropper"
  13. ></canvas>
  14. <canvas
  15. class="cropper"
  16. :disable-scroll="true"
  17. :style="{
  18. position: 'fixed',
  19. top: `-${cropperOpt.width * cropperOpt.pixelRatio}px`,
  20. left: `-${cropperOpt.height * cropperOpt.pixelRatio}px`,
  21. width: `${cropperOpt.width * cropperOpt.pixelRatio}px`,
  22. height: `${cropperOpt.height * cropperOpt.pixelRatio}`
  23. }"
  24. canvas-id="targetId"
  25. id="targetId"
  26. ></canvas>
  27. </view>
  28. <view class="cropper-buttons safe-area-padding" :style="{ height: bottomNavHeight + 'px' }">
  29. <!-- #ifdef H5 -->
  30. <view class="upload" @tap="uploadTap">选择图片</view>
  31. <!-- #endif -->
  32. <!-- #ifndef H5 -->
  33. <view class="upload" @tap="uploadTap">重新选择</view>
  34. <!-- #endif -->
  35. <view class="getCropperImage" @tap="getCropperImage(false)">确定</view>
  36. </view>
  37. </view>
  38. </template>
  39. <script>
  40. import WeCropper from './weCropper.js';
  41. export default {
  42. props: {
  43. // 裁剪矩形框的样式,其中可包含的属性为lineWidth-边框宽度(单位rpx),color: 边框颜色,
  44. // mask-遮罩颜色,一般设置为一个rgba的透明度,如"rgba(0, 0, 0, 0.35)"
  45. boundStyle: {
  46. type: Object,
  47. default() {
  48. return {
  49. lineWidth: 4,
  50. borderColor: 'rgb(245, 245, 245)',
  51. mask: 'rgba(0, 0, 0, 0.35)'
  52. };
  53. }
  54. }
  55. // // 裁剪框宽度,单位rpx
  56. // rectWidth: {
  57. // type: [String, Number],
  58. // default: 400
  59. // },
  60. // // 裁剪框高度,单位rpx
  61. // rectHeight: {
  62. // type: [String, Number],
  63. // default: 400
  64. // },
  65. // // 输出图片宽度,单位rpx
  66. // destWidth: {
  67. // type: [String, Number],
  68. // default: 400
  69. // },
  70. // // 输出图片高度,单位rpx
  71. // destHeight: {
  72. // type: [String, Number],
  73. // default: 400
  74. // },
  75. // // 输出的图片类型,如果发现裁剪的图片很大,可能是因为设置为了"png",改成"jpg"即可
  76. // fileType: {
  77. // type: String,
  78. // default: 'jpg',
  79. // },
  80. // // 生成的图片质量
  81. // // H5上无效,目前不考虑使用此参数
  82. // quality: {
  83. // type: [Number, String],
  84. // default: 1
  85. // }
  86. },
  87. data() {
  88. return {
  89. // 底部导航的高度
  90. bottomNavHeight: 50,
  91. originWidth: 200,
  92. width: 0,
  93. height: 0,
  94. cropperOpt: {
  95. id: 'cropper',
  96. targetId: 'targetCropper',
  97. pixelRatio: 1,
  98. width: 0,
  99. height: 0,
  100. scale: 2.5,
  101. zoom: 8,
  102. cut: {
  103. x: (this.width - this.originWidth) / 2,
  104. y: (this.height - this.originWidth) / 2,
  105. width: this.originWidth,
  106. height: this.originWidth
  107. },
  108. boundStyle: {
  109. lineWidth: uni.upx2px(this.boundStyle.lineWidth),
  110. mask: this.boundStyle.mask,
  111. color: this.boundStyle.borderColor
  112. }
  113. },
  114. // 裁剪框和输出图片的尺寸,高度默认等于宽度
  115. // 输出图片宽度,单位px
  116. destWidth: 200,
  117. //输出图片高低,单位px
  118. destHeight:200,
  119. // 裁剪框宽度,单位px
  120. rectWidth: 200,
  121. // 输出的图片类型,如果'png'类型发现裁剪的图片太大,改成"jpg"即可
  122. fileType: 'jpg',
  123. src: '', // 选择的图片路径,用于在点击确定时,判断是否选择了图片
  124. };
  125. },
  126. onLoad(option) {
  127. let rectInfo = uni.getSystemInfoSync();
  128. this.width = rectInfo.windowWidth;
  129. this.height = rectInfo.windowHeight - this.bottomNavHeight;
  130. this.cropperOpt.width = this.width;
  131. this.cropperOpt.height = this.height;
  132. this.cropperOpt.pixelRatio = rectInfo.pixelRatio;
  133. if (option.destWidth) this.destWidth = option.destWidth;
  134. if (option.destHeight) this.destHeight = option.destHeight;
  135. if (option.rectWidth && option.rectHeight) {
  136. let rectWidth = Number(option.rectWidth);
  137. let rectHeight = Number(option.rectHeight)
  138. this.cropperOpt.cut = {
  139. x: (this.width - rectWidth) / 2,
  140. y: (this.height - rectWidth) / 2,
  141. width: rectWidth,
  142. height: rectHeight
  143. };
  144. }
  145. this.rectWidth = option.rectWidth;
  146. if (option.fileType) this.fileType = option.fileType;
  147. // 初始化
  148. this.cropper = new WeCropper(this.cropperOpt)
  149. .on('ready', ctx => {
  150. // wecropper is ready for work!
  151. })
  152. .on('beforeImageLoad', ctx => {
  153. // before picture loaded, i can do something
  154. })
  155. .on('imageLoad', ctx => {
  156. // picture loaded
  157. })
  158. .on('beforeDraw', (ctx, instance) => {
  159. // before canvas draw,i can do something
  160. });
  161. // 设置导航栏样式,以免用户在page.json中没有设置为黑色背景
  162. uni.setNavigationBarColor({
  163. frontColor: '#ffffff',
  164. backgroundColor: '#000000'
  165. });
  166. uni.chooseImage({
  167. count: 1, // 默认9
  168. sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有
  169. sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
  170. success: res => {
  171. this.src = res.tempFilePaths[0];
  172. // 获取裁剪图片资源后,给data添加src属性及其值
  173. this.cropper.pushOrign(this.src);
  174. }
  175. });
  176. },
  177. methods: {
  178. touchStart(e) {
  179. this.cropper.touchStart(e);
  180. },
  181. touchMove(e) {
  182. this.cropper.touchMove(e);
  183. },
  184. touchEnd(e) {
  185. this.cropper.touchEnd(e);
  186. },
  187. getCropperImage(isPre = false) {
  188. if(!this.src) return this.$u.toast('请先选择图片再裁剪');
  189. let cropper_opt = {
  190. destHeight: Number(this.destHeight), // uni.canvasToTempFilePath要求这些参数为数值
  191. destWidth: Number(this.destWidth),
  192. fileType: this.fileType
  193. };
  194. this.cropper.getCropperImage(cropper_opt, (path, err) => {
  195. if (err) {
  196. uni.showModal({
  197. title: '温馨提示',
  198. content: err.message
  199. });
  200. } else {
  201. if (isPre) {
  202. uni.previewImage({
  203. current: '', // 当前显示图片的 http 链接
  204. urls: [path] // 需要预览的图片 http 链接列表
  205. });
  206. } else {
  207. uni.$emit('uAvatarCropper', path);
  208. this.$u.route({
  209. type: 'back'
  210. });
  211. }
  212. }
  213. });
  214. },
  215. uploadTap() {
  216. const self = this;
  217. uni.chooseImage({
  218. count: 1, // 默认9
  219. sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
  220. sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
  221. success: (res) => {
  222. self.src = res.tempFilePaths[0];
  223. // 获取裁剪图片资源后,给data添加src属性及其值
  224. self.cropper.pushOrign(this.src);
  225. }
  226. });
  227. }
  228. }
  229. };
  230. </script>
  231. <style scoped lang="scss">
  232. @import '/style.components.scss';
  233. .content {
  234. background: rgba(255, 255, 255, 1);
  235. }
  236. .cropper {
  237. position: absolute;
  238. top: 0;
  239. left: 0;
  240. width: 100%;
  241. height: 100%;
  242. z-index: 11;
  243. }
  244. .cropper-buttons {
  245. background-color: #000000;
  246. color: #eee;
  247. }
  248. .cropper-wrapper {
  249. position: relative;
  250. @include vue-flex;
  251. flex-direction: row;
  252. justify-content: space-between;
  253. align-items: center;
  254. width: 100%;
  255. background-color: #000;
  256. }
  257. .cropper-buttons {
  258. width: 100vw;
  259. @include vue-flex;
  260. flex-direction: row;
  261. justify-content: space-between;
  262. align-items: center;
  263. position: fixed;
  264. bottom: 0;
  265. left: 0;
  266. font-size: 28rpx;
  267. }
  268. .cropper-buttons .upload,
  269. .cropper-buttons .getCropperImage {
  270. width: 50%;
  271. text-align: center;
  272. }
  273. .cropper-buttons .upload {
  274. text-align: left;
  275. padding-left: 50rpx;
  276. }
  277. .cropper-buttons .getCropperImage {
  278. text-align: right;
  279. padding-right: 50rpx;
  280. }
  281. </style>