当前位置:   article > 正文

Hbuilder-二维码或条形码扫描

hbuilder pda条形码

使用cordova可以实现扫描二维码或者条形码的功能,但是环境配置比较复杂,需要额外安装插件。

采用html5+同样也可以实现二维码扫描功能,配合Hbuilder打包(必须),方便快捷,并且还可以修改扫描框的样式,更强的灵活度。实现方法如下:

新建2个html页面,一个作为页面的展示,一个用作扫描二维码界面

作为页面展示的index.html页面

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
  6. <meta name="HandheldFriendly" content="true" />
  7. <meta name="MobileOptimized" content="320" />
  8. <title>Hello H5+</title>
  9. <script type="text/javascript" src="js/common.js"></script>
  10. <script type="text/javascript">
  11. function scaned(t, r, f) {
  12. var inputContent = document.getElementById("input");
  13. inputContent.value = r;
  14. }
  15. </script>
  16. <style type="text/css" media="screen">
  17. .hdata {
  18. color: #e1673e;
  19. font-size: 14px;
  20. overflow: hidden;
  21. text-overflow: ellipsis;
  22. white-space: nowrap;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <div id="dcontent" class="dcontent">
  28. <div class="button" onclick="clicked('test1.html',true,true)">扫一扫</div>
  29. <input type="text" id="input" />
  30. </div>
  31. </body>
  32. </html>

common.js是用Hbuilder新建移动app hello html5+项目时生成的

扫描二维码页面test1.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
  6. <meta name="HandheldFriendly" content="true" />
  7. <meta name="MobileOptimized" content="320" />
  8. <title>Hello H5+</title>
  9. <script type="text/javascript" src="js/common.js"></script>
  10. <script type="text/javascript">
  11. var ws = null,
  12. wo = null;
  13. var scan = null,
  14. domready = false;
  15. // H5 plus事件处理
  16. function plusReady() {
  17. if(ws || !window.plus || !domready) {
  18. return;
  19. }
  20. // 获取窗口对象
  21. ws = plus.webview.currentWebview();
  22. wo = ws.opener();
  23. // 开始扫描
  24. ws.addEventListener('show', function() {
  25. var styles = {
  26. frameColor: "#65e102",
  27. scanbarColor: "#b7e871",
  28. background: "#000"
  29. }; //边框属性
  30. var filter; //扫码格式 空为全类型,不能省略
  31. scan = new plus.barcode.Barcode('bcid', filter, styles);
  32. scan.onmarked = onmarked;
  33. scan.start({
  34. conserve: true,
  35. filename: '_doc/barcode/'
  36. });
  37. }, false);
  38. // 显示页面并关闭等待框
  39. ws.show('pop-in');
  40. wo.evalJS('closeWaiting()');
  41. }
  42. if(window.plus) {
  43. plusReady();
  44. } else {
  45. document.addEventListener('plusready', plusReady, false);
  46. }
  47. // 监听DOMContentLoaded事件
  48. document.addEventListener('DOMContentLoaded', function() {
  49. domready = true;
  50. plusReady();
  51. }, false);
  52. // 二维码扫描成功
  53. function onmarked(type, result, file) {
  54. switch(type) {
  55. case plus.barcode.QR:
  56. type = 'QR';
  57. break;
  58. case plus.barcode.EAN13:
  59. type = 'EAN13';
  60. break;
  61. case plus.barcode.EAN8:
  62. type = 'EAN8';
  63. break;
  64. default:
  65. type = '其它' + type;
  66. break;
  67. }
  68. result = result.replace(/\n/g, '');
  69. wo.evalJS("scaned('" + type + "','" + result + "','" + file + "');");
  70. back();
  71. }
  72. // 从相册中选择二维码图片
  73. function scanPicture() {
  74. plus.gallery.pick(function(path) {
  75. plus.barcode.scan(path, onmarked, function(error) {
  76. plus.nativeUI.alert('无法识别此图片');
  77. });
  78. }, function(err) {
  79. console.log('Failed: ' + err.message);
  80. });
  81. }
  82. </script>
  83. <style type="text/css">
  84. #bcid {
  85. width: 100%;
  86. position: absolute;
  87. top: 0px;
  88. bottom: 44px;
  89. text-align: center;
  90. }
  91. .tip {
  92. color: #FFFFFF;
  93. font-weight: bold;
  94. text-shadow: 0px -1px #103E5C;
  95. }
  96. footer {
  97. width: 100%;
  98. height: 44px;
  99. position: absolute;
  100. bottom: 0px;
  101. line-height: 44px;
  102. text-align: center;
  103. color: #FFF;
  104. }
  105. .fbt {
  106. width: 50%;
  107. height: 100%;
  108. background-color: #FFCC33;
  109. float: left;
  110. }
  111. .fbt:active {
  112. -webkit-box-shadow: inset 0 3px 5px rgba(0, 0, 0, 0.5);
  113. box-shadow: inset 0 3px 5px rgba(0, 0, 0, 0.5);
  114. }
  115. </style>
  116. </head>
  117. <body style="background-color: #000000;">
  118. <div id="bcid">
  119. <div style="height:40%"></div>
  120. <p class="tip">...载入中...</p>
  121. </div>
  122. <footer>
  123. <div class="fbt" onclick="back()">取  消</div>
  124. <div class="fbt" onclick="scanPicture()">从相册选择二维码</div>
  125. </footer>
  126. </body>
  127. </html>

设置扫描框特定样式主要用到的代码,默认是红色的

var styles = {
                        frameColor: "#65e102",
                        scanbarColor: "#b7e871",
                        background: "#000"
                    }; //边框属性
var filter; //扫码格式 空为全类型,不能省略
scan = new plus.barcode.Barcode('bcid', filter, styles);

转载于:https://my.oschina.net/u/2612473/blog/1625149

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/854709
推荐阅读
相关标签
  

闽ICP备14008679号