uniapp中应用H5自定义二维码扫码界面

旧城等待, 2022-12-05 12:25 2189阅读 0赞

uniapp中应用H5自定义二维码扫码界面

  • 最终效果
  • pages配置
  • 组件代码

最终效果

效果图

pages配置

  1. {
  2. "path": "components/barcode/scan-page",
  3. "style": {
  4. "navigationBarTitleText": "扫一扫"
  5. }
  6. }

组件代码

  1. <template>
  2. <view>
  3. <!-- 扫码页面 -->
  4. <!-- #ifndef APP-PLUS -->
  5. <view class="wrap">
  6. <view class="u-tips-color">
  7. 请在app中打开
  8. </view>
  9. </view>
  10. <!-- #endif -->
  11. </view>
  12. </template>
  13. <script>
  14. export default {
  15. data() {
  16. return {
  17. barcode: null,
  18. flash: false,
  19. tip: '将二维码放入框中,即可自动扫描',
  20. }
  21. },
  22. onShow() {
  23. // 页面展示时,重新启动扫描检测
  24. if (this.barcode) {
  25. this.barcode.start()
  26. }
  27. },
  28. onLoad(params) {
  29. const {
  30. tip
  31. } = params
  32. if (tip) {
  33. this.tip = tip
  34. }
  35. // #ifdef APP-PLUS
  36. plus.navigator.setFullscreen(true); //全屏
  37. let currentWebview = this.$scope.$getAppWebview();
  38. this.createBarcode(currentWebview)
  39. this.createTipInfoView(currentWebview)
  40. this.createFlashBarView(currentWebview)
  41. // #endif
  42. },
  43. mounted() {
  44. },
  45. methods: {
  46. /** * 创建二维码 * @param {Object} currentWebview */
  47. createBarcode(currentWebview) {
  48. if (!this.barcode) {
  49. this.barcode = plus.barcode.create('barcode', [plus.barcode.QR], {
  50. top: `0px`,
  51. left: '0px',
  52. height: `100%`,
  53. width: '100%',
  54. position: 'absolute',
  55. background: '#FFCC00',
  56. frameColor: '#FFCC33',
  57. scanbarColor: '#FFCC33',
  58. });
  59. this.barcode.onmarked = this.onmarked;
  60. this.barcode.setFlash(this.flash);
  61. //此处未演示扫码成功回调的地址设置,实际请参考HTML5Plus API自行处理
  62. //注意扫码区域需为正方形,否则影响扫码识别率
  63. currentWebview.append(this.barcode);
  64. }
  65. this.barcode.start()
  66. },
  67. /** * 创建提示信息 * @param {Object} currentWebview */
  68. createTipInfoView(currentWebview) {
  69. const content = new plus.nativeObj.View('content', {
  70. top: '0px',
  71. left: '0px',
  72. height: '100%',
  73. width: '100%'
  74. },
  75. [{
  76. tag: 'font',
  77. id: 'scanTips',
  78. text: this.tip,
  79. textStyles: {
  80. size: '14px',
  81. color: '#ffffff',
  82. whiteSpace: 'normal'
  83. },
  84. position: {
  85. top: '90px',
  86. left: '10%',
  87. width: '80%',
  88. height: 'wrap_content'
  89. }
  90. }]);
  91. currentWebview.append(content);
  92. },
  93. // 创建 开关灯按钮
  94. createFlashBarView(currentWebview) {
  95. const openImg = this.crtFlashImg('static/yellow-scanBar.png')
  96. const closeImg = this.crtFlashImg('static/scanBar.png')
  97. const scanBarVew = new plus.nativeObj.View('scanBarVew', {
  98. top: '65%',
  99. left: '40%',
  100. height: '10%',
  101. width: '20%',
  102. },
  103. closeImg);
  104. scanBarVew.interceptTouchEvent(true);
  105. currentWebview.append(scanBarVew);
  106. scanBarVew.addEventListener("click", (e) => { //点亮手电筒
  107. this.flash = !this.flash;
  108. if (this.flash) {
  109. scanBarVew.draw(openImg);
  110. } else {
  111. scanBarVew.draw(closeImg)
  112. }
  113. if (this.barcode) {
  114. this.barcode.setFlash(this.flash);
  115. }
  116. }, false)
  117. },
  118. crtFlashImg(imgsrc) {
  119. return [{
  120. tag: 'img',
  121. id: 'scanBar',
  122. src: imgsrc,
  123. position: {
  124. width: '28%',
  125. left: '36%',
  126. height: '30%'
  127. }
  128. }, {
  129. tag: 'font',
  130. id: 'font',
  131. text: '轻触照亮',
  132. textStyles: {
  133. size: '10px',
  134. color: '#ffffff'
  135. },
  136. position: {
  137. width: '80%',
  138. left: '10%'
  139. }
  140. }]
  141. },
  142. // 扫码成功回调
  143. onmarked(type, result) {
  144. console.log('条码类型:' + type);
  145. console.log('条码内容:' + result);
  146. // 业务代码
  147. // 核对扫描结果
  148. // 判断是否是正确的格式
  149. // 不正确则跳转到 错误页面
  150. }
  151. }
  152. }
  153. </script>
  154. <style scoped>
  155. .wrap {
  156. height: calc(100vh);
  157. /* #ifdef H5 */
  158. height: calc(100vh - var(--window-top));
  159. /* #endif */
  160. display: flex;
  161. flex-direction: column;
  162. justify-content: center;
  163. align-items: center;
  164. }
  165. </style>

参考资料
h5 barcode

发表评论

表情:
评论列表 (有 0 条评论,2189人围观)

还没有评论,来说两句吧...

相关阅读