HarmonyOS 前端页面调用应用侧函数javascript可运行demo(javaScriptProxy)

HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
fox280
//ArkTS
import { common } from '@kit.AbilityKit';
import { util } from '@kit.ArkTS';
import { webview } from '@kit.ArkWeb';

@Entry
@Component
struct Page4 {
  webController: webview.WebviewController = new webview.WebviewController();
  @State test: Test = new Test()
  build() {
    Column(){
      Web({src:$rawfile('index2.html'),controller:this.webController})
        .javaScriptProxy({
          object: this.test,
          name: "picture",
          methodList: ["takePhoto"],
          controller: this.webController
        }).height("50%")
    }
  }
}

class Test{
  takePhoto() {
    let context = getContext(this) as common.UIAbilityContext
    let arrayBuff = context.resourceManager.getRawFileContentSync("startIcon.png")
    let base64helper = new util.Base64Helper()
    let base64string = base64helper.encodeToStringSync(arrayBuff,util.Type.MIME)
    console.debug(base64string)
    return base64string
  }
}

//h5:
<!DOCTYPE html>
  <html lang="zh-CN">
  <head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  </head>
  <body>

  <button id="init" onclick="takePicture()">模拟拍照</button>
  <img src="" id="myImage" alt="">
  </body>
  </html>
  <script>
  function takePicture() {
    let s = 'data:image/png;base64,' + picture.takePhoto();

    document.getElementById('myImage').src =  s;
  }
  </script>
分享
微博
QQ
微信
回复
1天前
相关问题
rn HarmonyOS可运行demo
30浏览 • 1回复 待解决
HarmonyOS 在h5前端调用应用侧方法
49浏览 • 2回复 待解决
Native调用ArkTS函数
995浏览 • 1回复 待解决
HarmonyOS 应用访问前端页面方法
82浏览 • 1回复 待解决
HarmonyOS WebView组件前端页面调试
276浏览 • 1回复 待解决
HarmonyOS H5与应用数据交互的Demo
589浏览 • 1回复 待解决