take-image.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. const { isStable } = require('./utils');
  2. const compareVersion = require('../../../utils/compareVersion');
  3. Component({
  4. properties: {
  5. side: {
  6. type: String,
  7. },
  8. },
  9. data: {
  10. isAutoMode: false,
  11. maxTry: 3,
  12. autoCount: 0,
  13. isGyroscopeOn: false,
  14. isTorchOn: false,
  15. disableAlbum: false,
  16. },
  17. attached() {
  18. const { theme, cameraConfig, ocrType } = wx.clientInfo;
  19. const currentVersion = wx.getSystemInfoSync().SDKVersion;
  20. const supportAutoMode = compareVersion(currentVersion, '2.12.2') >= 0;
  21. const isAutoMode = !!(supportAutoMode && cameraConfig && cameraConfig.autoMode);
  22. const maxTry = cameraConfig && cameraConfig.maxTry ? cameraConfig.maxTry : 3;
  23. const disableAlbum = !!(cameraConfig && cameraConfig.disableAlbum);
  24. this.setData({
  25. theme,
  26. isAutoMode,
  27. maxTry,
  28. disableAlbum,
  29. category: ocrType,
  30. });
  31. setTimeout(() => {
  32. this.startAuto();
  33. if (isAutoMode) {
  34. this.barDown = true;
  35. this.barAnimation = wx.createAnimation({
  36. duration: 1500,
  37. delay: 0,
  38. timingFunction: 'linear',
  39. });
  40. this.reAnimation();
  41. };
  42. }, 2000);
  43. },
  44. detached() {
  45. this.setData({
  46. isTorchOn: false,
  47. });
  48. this.endAuto();
  49. },
  50. methods: {
  51. /* 自动识别模式下的动画效果 */
  52. reAnimation() {
  53. const curBarDown = this.barDown;
  54. if (curBarDown) {
  55. this.barAnimation.translateY('390rpx').step();
  56. this.barDown = !curBarDown;
  57. } else {
  58. this.barAnimation.translateY(0).step();
  59. this.barDown = !curBarDown;
  60. }
  61. this.setData({
  62. barMove: this.barAnimation.export(),
  63. });
  64. },
  65. /* 打开陀螺仪,开始自动识别 */
  66. startAuto() {
  67. const { isAutoMode, maxTry, autoCount } = this.data;
  68. if (!isAutoMode) {
  69. return;
  70. };
  71. if (autoCount >= maxTry) {
  72. const self = this;
  73. wx.showModal({
  74. title: '提示',
  75. content: '未能识别到,是否切换到拍照模式?',
  76. confirmText: '切换模式',
  77. success(res) {
  78. if (res.confirm) {
  79. self.setData({
  80. isAutoMode: false,
  81. });
  82. } else if (res.cancel) {
  83. self.restartAuto();
  84. }
  85. },
  86. });
  87. return;
  88. };
  89. wx.startGyroscope({
  90. success: () => {
  91. this.setData({ isGyroscopeOn: true });
  92. },
  93. });
  94. wx.onGyroscopeChange((res) => {
  95. this.autoTake(res);
  96. });
  97. },
  98. /* 自动拍摄 */
  99. async autoTake(res) {
  100. const { autoCount } = this.data;
  101. if (isStable(res)) {
  102. this.endAuto();
  103. this.setData({
  104. autoCount: autoCount + 1,
  105. }, () => {
  106. this.takeImage();
  107. });
  108. };
  109. },
  110. /* 关闭陀螺仪 */
  111. endAuto() {
  112. const { isGyroscopeOn } = this.data;
  113. if (isGyroscopeOn) {
  114. wx.offGyroscopeChange();
  115. wx.stopGyroscope({
  116. success: () => {
  117. this.setData({ isGyroscopeOn: false });
  118. },
  119. });
  120. }
  121. },
  122. /* 重启自动模式 */
  123. restartAuto() {
  124. this.setData({
  125. autoCount: 0,
  126. });
  127. setTimeout(() => {
  128. this.startAuto();
  129. }, 2000);
  130. },
  131. /* 从相册选择照片 */
  132. chooseImage() {
  133. this.endAuto();
  134. this.setData({
  135. isAutoMode: false,
  136. });
  137. const { side } = this.properties;
  138. wx.chooseImage({
  139. count: 1,
  140. sizeType: ['compressed'],
  141. sourceType: ['album'],
  142. success: (res) => {
  143. const detail = {
  144. filePath: res.tempFilePaths[0],
  145. side,
  146. };
  147. this.triggerEvent('onImageReady', detail);
  148. },
  149. });
  150. },
  151. /* 拍摄照片 */
  152. takeImage() {
  153. const { side } = this.properties;
  154. const { isAutoMode } = this.data;
  155. const ctx = wx.createCameraContext();
  156. ctx.takePhoto({
  157. quality: 'normal',
  158. success: (res) => {
  159. /* 通知页面图片已准备好 */
  160. const detail = {
  161. filePath: res.tempImagePath,
  162. side,
  163. isAuto: isAutoMode,
  164. };
  165. this.triggerEvent('onImageReady', detail);
  166. },
  167. });
  168. },
  169. /* 手电筒开关 */
  170. toggleTorch() {
  171. if (wx.canIUse('camera.flash.torch')) {
  172. const { isTorchOn } = this.data;
  173. this.setData({
  174. isTorchOn: !isTorchOn,
  175. });
  176. } else {
  177. wx.showToast({
  178. title: '该版本暂时不支持手电筒功能',
  179. icon: 'none',
  180. duration: 3000,
  181. });
  182. }
  183. },
  184. },
  185. });