大家知道一款小程序,很多时候需要调用内置的照相机,实现拍照并图片上传,那么要如何实现呢?

  1.index.wxml
 

<!--index.wxml--> <button style="margin:30rpx;" bindtap="chooseimage">获取图片</button> <image src="{{tempFilePaths }}" mode="aspecFill" style="width: 100%; height: 450rpx"/>

  2.index.js
 

//index.js //获取应用实例 var app = getApp()

微信小程序调用内置照相机实现拍照及图片上传

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

  API 说明:

微信小程序调用内置照相机实现拍照及图片上传

  这里说说sourcetype.默认是从相册获取和使用相机拍照,跟微信现在选择图片的界面一样,第一格是拍照,后面的是相册照片.

  这里注意:返回的是图片在本地的路径.如果需要将图片上传到服务器,需要用到另一个API.

  示例代码:
 

wx.chooseImage({   success: function(res) {     var tempFilePaths = res.tempFilePaths     wx.uploadFile({       url: 'http://example.weixin.qq.com/upload', //仅为示例,非真实的接口地址       filePath: tempFilePaths[0],       name: 'file',       formData:{         'user': 'test'       },       success: function(res){         var data = res.data         //do something       }     })   } })

HiShop小程序工具提供多类型商城/门店小程序制作,可视化编辑 1秒生成5步上线。通过拖拽、拼接模块布局小程序商城页面,所看即所得,只需要美工就能做出精美商城。

更多小程序开发案例,尽在:http://www.hishop.com.cn/xiaocx/kaifa.html


TAG标签: 小程序相机小程序图片小程序

http://www.xiaochengxub.com/read-71755.html