美文网首页
3.【已解决】小程序camera组件无法拍照

3.【已解决】小程序camera组件无法拍照

作者: TensorFlow开发者 | 来源:发表于2019-05-26 12:25 被阅读0次

场景

自己在参考小程序官方文档,使用camera组件时,无法拍照。代码都是参考官方文档的,但无法正常拍照。


element.wxml文件:

<camera mode="normal" device-position="back" binderror="cameraBindError"
style="width: 100%; height: 300px;"
></camera>

<button type='primary' bindtap="takePhoto">拍照</button>
<view>预览</view>
<image mode="widthFix" style="width:100%; height:500px" src="{{camera_src}}"></image>

element.js文件:

  // 拍照
  takePhoto(event){
    const cameraCtx = wx.createCameraContext(this);
    cameraCtx.takePhoto({
      quality:"high",
      success:(res)=>{
        this.setData({
          camera_src: res.tempImagePath,
        });
      },
      fail:(msg)=>{
        wx.showToast({
          title: "Error,相机拍照出错!!",
        })
      }
    });
  },
  cameraBindError(event){
    wx.showToast({
      title: "Error,相机未授权!",
    })

  },

解决方案

和官方文档仔仔细细对比后,发现:在element.js文件中,我是这么写的,多了一个this参数。
const cameraCtx = wx.createCameraContext(this);
官方文档中:
const cameraCtx = wx.createCameraContext();

把自己的代码去掉this后,就可以正常拍照了。

相关文章

网友评论

      本文标题:3.【已解决】小程序camera组件无法拍照

      本文链接:https://www.haomeiwen.com/subject/wgpxtctx.html