微信小程序调用相册和相机

首先来写好wxml部分:给一个图片列表(img_list)和上传图片的按钮(addimg)

<view class="container">
  <view class='img_body'>
    <view class='img_list'>
      <view class='img_li' wx:for="{{imglist}}" wx:key="{{index}}">
        <image src="{{item}}"></image>
      </view>
      <view class='addimg' bindtap='img_w_show'>
        <image src='../../img/add.png'></image>
      </view>
    </view>
  </view>
</view>

css部分控制如下:

.img_list{
	width: 100vw;
	display: flex;
	display: -webkit-flex;
  padding: 0 20rpx;
  box-sizing: border-box;
  flex-wrap: wrap;
  -webkit-flex-wrap:wrap;
}
.img_list .img_li,
.addimg{
	width: 200rpx;
	height: 250rpx;
	border: 1px solid #999999;
  margin: 5rpx;
  flex-shrink: 0;
  -webkit-flex-shrink:0;
}
.addimg image{
	width: 150rpx;
	height: 150rpx;
	margin: 50rpx 25rpx;
}
.img_list .img_li image{
  width: 100%;
  height: 100%;
}

如果只是想调用下面这个弹窗,微信小程序调用的api是wx.showActionSheet,

代码如下:

wx.showActionSheet({
  itemList: ['从手机相册选择', '拍照'],
  success: function(res) {
    console.log(res.tapIndex)
  },
  fail: function(res) {
    console.log(res.errMsg)
  }
})

调用的图如下:

 

但是,我们是要实现功能的丫

在我的js文件里面先在data里面命名imglist为空数组

然后调用img_w_show函数:

img_w_show(){
    var _this=this;
    wx.chooseImage({
      count: 9, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths
        _this.setData({
          imglist: _this.data.imglist.concat(tempFilePaths)
        })
      }
    })
  }
})

使用情况如下:

  • 13
    点赞
  • 86
    收藏
    觉得还不错? 一键收藏
  • 16
    评论
微信小程序可以通过调用微信开放平台提供的人脸识别接口来实现人脸识别功能。下面是一个简单的示例代码: 1. 在小程序中引入wx.request函数,用于向后端发送请求。代码示例: ```javascript import wx from 'weixin-js-sdk'; wx.ready(function(){ // 在此处调用wx.request函数向后端发送请求,获取人脸识别结果 }) ``` 2. 后端接收小程序发送的请求,并进行人脸识别。代码示例: ```python import requests import base64 def face_detect(image): # 调用人脸识别API进行人脸检测 url = 'https://api-cn.faceplusplus.com/facepp/v3/detect' data = { 'api_key': 'your_api_key', 'api_secret': 'your_api_secret', 'image_base64': base64.b64encode(image).decode('utf-8'), 'return_attributes': 'gender,age,smiling,emotion' } response = requests.post(url, data=data) return response.json() ``` 在上述代码中,我们使用Face++提供的人脸识别API进行人脸检测。需要将`your_api_key`和`your_api_secret`替换为自己在Face++官网申请的API Key和API Secret。 3. 在前端调用wx.chooseImage函数,用于选择要识别的图片。代码示例: ```javascript wx.chooseImage({ count: 1, // 只能选择一张图片 sizeType: ['compressed'], // 压缩图片 sourceType: ['album', 'camera'], // 可以从相册相机中选择图片 success: function (res) { var tempFilePaths = res.tempFilePaths; // 在此处调用wx.request函数向后端发送请求,获取人脸识别结果 } }) ``` 在上述代码中,我们使用`wx.chooseImage`函数从相册相机中选择图片,并将选择的图片路径保存在`tempFilePaths`中。 4. 在前端将选择的图片转换成Base64格式,并将Base64编码后的图片数据发送到后端进行人脸识别。代码示例: ```javascript wx.getFileSystemManager().readFile({ filePath: tempFilePaths[0], encoding: 'base64', success: function(res) { // 调用wx.request函数向后端发送请求,获取人脸识别结果 wx.request({ url: 'https://your_server.com/face_detect', method: 'POST', data: { image: res.data }, success: function(response) { console.log(response.data); } }) } }) ``` 在上述代码中,我们使用`wx.getFileSystemManager().readFile`函数将选择的图片转换成Base64格式,并将Base64编码后的图片数据发送到后端进行人脸识别。需要将`https://your_server.com/face_detect`替换为自己的后端接口地址。
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值