中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
微信扫码分享
//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>