文档管理中心
|

Vue.js

HarmonyOS

HarmonyOS行业解决方案

H5页面通过input标签唤起相机功能,鸿蒙手机失效,要怎么处理呢

h5页面中通过
收起
自动换行
深色代码主题
复制
<input ref="file" type="file" capture="user" accept="image/*"  style="display:none" @change="filechange"> 
收起
自动换行
深色代码主题
复制
this.$refs.file.click();

方式点击拍摄按钮唤起相机功能,但是在鸿蒙系统的手机中失效(点击没有反应)其他设备正常,此H5页面可以通过小程序进入也可以通过短信链接进入,要如何适配鸿蒙系统呢,麻烦各位老师帮忙看看。

点赞
收藏
回复
4
分享
举报
浏览168 编辑于2025-12-03 07:38北京
全部评论
最多点赞
最新发布
最早发布

通过H5的capture属性,直接拉起相机进行拍照或者录像。只能使用比较简单的拍照或者录像功能。

收起
自动换行
深色代码主题
复制
<input type="file" accept="video/*" capture="user"/>
4楼编辑于2025-12-02 10:23 来自广东
收起
自动换行
深色代码主题
复制
<!-- videoCapture.html -->
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
</head>
<body>
<p>
    <input type="file" accept="image/*" capture="user"/>
</p>
<p>
    <input type="file" accept="video/*" capture="user"/>
</p>
</body>
</html>

官方有参考文档

https://developer.huawei.com/consumer/cn/doc/architecture-guides/traffic-v1_1-ts_14-0000002376460965

3楼回复于2025-12-02 08:16 来自安徽
2楼回复于2025-12-02 03:16 来自浙江
  • onPermissionRequest:通知收到获取权限请求,需配置"ohos.permission.CAMERA"、"ohos.permission.MICROPHONE"权限。具体权限配置可参考声明权限
  • Web组件可以通过W3C标准协议接口拉起摄像头和麦克风,通过在JavaScript中调用W3C标准协议接口navigator.mediaDevices.getUserMedia(),该接口用于拉起摄像头和麦克风。constraints参数是一个包含了video和audio两个成员的MediaStreamConstraints对象,用于说明请求的媒体类型。

    方案一:通过H5的capture属性,直接拉起相机进行拍照或者录像。只能使用比较简单的拍照或者录像功能。

    收起
    自动换行
    深色代码主题
    复制
    <!-- videoCapture.html -->
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
    </head>
    <body>
    <p>
        <input type="file" accept="image/*" capture="user"/>
    </p>
    <p>
        <input type="file" accept="video/*" capture="user"/>
    </p>
    </body>
    </html>
    

    方案二:Web组件向用户请求手动授权,获取相机权限。

  • 在module.json5文件中配置相关权限:ohos.permission.CAMERAohos.permission.MICROPHONE,具体申请方式请参考声明权限
  • Web组件向用户请求手动授权。
    收起
    自动换行
    深色代码主题
    复制
    import { webview } from '@kit.ArkWeb';
    import { BusinessError } from '@kit.BasicServicesKit';
    import { abilityAccessCtrl } from '@kit.AbilityKit';
    @Entry
    @Component
    struct WebComponent {
      controller: webview.WebviewController = new webview.WebviewController();
      uiContext: UIContext = this.getUIContext();
      aboutToAppear() {
        // 配置Web开启调试模式
        webview.WebviewController.setWebDebuggingAccess(true);
        let atManager = abilityAccessCtrl.createAtManager();
        atManager.requestPermissionsFromUser(this.uiContext.getHostContext(),
          ['ohos.permission.CAMERA', 'ohos.permission.MICROPHONE'])
          .then((data) => {
            console.info(`data:${data}`);
            console.info('data permissions:' + data.permissions);
            console.info('data authResults:' + data.authResults);
          }).catch((error: BusinessError) => {
          console.error(`Failed to request permissions from user. Code is ${error.code}, message is ${error.message}`);
        });
      }
      build() {
        Column() {
          Web({ src: $rawfile('videoCall.html'), controller: this.controller })
            .fileAccess(false)
            .geolocationAccess(false)
            .onPermissionRequest((event) => {
              if (event) {
                this.uiContext.showAlertDialog({
                  title: 'title',
                  message: 'text',
                  primaryButton: {
                    value: 'deny',
                    action: () => {
                      event.request.deny();
                    }
                  },
                  secondaryButton: {
                    value: 'onConfirm',
                    action: () => {
                      event.request.grant(event.request.getAccessibleResource());
                    }
                  },
                  cancel: () => {
                    event.request.deny();
                  }
                });
              }
            });
        };
      }
    }
    
  • HTML
    收起
    自动换行
    深色代码主题
    复制
    <!-- videoCall.html -->
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
    </head>
    <body>
    <video id="video" width="500px" height="500px" autoplay="autoplay"></video>
    <canvas id="canvas" width="500px" height="500px"></canvas>
    <br>
    <input type="button" title="HTML5摄像头" value="开启摄像头" onclick="getMedia()"/>
    <script>
        function getMedia()
        {
          // 通过facingMode指定使用摄像头,前置为user,后置为environment
          let constraints = {
            video: {width: 500, height: 500, facingMode: "user"},
            audio: true
          };
          let video = document.getElementById("video");
          let promise = navigator.mediaDevices.getUserMedia(constraints);
          promise.then(function (MediaStream) {
            video.srcObject = MediaStream;
            video.play();
          });
        }
    </script>
    </body>
    </html>
    

展开全部
1楼回复于2025-12-02 02:38 来自北京
写回答
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
  • 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
  • 如您发布的内容为转载内容,请注明内容来源。

我要提问题

了解社区公约,与您携手共创和谐专业的开发者社区。