在浏览器调用相机(只对https兼容),适用于H5

文章介绍了如何在Vue组件中实现一个在HTTPS环境下使用的摄像头功能,包括用户权限检查、拍照并处理Base64流上传至服务器的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

由于浏览器的机制,只能在https中才能正常使用(注意注意)

一、组件

<template>
  <view class="root">
    <view class="canvasBox">
      <template v-if="!isUse">
        <slot name="error">
          <view class="error">
            <view class="on1">相机权限被拒绝,请尝试如下操作:</view>
            <view>· 刷新页面后重试;</view>
            <view>· 在系统中检测当前App或浏览器的相机权限是否被禁用;</view>
            <view>· 如果依然不能体验,建议在微信中打开链接;</view>
          </view>
        </slot>
      </template>
    </view>
  </view>
</template>

<script>
export default {
  props: {
    continue: {
      type: Boolean,
      default: false, // false 监听一次   true 持续监听
    },
    exact: {
      type: String,
      default: "user", // environment 后摄像头  user 前摄像头
    },
    size: {
      type: String,
      default: "whole", // whole 全屏  balf 半屏
    },
    definition: {
      type: Boolean,
      default: false, // fasle 正常  true 高清
    },
  },
  data() {
    return {
      windowWidth: 0,
      windowHeight: 0,
      video: null,
      canvas2d: null,
      canvasWidth: 400,
      canvasHeight: 400,
      maskWidth: 400,
      maskHeight: 400,
      inter: 0,

      isParse: false,
      isUse: true,
    };
  },
  mounted() {
    if (origin.indexOf("https") === -1)
      throw "请在 https 环境中使用摄像头组件。";

    this.windowWidth =
      document.documentElement.clientWidth || document.body.clientWidth;
    this.windowHeight =
      document.documentElement.clientHeight || document.body.clientHeight;
    this.windowHeight =
      this.size === "whole" ? this.windowHeight : this.windowHeight / 2;
    
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值