使用相机组件,部分华为(荣耀)拍照失败。
发布于 6 年前 作者 tao61 6829 次浏览 来自 问答
  • 当前 Bug 的表现(可附上截图)

能看到拍照的界面,但是调用 takePhoto 提示错误 operateCamera:fail:camera has not been initialized

只有部分华为(荣耀)手机出现该情况。ios正常。

如果写一个简单的demo或者官方相机组件的demo也没有问题。

  • 相关代码

wxml

<view id="wrapper">
  <!-- 相机 -->
  <camera id="camera" device-position="back" flash="off" binderror="error"></camera>
  <cover-view id="customStyle">
    <!-- 点击拍摄 -->
    <cover-view class="takePhoto" bindtap="takePhoto">
      <cover-view class="inside"></cover-view>
    </cover-view>
  </cover-view>
</view>

js

Page({
  data: {
    // 图片路径
    imgUrl: ''
  },
 
  onLoad: function(options) {
    this.ctx = wx.createCameraContext()
  },
  // 拍照
  takePhoto() {
    wx.getSetting({
      success: (res) => {
        if (!res.authSetting['scope.camera']) {
          wx.openSetting({
            success(res) {
              if (res.authSetting['scope.camera']) {
                wx.navigateBack({
                  delete: 1
                })
              }
            }
          })
        } else {
          this.ctx.takePhoto({
            quality: 'high',
            success: (res) => {
              this.setData({
                imgUrl: res.tempImagePath,
                oppenPhoto: true
              })
            },
            fail: (err) => {
              console.log(err)
            }
          })
        }
      }
    })
  }
})
2 回复

camera 组件插入后,客户端要先初始化相机。在这之前调用 take photo 等接口会失败,出现上述提示。

下个版本增加初始化完成事件,在这之后调用就可以了。

回到顶部