HarmonyOS h5拉起系统扫码页面的demo

HarmonyOS h5拉起系统扫码页面的demo。

HarmonyOS
2024-11-05 10:42:39
1630浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Heiang

可参考下述demo进行扫码服务编写,如果是原生调用扫码页面的话,直接调用scan()函数即。

// WebComponent.ets: 
import web_webview from '@ohos.web.webview' 
import { scanCore, scanBarcode } from '@kit.ScanKit'; 
// 导入默认界面需要的日志模块和错误码模块 
import { hilog } from '@kit.PerformanceAnalysisKit'; 
import { BusinessError } from '@kit.BasicServicesKit'; 
 
let options: scanBarcode.ScanOptions = { 
  scanTypes: [scanCore.ScanType.ALL], 
  enableMultiMode: true, 
  enableAlbum: true 
}; 
 
class TestClass { 
  // arkts h5都用await保障执行顺序 
  async scan(): Promise<string> { 
    let scanResult = await scanBarcode.startScanForResult(getContext(this), options) 
    // 收到扫码结果后返回 
    hilog.info(0x0001, '[Scan CPSample]', 'Promise scan result: %{public}s', JSON.stringify(scanResult)); 
    return scanResult.originalValue; 
  } 
} 
 
@Entry 
@Component 
export struct PageWeb { 
  readonly webUrl: string | Resource = 'www.harmonyOS.com' 
  readonly controller: WebviewController = new web_webview.WebviewController() 
  userAgentAlreadySet: boolean = false 
  @State customUserAgent?: string = undefined 
  @State testObj: TestClass = new TestClass(); 
 
  build() { 
    if (this.customUserAgent) { 
      Column() { 
 
 
        Web({ src: $rawfile('index.html'), controller: this.controller }) 
          .domStorageAccess(true) 
          .javaScriptProxy({ 
            object: this.testObj, 
            name: "testObjName", 
            methodList: ["scan"], 
            controller: this.controller 
          }) 
 
      } 
    } 
  } 
 
  aboutToAppear(): void { 
    // 模拟异步构建 customUserAgent 过程 
    setTimeout(() => this.customUserAgent = 'ua/value', 200) 
  } 
 
  applyCustomUserAgent(): void { 
    this.userAgentAlreadySet = true 
    let defaultUserAgent = this.controller.getUserAgent() 
    this.controller.setCustomUserAgent(defaultUserAgent + ' ' + this.customUserAgent) 
    this.controller.loadUrl(this.webUrl) 
  } 
 
  onBackPress(): boolean | void { 
    // 自定义页面返回逻辑 
    // if (this.controller.accessBackward()) { 
    // this.controller.backward() 
    // return true 
    // } 
    return true; 
  } 
} 
<!-- index.html --> 
  <!DOCTYPE html> 
  <html> 
  <body> 
  <button type="button" onclick="callArkTS()">Click Me!</button> 
  <p id="demo"></p> 
  <script> 
  async function callArkTS() { 
    let str = await testObjName.scan(); 
    document.getElementById("demo").innerHTML = str; 
    console.info('ArkTS Hello World! :' + str); 
  } 
  </script> 
  </body> 
  </html>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
分享
微博
QQ
微信
回复
2024-11-05 18:06:13
相关问题
HarmonyOS H5调用原生功能
963浏览 • 1回复 待解决
HarmonyOS H5拉起系统相机的样例代码
1177浏览 • 1回复 待解决
HarmonyOS web组件加载h5h5拉起摄像头
1585浏览 • 1回复 待解决
HarmonyOS 外部H5页面拉起手机银行app
820浏览 • 1回复 待解决
HarmonyOS H5如何复用?
1195浏览 • 1回复 待解决
HarmonyOS 需要demo
831浏览 • 1回复 待解决
HarmonyOS h5拉起app,如何获取参数
721浏览 • 1回复 待解决
H5原生调JSbrige的demo示例
1002浏览 • 1回复 待解决
HarmonyOS h5 web上传图片,文件的demo
819浏览 • 1回复 待解决