微信小程序开发之——拍照和录像

一 概述

微信小程序中,与相机相关的组件是媒体组件camera

  • 调用系统相机拍照
  • 调用系统相机录像
  • 调用系统相机扫码

二 camera相关属性

属性类型默认值说明
modestringnormal应用模式,只在初始化时有效,不能动态变更
resolutionstringmedium分辨率,不支持动态修改
device-positionstringback摄像头朝向
flashstringauto闪光灯,值为auto, on, off
frame-sizestringmedium指定期望的相机帧数据尺寸
bindstopeventhandle摄像头在非正常终止时触发,如退出后台等情况
binderroreventhandle用户不允许使用摄像头时触发
bindinitdoneeventhandle相机初始化完成时触发,e.detail = {maxZoom}
bindscancodeeventhandle在扫码识别成功时触发,仅在 mode=“scanCode” 时生效

mode 的合法值

说明
normal相机模式
scanCode扫码模式

resolution 的合法值

说明
low
medium
high

device-position 的合法值

说明
front前置
back后置

flash 的合法值

说明
auto自动
on打开
off关闭
torch常亮

frame-size 的合法值

说明
small小尺寸帧数据
medium中尺寸帧数据
large大尺寸帧数据

三 示例代码

3.1 camera.wxml 布局文件

<view class="page-body">
  <view class="page-body-wrapper">
    <camera device-position="back" flash="off" binderror="error" style="width: 100%; height: 300px;"></camera>
    <view class="btn-area">
      <button type="primary" bindtap="takePhoto">拍照</button>
    </view>
    <view class="btn-area">
      <button type="primary" bindtap="startRecord">开始录像</button>
    </view>
    <view class="btn-area">
      <button type="primary" bindtap="stopRecord">结束录像</button>
    </view>
    <view class="preview-tips">预览</view>
    <image wx:if="{{src}}" mode="widthFix" src="{{src}}"></image>
    <video wx:if="{{videoSrc}}" class="video" src="{{videoSrc}}"></video>
  </view>
</view>

3.2 camera.js 逻辑文件 拍照和录像

Page({
  onLoad() {
    this.ctx = wx.createCameraContext()
  },
  takePhoto() {
    this.ctx.takePhoto({
      quality: 'high',
      success: (res) => {
        this.setData({
          src: res.tempImagePath
        })
      }
    })
  },
  startRecord() {
    this.ctx.startRecord({
      success: (res) => {
        console.log('startRecord')
      }
    })
  },
  stopRecord() {
    this.ctx.stopRecord({
      success: (res) => {
        this.setData({
          src: res.tempThumbPath,
          videoSrc: res.tempVideoPath
        })
      }
    })
  },
  error(e) {
    console.log(e.detail)
  }
})

3.3 预览图(模拟器不显示预览图像)

四 参考

  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值