Get the App
SLTechnology News&Howtos  ›  Development  › 

How does WeChat Mini Programs use the built-in camera to take pictures and upload pictures?

Shulou Source: shulou.com Published: 2022-06-01 05:46:12 11月04日 Update

This article mainly introduces the relevant knowledge of WeChat Mini Programs how to use the built-in camera to take photos and upload pictures. The content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on how WeChat Mini Programs uses the built-in camera to take photos and upload pictures. Let's take a look.

1.index.wxml

Get a picture

2.index.js

/ / index.js

/ / obtain an application instance

Var app = getApp ()

Page ({

Data: {

TempFilePaths:''

}

OnLoad: function () {

}

Chooseimage: function () {

Var _ this = this

Wx.chooseImage ({

Count: 1, / / default 9

SizeType: ['original',' compressed'], / / you can specify whether it is the original image or the compressed image. Both are available by default.

SourceType: ['album',' camera'], / / you can specify whether the source is a photo album or a camera. It has both by default.

Success: function (res) {

/ / returns the local file path list of the selected photo. TempFilePath can display the picture as the src attribute of the img tag.

_ this.setData ({

TempFilePaths:res.tempFilePaths

})

}

})

}

})

API description:

Let's talk about sourcetype. The default is to get photos from albums and take pictures with a camera. Like the interface in which Wechat now selects pictures, the first frame is to take photos, followed by album photos.

Note here: what is returned is the local path of the picture. If you need to upload the picture to the server, you need to use another API.

Sample code:

Wx.chooseImage ({

Success: function (res) {

Var tempFilePaths = res.tempFilePaths

Wx.uploadFile ({

Url: 'http://example.weixin.qq.com/upload', / / is an example only, not a real interface address

FilePath: tempFilePaths [0]

Name: 'file'

FormData: {

'user': 'test'

}

Success: function (res) {

Var data = res.data

/ / do something

}

})

}

})

This is the end of the article on "how to use WeChat Mini Programs's built-in camera to take photos and upload pictures". Thank you for reading! I believe you all have a certain understanding of "how WeChat Mini Programs uses the built-in camera to take photos and upload pictures". If you want to learn more, you are welcome to follow the industry information channel.

Tags: Pictures cameras programs photos albums knowledge content photos cameras examples articles paths or code values original images addresses examples properties interfaces Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Linux OPPO Reno MySQL vpn