# camera
基礎(chǔ)庫(kù) 1.6.0 開始支持,低版本需做兼容處理。
微信 Mac 版:支持
微信 鴻蒙 OS 版:支持
相關(guān)文檔: wx.createCameraContext
渲染框架支持情況:Skyline (使用最新 Nightly 工具調(diào)試)、WebView
# 功能描述
系統(tǒng)相機(jī)。掃碼二維碼功能,需升級(jí)微信客戶端至6.7.3。需要用戶授權(quán) scope.camera。
2.10.0起 initdone 事件返回 maxZoom,最大變焦范圍,相關(guān)接口 CameraContext.setZoom。
# 通用屬性
| 屬性 | 類型 | 默認(rèn)值 | 必填 | 說(shuō)明 | 最低版本 | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| mode | string | normal | 否 | 應(yīng)用模式,只在初始化時(shí)有效,不能動(dòng)態(tài)變更 | 2.1.0 | ||||||||||||||||
| |||||||||||||||||||||
| resolution | string | medium | 否 | 分辨率,不支持動(dòng)態(tài)修改 | 2.10.0 | ||||||||||||||||
| |||||||||||||||||||||
| device-position | string | back | 否 | 攝像頭朝向 | 1.0.0 | ||||||||||||||||
| |||||||||||||||||||||
| flash | string | auto | 否 | 閃光燈,值為auto, on, off | 1.0.0 | ||||||||||||||||
| |||||||||||||||||||||
| frame-size | string | medium | 否 | 指定期望的相機(jī)幀數(shù)據(jù)尺寸 | 2.7.0 | ||||||||||||||||
| |||||||||||||||||||||
| bindstop | eventhandle | 否 | 攝像頭在非正常終止時(shí)觸發(fā),如退出后臺(tái)等情況 | 1.0.0 | |||||||||||||||||
| binderror | eventhandle | 否 | 用戶不允許使用攝像頭時(shí)觸發(fā) | 1.0.0 | |||||||||||||||||
| bindinitdone | eventhandle | 否 | 相機(jī)初始化完成時(shí)觸發(fā),e.detail = {maxZoom} | 2.7.0 | |||||||||||||||||
| bindscancode | eventhandle | 否 | 在掃碼識(shí)別成功時(shí)觸發(fā),僅在 mode="scanCode" 時(shí)生效 | 2.1.0 | |||||||||||||||||
# Bug & Tip
tip: 同一頁(yè)面只能插入一個(gè)camera組件tip:請(qǐng)注意原生組件使用限制tip:onCameraFrame 接口根據(jù) frame-size 返回不同尺寸的原始幀數(shù)據(jù),與 Camera 組件展示的圖像不同,其實(shí)際像素值由系統(tǒng)決定
# 示例代碼
<!-- 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)
}
})