vue项目使用html5+ barcode扫码在苹果遇到的问题以及自己的解决方法

前端开发   发布日期:2025年04月30日   浏览次数:216
 
之前在记录扫码 在安卓时,会出现黑屏,错位,闪退等等问题。解决方法在另一篇文章里 https://www.cnblogs.com/huzhuhua/p/11064764.html 。

当时以为 是解决了。后来打包到IOS上时也是 出现。原因是

plus.webview.create(location.href)这个不是在新的窗口打开,都是在同一窗口。我也不知道什么 原因。
当时以为是路径问题,然后想到了换另一个地址试试。然后记录了另一篇vue引用多入口 文件 https://www.cnblogs.com/huzhuhua/p/11202565.html。
后来还是不行。最终只能新建一个静态的camera.html页面,放在dist打包的文件夹内。一块打包成APP。在调用上我是做了区分,安卓还是照上面 的文章做。IOS的话
就跳到camera.html。
具体代码如下。
要跳转去的VUE页面上
 
  1. let ws = plus.webview.create("./camera.html", "camera");
  2. ws.show();
  3. ws.addEventListener(
  4. "loaded",
  5. function() {
  6. //页面加载完成后才显示
  7. setTimeout(function() {
  8. ws.show();
  9. }, );
  10. },
  11. false
  12. );
  13. ws.addEventListener(
  14. "close",
  15. function() {
  16. ws = null;
  17. },
  18. false
  19. );

camera.html页面上

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>camera</title>
  8. <style>
  9. html,
  10. body,
  11. div,
  12. span,
  13. img {
  14. margin: ;
  15. padding: ;
  16. }
  17. body {
  18. background: #;
  19. }
  20. .tips {
  21. margin-top: %;
  22. color: #fff;
  23. text-align: center
  24. }
  25. .action {
  26. position: fixed;
  27. z-index: ;
  28. width: %;
  29. left: ;
  30. bottom: ;
  31. }
  32. .action .items {
  33. display: flex;
  34. justify-content: space-around;
  35. background: rgba(, , , 0.35);
  36. width: %;
  37. padding: 4px;
  38. margin: 4px auto;
  39. }
  40. .action .items .item {
  41. flex-basis: 50px;
  42. text-align: center;
  43. }
  44. .action .items img {
  45. width: 27px;
  46. }
  47. </style>
  48. </head>
  49. <body>
  50. <div id="camera">
  51. <div id="scan"></div>
  52. <div class="tips">加载中...</div>
  53. <div class="action">
  54. <div class="items">
  55. <div class="item" onclick="openLight"><img src="./src/assets/img/png-60@3x.png"></div>
  56. <div class="item" onclick="getPicture"><img src="./src/assets/img/png-59@3x.png"></div>
  57. <!-- <div
  58. class="item"
  59. @click="showInput"
  60. ><img src="../assets/img/png-68@3x.png">
  61. </div> -->
  62. <div class="item" onclick="cancelScan"><img src="./src/assets/img/png-61@3x.png"></div>
  63. <!-- <d class="item"><img src="../../assets/img/png-25@3x.png"></d -->
  64. </div>
  65. </div>
  66. </div>
  67. </body>
  68. <script>
  69. var isLight = false, scan = null;
  70. // 打开闪光灯
  71. function openLight() {
  72. isLight = !isLight;
  73. scan.setFlash(isLight);
  74. }
  75. //创建扫描控件
  76. function startRecognize() {
  77. if (!window.plus) return;
  78. scan = null;
  79. scan = new plus.barcode.Barcode(
  80. "scan",
  81. [plus.barcode.QR, plus.barcode.EAN8, plus.barcode.EAN13],
  82. {
  83. frameColor: "#1294cb",
  84. scanbarColor: "#1294cb",
  85. top: "100px",
  86. left: "0px",
  87. width: "100%",
  88. height: "500px",
  89. position: "fixed"
  90. }
  91. );
  92. // 条码识别成功
  93. scan.onmarked = onmarked;
  94. function onmarked(type, result, file) {
  95. result = result.replace(/\n/g, "");
  96. localStorage.setItem("cameraData", result);
  97. let ws = plus.webview.getWebviewById("camera");
  98. ws.close()
  99. }
  100. }
  101. // //开始扫描
  102. function startScan() {
  103. if (!window.plus) return;
  104. startRecognize(); //创建控件
  105. setTimeout(() => {
  106. scan.start();
  107. }, );
  108. }
  109. // 取消扫描
  110. function cancelScan() {
  111. if (!window.plus) return;
  112. plus.navigator.setStatusBarStyle("dark");
  113. if (scan) {
  114. scan.cancel(); //关闭扫描
  115. scan.close(); //关闭条码识别控件
  116. }
  117. let ws = plus.webview.getWebviewById("camera");
  118. ws.close()
  119. }
  120. // 从相册选取二维码相片
  121. function getPicture() {
  122. plus.gallery.pick(src => {
  123. // scan.cancel(); //关闭扫描
  124. plus.barcode.scan(
  125. src,
  126. (type, result) => {
  127. scan.cancel(); //关闭扫描
  128. scan.close();
  129. localStorage.setItem("cameraData", result);
  130. plus.navigator.setStatusBarStyle("dark");
  131. let ws = plus.webview.getWebviewById("camera");
  132. ws.close()
  133. }
  134. );
  135. });
  136. }
  137. window.onload = function () {
  138. setTimeout(() => {
  139. plus.navigator.setStatusBarStyle("dark");
  140. startScan()
  141. }, );
  142. }
  143. </script>
  144. </html>

这样算解决了。折腾了N久

以上就是vue项目使用html5+ barcode扫码在苹果遇到的问题以及自己的解决方法的详细内容,更多关于vue项目使用html5+ barcode扫码在苹果遇到的问题以及自己的解决方法的资料请关注九品源码其它相关文章!