HarmonyOS 能否提供扫码的demo代码

HarmonyOS
2024-12-26 13:02:52
565浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

h5拉起系统扫码页面的demo如下:

// 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.huawei.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;
  }
}
  • 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.
分享
微博
QQ
微信
回复
2024-12-26 16:39:56


相关问题
HarmonyOS 需要demo
836浏览 • 1回复 待解决
HarmonyOS h5拉起系统页面的demo
1059浏览 • 1回复 待解决
HarmonyOS 功能Demo
683浏览 • 1回复 待解决
HarmonyOS 能否提供登录界面样例demo
957浏览 • 1回复 待解决
HarmonyOS 能否提供Web样例工程代码
984浏览 • 1回复 待解决
HarmonyOS 直达问题
772浏览 • 1回复 待解决
HarmonyOS 能否提供一个视频压缩demo
776浏览 • 1回复 待解决
相册识别多失败
2627浏览 • 1回复 待解决
HarmonyOS 服务权限问题
875浏览 • 1回复 待解决
HarmonyOS模块”通用能力
851浏览 • 1回复 待解决
HarmonyOS能否提供一个NFC识别的demo
1410浏览 • 2回复 待解决
HarmonyOS 是否有功能
752浏览 • 1回复 待解决
HarmonyOS 自定义咨询
617浏览 • 1回复 待解决