碧桂园
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

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