碧桂园
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

246 righe
7.7 KiB

  1. import {CrsClient} from "./crsClient";
  2. import {CryptoJS} from "./CryptoJS";
  3. const systemInfo = wx.getSystemInfoSync();
  4. const SELECT_TYPE = {
  5. NONE: 0,
  6. IMAGE: 1,
  7. VIDEO: 2,
  8. };
  9. Page({
  10. data: {
  11. showOverlay: true,
  12. showSelect: false,
  13. SELECT_TYPE: SELECT_TYPE,
  14. selectType: 0,
  15. //CRS配置
  16. config: {
  17. apiKey: 'b645a18f1e53b3bf1b22253748bc1672', // EasyAR开发中心 - API KEY - API Key
  18. apiSecret: '701b6eadaf58da17c31b57ae4def08ff8ecee63537e97fd3866454c8efe1b30c', // EasyAR开发中心 - API KEY - API Secret
  19. crsAppId: '20239cb7aa12abd58af820458711fea0', // EasyAR开发中心 - 云服务 - 云识别管理 - 云识别库信息 - CRS AppId
  20. token: '',
  21. clientHost: 'https://6db69340022abbad5fa30787b96a3d4b.cn1.crs.easyar.com:8080', //服务器一般不变
  22. jpegQuality: 0.7, //JPEG压缩质量,建议不低于70%
  23. minInterval: 1000, //最短的两次CRS请求间隔
  24. },
  25. //识别到这个数组中的ID就触发内容
  26. targetIds: [
  27. "TODO 云识别管理 - 某个图库 - 识别图 - 某个识别图的ID",
  28. "4ff97e95-3467-40e9-85df-93b99f58cd3b"
  29. ],
  30. showLoading: false,
  31. showLoadingText: "",
  32. },
  33. /** @type {CameraFrameListener} 相机帧回调 */
  34. listener: undefined,
  35. /** @type {HTMLCanvasElement} canvas对象 */
  36. canvas: undefined,
  37. /** @type {boolean} 是否需要持续识别,在点击“识别体验”之后和识别成功之前为true */
  38. runningCrs: undefined,
  39. /** @type {boolean} 当前是否正在进行CRS请求 */
  40. busy: undefined,
  41. /** @type {CrsClient} 负责发起CRS请求的对象 */
  42. crsClient: undefined,
  43. /** @type {number} 最后一次CRS请求的事件,用于判断是否满足最短请求间隔 */
  44. last: undefined,
  45. onLoad: function () {
  46. let base64 ='aHR0cHM6Ly93d3cuYmFpZHUuY29tLw=='
  47. },
  48. onReady: function () {
  49. if (systemInfo.platform === "devtools") { //开发工具不会触发initdone事件,于是在onReady手动触发
  50. this.onCameraInit();
  51. }
  52. // 获取token
  53. this.queryToken().then(msg => {
  54. this.data.config.token = msg.result.token;
  55. }).catch(err => {
  56. console.info(err);
  57. });
  58. },
  59. showLoading(text) {
  60. this.setData({
  61. showLoading: true,
  62. showLoadingText: text,
  63. });
  64. },
  65. hideLoading() {
  66. this.setData({
  67. showLoading: false,
  68. });
  69. },
  70. //图像识别部分:
  71. onShow: function () {
  72. if (this.listener) this.listener.start(); //页面隐藏时相机帧的监听会自动停止,但恢复展示时不会自动启动,这里手动启动
  73. this.scan()
  74. },
  75. scan: function () {
  76. this.runningCrs = true;
  77. this.setData({
  78. showOverlay: false,
  79. showContent: false,
  80. selectType: SELECT_TYPE.NONE,
  81. });
  82. this.showLoading("识别中");
  83. },
  84. onCameraInit: function () {
  85. //找到canvas对象
  86. const query = wx.createSelectorQuery();
  87. query.select('#capture')
  88. .fields({node: true})
  89. .exec((res) => {
  90. const canvas = res[0].node;
  91. //设置canvas内部尺寸为480*640,frame-size="medium"的设置下相机帧大多是480*640
  92. canvas.width = 480;
  93. canvas.height = 640;
  94. this.canvas = canvas;
  95. this.crsClient = new CrsClient(this.data.config, this.canvas);
  96. //开始监听相机帧
  97. let cameraContext = wx.createCameraContext();
  98. this.listener = cameraContext.onCameraFrame(frame => {
  99. if (!this.canvas) return;
  100. let canvas = this.canvas;
  101. //如果尺寸不匹配,就修改canvas尺寸以适应相机帧
  102. if (canvas.width !== frame.width || canvas.height !== frame.height) {
  103. canvas.width = frame.width;
  104. canvas.height = frame.height;
  105. }
  106. this.queryImage(frame);
  107. });
  108. this.listener.start();
  109. });
  110. },
  111. queryImage: function (frame) {
  112. if (!this.runningCrs || this.busy || !this.crsClient) return;
  113. //最短的两次CRS请求间隔
  114. let now = new Date().getTime();
  115. if (this.last && (now - this.last < this.data.config.minInterval)) return;
  116. this.last = now;
  117. this.busy = true; //如果正在进行CRS请求,就不允许再次请求
  118. this.crsClient.queryImage(frame).then(res => {
  119. console.log(123);
  120. console.log(frame);
  121. if (!this.runningCrs) return; //避免在停止后仍然触发
  122. let result = res && res.result;
  123. if (!result) return;
  124. if (result.target) {
  125. console.log("识别成功", result.target.targetId);
  126. this.runningCrs = false;
  127. this.hideLoading();
  128. // todo: 解析meta中的信息,触发业务逻辑
  129. //如果待触发的id列表中存在识别到的这个id,就触发
  130. if (this.data.targetIds.find(targetId => targetId === result.target.targetId)) {
  131. this.onResult(result.target);
  132. }
  133. } else {
  134. console.log("识别失败", result.message);
  135. }
  136. this.busy = false;
  137. }).catch(e => {
  138. this.busy = false;
  139. console.log(e);
  140. }); //小程序iOS端不支持finally,所以在then和catch里分别设置busy = false
  141. },
  142. onResult: function (target) {
  143. console.log("触发内容!");
  144. if (target.meta) {
  145. console.log("meta base64:", target.meta);
  146. }
  147. this.setData({
  148. showOverlay: false,
  149. showContent: true,
  150. selectType: SELECT_TYPE.IMAGE,
  151. });
  152. },
  153. //界面:
  154. back: function () {
  155. this.runningCrs = false;
  156. this.setData({
  157. showOverlay: true,
  158. showContent: false,
  159. selectType: SELECT_TYPE.NONE,
  160. });
  161. this.hideLoading();
  162. },
  163. experience: function () {
  164. this.setData({
  165. showOverlay: false,
  166. showContent: true,
  167. selectType: SELECT_TYPE.IMAGE,
  168. });
  169. },
  170. download: function () {
  171. wx.saveImageToPhotosAlbum({
  172. filePath: "/images/namecard.jpg",
  173. success: res => {
  174. wx.showToast({title: "已保存到相册", icon: "none"});
  175. },
  176. fail: res => {
  177. wx.showToast({title: "保存失败", icon: "none"});
  178. },
  179. });
  180. },
  181. selectContent: function (e) {
  182. this.setData({
  183. selectType: e.currentTarget.dataset.contenttype,
  184. });
  185. },
  186. /**
  187. * 生成token
  188. */
  189. queryToken: function() {
  190. // const obj = {
  191. // 'apiKey': this.data.config.apiKey,
  192. // "apiSecret":this.data.config.apiSecret
  193. // };
  194. return new Promise((resolve, reject) => {
  195. let that =this
  196. wx.request({
  197. url: 'http://49.232.159.78:6010/ar/getToken',
  198. method: 'get',
  199. // data: obj,
  200. header: {
  201. 'content-type': 'application/json'
  202. },
  203. success: res => {
  204. wx.setStorageSync('token', res.data)
  205. console.log(res,'成功');
  206. },
  207. fail: err =>{
  208. console.log(err,'失败');
  209. }
  210. });
  211. });
  212. },
  213. });