往期鸿蒙5.0全套实战文章必看:(文中附带鸿蒙5.0全栈学习资料)
Web获取相机拍照图片案例
介绍
本示例介绍如何在HTML页面中拉起原生相机进行拍照,并获取返回的图片。
效果预览图
使用说明
- 点击HTML页面中的选择文件按钮,拉起原生相机进行拍照。
- 完成拍照后,将图片在HTML的img标签中显示。
实现思路
-
添加Web组件,设置onShowFileSelector属性,接收HTML页面中input的点击事件。在onShowFileSelector中调用invokeCamera接口,拉起原生相机进行拍照,并通过callback回调方法获得照片的uri。然后将uri放在FileSelectorResult中,通过event参数返回给HTML页面。
Web({ src: $rawfile(LOCAL_HTML_PATH), controller: this.controller }) .onShowFileSelector((event: FileResult) => { this.invokeCamera(((uri: string) => { event?.result.handleFileList([uri]); })) return true; })
-
实现invokeCamera接口,拉起原生相机,并通过callback回调方法返回拍照结果。
invokeCamera(callback: (uri: string) => void) { const context = getContext(this) as common.UIAbilityContext; let want: Want = { action: ACTION_IMAGE_CAPTURE, parameters: { "callBundleName": context.abilityInfo.bundleName, } }; let result: (error: BusinessError, data: common.AbilityResult) => void = (error: BusinessError, data: common.AbilityResult) => { if (error && error.code !== 0) { logger.error(`${TAG_CAMERA_ERROR} ${JSON.stringify(error.message)}`); return; } let resultUri: string = data.want?.parameters?.resourceUri as string; if (callback && resultUri) { callback(resultUri); } } context.startAbilityForResult(want, result); }
-
在HTML页面中添加input标签,并在onChange属性中添加js方法,通过dom tree返回的描述事件相关信息的event对象接收ArkTS返回的照片,并显示在img标签上。
<script> function showPic() { let event = this.event; let tFile = event ? event.target.files : []; if (tFile === 0) { document.getElementById('image_preview').style.display = 'block'; document.getElementById('image_preview').innerHTML = "未选择图片"; return } document.getElementById('image').setAttribute('src', URL.createObjectURL(tFile[0])); document.getElementById('image_preview').style.display = 'block'; document.getElementById('image').style.display = 'block'; } </script> <input ref="camera" type="file" id="upload" name="upload" accept="image/*" capture="upload" onchange="showPic()" /> <p id="image_preview">图片预览</p> <img id="image">
工程结构&模块类型
webgetcameraimage // har类型
|---mainpage
| |---MainPage.ets // ArkTS页面
|---rawfile
| |---camera.html // HTML页面