碧桂园
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

285 rader
9.9 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: 'a9abd99b2dfb3a5bee964cbb0bced0ae', // EasyAR开发中心 - API KEY - API Key
  18. apiSecret: '483e000a38265687c058efb7ab66cbc0917a3331b6dbf5482df46c76cb749529', // EasyAR开发中心 - API KEY - API Secret
  19. crsAppId: '8911f4b8f03725e300ca4130c01f27e9', // EasyAR开发中心 - 云服务 - 云识别管理 - 云识别库信息 - CRS AppId
  20. token: '',
  21. clientHost: 'https://bd9b3490fe845642e600cf53f0cd64b8.cn1.crs.easyar.com:8443', //服务器一般不变
  22. jpegQuality: 0.7, //JPEG压缩质量,建议不低于70%
  23. minInterval: 2000, //最短的两次CRS请求间隔
  24. },
  25. //识别到这个数组中的ID就触发内容
  26. targetIds: [
  27. "6c27e0a1-e4ca-424d-9062-4c8ec676a5ad",
  28. "ec72a9a2-20d5-4b86-82db-05f29e1c0fbb",
  29. "f0bdba05-e610-4d1c-811a-5034ccc0d1f7",
  30. "79014ee9-60c7-4a0c-81ed-0d5889a7ecc9"
  31. ],
  32. showLoading: false,
  33. showLoadingText: "",
  34. },
  35. /** @type {CameraFrameListener} 相机帧回调 */
  36. listener: undefined,
  37. /** @type {HTMLCanvasElement} canvas对象 */
  38. canvas: undefined,
  39. /** @type {boolean} 是否需要持续识别,在点击“识别体验”之后和识别成功之前为true */
  40. runningCrs: undefined,
  41. /** @type {boolean} 当前是否正在进行CRS请求 */
  42. busy: undefined,
  43. /** @type {CrsClient} 负责发起CRS请求的对象 */
  44. crsClient: undefined,
  45. /** @type {number} 最后一次CRS请求的事件,用于判断是否满足最短请求间隔 */
  46. last: undefined,
  47. onLoad: function () {
  48. this.daojishi() // 默认进入页面10S后退出
  49. },
  50. onReady: function () {
  51. if (systemInfo.platform === "devtools") { //开发工具不会触发initdone事件,于是在onReady手动触发
  52. this.onCameraInit();
  53. }
  54. // 获取token
  55. this.queryToken().then(msg => {
  56. console.log('token1')
  57. console.log(msg)
  58. this.data.config.token = msg;
  59. }).catch(err => {
  60. console.info(err);
  61. });
  62. },
  63. daojishi(){
  64. var t = 10;
  65. var timer = setInterval(function(){
  66. t--;
  67. if(t<4){
  68. wx.showToast({
  69. icon: 'none',
  70. title: '扫描时间过长,请退出后重新扫描!',
  71. })
  72. }
  73. if(t<0){
  74. clearInterval(timer);
  75. wx.navigateBack({
  76. delta: 1,
  77. })
  78. }
  79. }, 1000)
  80. },
  81. showLoading(text) {
  82. this.setData({
  83. showLoading: true,
  84. showLoadingText: text,
  85. });
  86. },
  87. hideLoading() {
  88. this.setData({
  89. showLoading: false,
  90. });
  91. },
  92. //图像识别部分:
  93. onShow: function () {
  94. if (this.listener) this.listener.start(); //页面隐藏时相机帧的监听会自动停止,但恢复展示时不会自动启动,这里手动启动
  95. this.scan()
  96. },
  97. scan: function () {
  98. this.runningCrs = true;
  99. this.setData({
  100. showOverlay: false,
  101. showContent: false,
  102. selectType: SELECT_TYPE.NONE,
  103. });
  104. this.showLoading("识别中");
  105. },
  106. onCameraInit: function () {
  107. //找到canvas对象
  108. const query = wx.createSelectorQuery();
  109. query.select('#capture')
  110. .fields({node: true})
  111. .exec((res) => {
  112. const canvas = res[0].node;
  113. //设置canvas内部尺寸为480*640,frame-size="medium"的设置下相机帧大多是480*640
  114. canvas.width = 480;
  115. canvas.height = 640;
  116. this.canvas = canvas;
  117. this.crsClient = new CrsClient(this.data.config, this.canvas);
  118. //开始监听相机帧
  119. let cameraContext = wx.createCameraContext();
  120. this.listener = cameraContext.onCameraFrame(frame => {
  121. if (!this.canvas) return;
  122. let canvas = this.canvas;
  123. //如果尺寸不匹配,就修改canvas尺寸以适应相机帧
  124. if (canvas.width !== frame.width || canvas.height !== frame.height) {
  125. canvas.width = frame.width;
  126. canvas.height = frame.height;
  127. }
  128. this.queryImage(frame);
  129. });
  130. this.listener.start();
  131. });
  132. },
  133. queryImage: function (frame) {
  134. if (!this.runningCrs || this.busy || !this.crsClient) return;
  135. //最短的两次CRS请求间隔
  136. let now = new Date().getTime();
  137. if (this.last && (now - this.last < this.data.config.minInterval)) return;
  138. this.last = now;
  139. this.busy = true; //如果正在进行CRS请求,就不允许再次请求
  140. this.crsClient.queryImage(frame).then(res => {
  141. if (!this.runningCrs) return; //避免在停止后仍然触发
  142. let result = res.result;
  143. if (!result) return;
  144. if (result.target) {
  145. console.log("识别成功");
  146. this.runningCrs = false;
  147. this.hideLoading();
  148. let base64 = result.target.meta
  149. let web = this.base64_decode(base64)
  150. // 解析跳转链接
  151. wx.navigateTo({
  152. url: '../out/index?web=' + web + '&index=2',
  153. })
  154. // todo: 解析meta中的信息,触发业务逻辑
  155. //如果待触发的id列表中存在识别到的这个id,就触发
  156. if (this.data.targetIds.find(targetId => targetId === result.target.meta)) {
  157. this.onResult(result.target.meta);
  158. }
  159. } else {
  160. console.log("识别失败");
  161. }
  162. this.busy = false;
  163. }).catch(e => {
  164. this.busy = false;
  165. }); //小程序iOS端不支持finally,所以在then和catch里分别设置busy = false
  166. },
  167. onResult: function (target) {
  168. console.log("触发内容!");
  169. console.log(target);
  170. if (target) {
  171. console.log("meta base64:", target);
  172. }
  173. this.setData({
  174. showOverlay: false,
  175. showContent: true,
  176. selectType: SELECT_TYPE.IMAGE,
  177. });
  178. },
  179. //界面:
  180. back: function () {
  181. this.runningCrs = false;
  182. this.setData({
  183. showOverlay: true,
  184. showContent: false,
  185. selectType: SELECT_TYPE.NONE,
  186. });
  187. this.hideLoading();
  188. },
  189. experience: function () {
  190. this.setData({
  191. showOverlay: false,
  192. showContent: true,
  193. selectType: SELECT_TYPE.IMAGE,
  194. });
  195. },
  196. download: function () {
  197. wx.saveImageToPhotosAlbum({
  198. filePath: "/images/namecard.jpg",
  199. success: res => {
  200. wx.showToast({title: "已保存到相册", icon: "none"});
  201. },
  202. fail: res => {
  203. wx.showToast({title: "保存失败", icon: "none"});
  204. },
  205. });
  206. },
  207. selectContent: function (e) {
  208. this.setData({
  209. selectType: e.currentTarget.dataset.contenttype,
  210. });
  211. },
  212. queryToken: function() { // 获取token
  213. return new Promise((resolve, reject) => {
  214. wx.request({
  215. url: 'https://cktest.2weisou.com/meta/ar/ar/getToken',
  216. method: 'get',
  217. data: {},
  218. header: {
  219. 'content-type': 'application/json'
  220. },
  221. success: res => resolve(res.data),
  222. fail: err => reject(err)
  223. });
  224. });
  225. },
  226. base64_decode(input) { // 解码,配合decodeURIComponent使用
  227. var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  228. var output = "";
  229. var chr1, chr2, chr3;
  230. var enc1, enc2, enc3, enc4;
  231. var i = 0;
  232. input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
  233. while (i < input.length) {
  234. enc1 = base64EncodeChars.indexOf(input.charAt(i++));
  235. enc2 = base64EncodeChars.indexOf(input.charAt(i++));
  236. enc3 = base64EncodeChars.indexOf(input.charAt(i++));
  237. enc4 = base64EncodeChars.indexOf(input.charAt(i++));
  238. chr1 = (enc1 << 2) | (enc2 >> 4);
  239. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  240. chr3 = ((enc3 & 3) << 6) | enc4;
  241. output = output + String.fromCharCode(chr1);
  242. if (enc3 != 64) {
  243. output = output + String.fromCharCode(chr2);
  244. }
  245. if (enc4 != 64) {
  246. output = output + String.fromCharCode(chr3);
  247. }
  248. }
  249. return this.utf8_decode(output);
  250. },
  251. utf8_decode(utftext) { // utf-8解码
  252. var string = '';
  253. let i = 0;
  254. let c = 0;
  255. let c1 = 0;
  256. let c2 = 0;
  257. while (i < utftext.length) {
  258. c = utftext.charCodeAt(i);
  259. if (c < 128) {
  260. string += String.fromCharCode(c);
  261. i++;
  262. } else if ((c > 191) && (c < 224)) {
  263. c1 = utftext.charCodeAt(i + 1);
  264. string += String.fromCharCode(((c & 31) << 6) | (c1 & 63));
  265. i += 2;
  266. } else {
  267. c1 = utftext.charCodeAt(i + 1);
  268. c2 = utftext.charCodeAt(i + 2);
  269. string += String.fromCharCode(((c & 15) << 12) | ((c1 & 63) << 6) | (c2 & 63));
  270. i += 3;
  271. }
  272. }
  273. return string;
  274. }
  275. });