You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

288 lines
7.1 KiB

1 year ago
  1. // pagesA/selectFace/selectFace.js
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. cameraStatus:false,
  8. imgSrc:'',
  9. screenW: '',
  10. width: '',
  11. height: '',
  12. distance: '',
  13. old_width: '',
  14. old_height: '',
  15. cut_: '',
  16. x: 0,
  17. y: 0,
  18. crop_pic:''
  19. },
  20. /**
  21. * 生命周期函数--监听页面加载
  22. */
  23. onLoad: function (options) {
  24. const _this = this
  25. wx.getSetting({
  26. success: res => {
  27. if (res.authSetting['scope.camera']) {
  28. _this.setData({
  29. cameraStatus: true
  30. })
  31. } else {
  32. // 用户还没有授权,向用户发起授权请求
  33. wx.authorize({
  34. scope: 'scope.camera',
  35. success() { // 用户同意授权
  36. _this.setData({
  37. cameraStatus: true
  38. })
  39. },
  40. fail() { // 用户不同意授权
  41. _this.openSetting().then(res => {
  42. _this.setData({
  43. cameraStatus: true
  44. })
  45. })
  46. }
  47. })
  48. }
  49. },
  50. fail: res => {
  51. console.log('获取用户授权信息失败')
  52. }
  53. })
  54. wx.getSystemInfo({
  55. success: res => {
  56. const screenH = res.screenHeight,
  57. screenW = res.screenWidth,
  58. cut_ = screenW - 4;
  59. let that = this;
  60. that.setData({
  61. screenW: screenW,
  62. cut_: cut_
  63. })
  64. },
  65. })
  66. },
  67. // 打开授权设置界面
  68. openSetting() {
  69. const _this = this
  70. let promise = new Promise((resolve, reject) => {
  71. wx.showModal({
  72. title: '授权',
  73. content: '请先授权获取摄像头权限',
  74. success(res) {
  75. if (res.confirm) {
  76. wx.openSetting({
  77. success(res) {
  78. if (res.authSetting['scope.camera']) { // 用户打开了授权开关
  79. resolve(true)
  80. } else {
  81. // 用户没有打开授权开关, 继续打开设置页面
  82. _this.openSetting().then(res => {
  83. resolve(true)
  84. })
  85. }
  86. },
  87. fail(res) {
  88. console.log(res)
  89. }
  90. })
  91. } else if (res.cancel) {
  92. _this.openSetting().then(res => {
  93. resolve(true)
  94. })
  95. }
  96. }
  97. })
  98. })
  99. return promise;
  100. },
  101. // 拍照
  102. takePhoto() {
  103. var that=this,
  104. width = that.data.screenW,
  105. height = that.data.screenW;
  106. wx.chooseImage({
  107. count:1,
  108. sizeType: ['original', 'compressed'],
  109. sourceType: ['album', 'camera'],
  110. success:function(res){
  111. const tempFilePaths = res.tempFilePaths[0];
  112. wx.getImageInfo({
  113. src: tempFilePaths,
  114. success: function(msg) {
  115. let img_w = msg.width,
  116. img_h = msg.height,
  117. scale = img_w / img_h
  118. //横向图片,宽不变
  119. //横向图片,高变成固定,宽度自适应
  120. if (scale > 1) {
  121. that.setData({
  122. width: scale * width,
  123. height: height,
  124. old_width: scale * width,
  125. old_height: height,
  126. imgSrc: tempFilePaths
  127. })
  128. } else { //纵向图片,短边是宽,宽变成系统固定,高自适应
  129. that.setData({
  130. width: width,
  131. height: height / scale,
  132. old_width: width,
  133. old_height: height / scale,
  134. imgSrc: tempFilePaths
  135. })
  136. }
  137. }
  138. })
  139. },
  140. fail:function(error){
  141. console.error("调用本地相册文件时出错")
  142. console.warn(error)
  143. },
  144. complete:function(back){
  145. console.log(back)
  146. }
  147. })
  148. },
  149. start: function(res) {
  150. let that = this;
  151. if (res.touches.length == 2) {
  152. let _x = res.touches[1].pageX - res.touches[0].pageX,
  153. _y = res.touches[1].pageY - res.touches[0].pageY,
  154. distance = Math.sqrt(Math.pow(_x, 2) + Math.pow(_y, 2));
  155. that.setData({
  156. distance: distance
  157. })
  158. }
  159. },
  160. move: function(res) {
  161. let that = this;
  162. console.log(res)
  163. if (res.touches.length == 2) {
  164. let _x = res.touches[1].pageX - res.touches[0].pageX,
  165. _y = res.touches[1].pageY - res.touches[0].pageY,
  166. old_width = that.data.old_width,
  167. old_height = that.data.old_height,
  168. newdistance = Math.sqrt(Math.pow(_x, 2) + Math.pow(_y, 2)),
  169. width = that.data.width,
  170. height = that.data.height,
  171. distance = that.data.distance,
  172. end_distance = newdistance - distance,
  173. pic_scale = 1 + end_distance * 0.001;
  174. that.setData({
  175. width: width * pic_scale,
  176. height: height * pic_scale
  177. })
  178. let max = width / old_width;
  179. if (max > 2) {
  180. that.setData({
  181. width: old_width * 2,
  182. height: old_height * 2
  183. })
  184. } else if (max < 1) {
  185. that.setData({
  186. width: old_width,
  187. height: old_height
  188. })
  189. }
  190. }
  191. },
  192. scroll: function(e) {
  193. console.log('TOP:' + e.detail.scrollTop + 'Left:' + e.detail.scrollLeft)
  194. let x = e.detail.scrollLeft,
  195. y = e.detail.scrollTop,
  196. that = this;
  197. that.setData({
  198. x: x,
  199. y: y
  200. })
  201. },
  202. crop: function() {
  203. let that = this,
  204. img = that.data.imgSrc,
  205. width = that.data.width,
  206. height = that.data.height,
  207. crop = that.data.screenW,
  208. x = that.data.x,
  209. y = that.data.y;
  210. wx.showToast({
  211. title: 'loading...',
  212. icon: 'loading',
  213. duration: 1000
  214. })
  215. const canvas = wx.createCanvasContext('canvas');
  216. canvas.drawImage(img, 0, 0, width, height)
  217. canvas.draw(setTimeout(() => {
  218. wx.canvasToTempFilePath({
  219. x: x,
  220. y: y,
  221. width: crop,
  222. height: crop,
  223. canvasId: 'canvas',
  224. success: suc => {
  225. console.log(suc.tempFilePath)
  226. that.setData({
  227. crop_pic: suc.tempFilePath
  228. })
  229. },
  230. fail: err => {
  231. console.log('err:' + err)
  232. }
  233. }, this)
  234. }, 1000))
  235. },
  236. /**
  237. * 生命周期函数--监听页面初次渲染完成
  238. */
  239. onReady: function () {
  240. },
  241. /**
  242. * 生命周期函数--监听页面显示
  243. */
  244. onShow: function () {
  245. },
  246. /**
  247. * 生命周期函数--监听页面隐藏
  248. */
  249. onHide: function () {
  250. },
  251. /**
  252. * 生命周期函数--监听页面卸载
  253. */
  254. onUnload: function () {
  255. },
  256. /**
  257. * 页面相关事件处理函数--监听用户下拉动作
  258. */
  259. onPullDownRefresh: function () {
  260. },
  261. /**
  262. * 页面上拉触底事件的处理函数
  263. */
  264. onReachBottom: function () {
  265. },
  266. /**
  267. * 用户点击右上角分享
  268. */
  269. onShareAppMessage: function () {
  270. }
  271. })