代码拉取完成,页面将自动刷新
本示例介绍如何在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: (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);
}
<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页面
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。