微信小程序媒體組件 camera

2022-05-11 16:11 更新

camera


基礎(chǔ)庫 1.6.0 開始支持,低版本需做兼容處理。

系統(tǒng)相機(jī)。

需要用戶授權(quán) scope.camera

屬性名 類型 默認(rèn)值 說明
device-position String back 前置或后置,值為front, back
flash String auto 閃光燈,值為auto, on, off
bindstop EventHandle 攝像頭在非正常終止時(shí)觸發(fā),如退出后臺(tái)等情況
binderror EventHandle 用戶不允許使用攝像頭時(shí)觸發(fā)

相關(guān)api:wx.createCameraContext

Bug & Tip
  1. tip: camera 組件是由客戶端創(chuàng)建的原生組件,它的層級(jí)是最高的,不能通過 z-index 控制層級(jí)??墒褂?nbsp;cover-view cover-image覆蓋在上面。
  2. tip: 同一頁面只能插入一個(gè) camera 組件。
  3. tip: 請勿在 scroll-view、swiper、picker-view、movable-view 中使用 camera 組件。

示例:

<!-- camera.wxml -->
<camera device-position="back" flash="off" binderror="error" style="width: 100%; height: 300px;"></camera>
<button type="primary" bindtap="takePhoto">拍照</button>
<view>預(yù)覽</view>
<image mode="widthFix" src="{{src}}"></image>
// camera.js
Page({
    takePhoto() {
        const ctx = wx.createCameraContext()
        ctx.takePhoto({
            quality: 'high',
            success: (res) => {
                this.setData({
                    src: res.tempImagePath
                })
            }
        })
    },
    error(e) {
        console.log(e.detail)
    }
})
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)