You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

289 lines
7.1 KiB

// pagesA/selectFace/selectFace.js
Page({
/**
* 页面的初始数据
*/
data: {
cameraStatus:false,
imgSrc:'',
screenW: '',
width: '',
height: '',
distance: '',
old_width: '',
old_height: '',
cut_: '',
x: 0,
y: 0,
crop_pic:''
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
const _this = this
wx.getSetting({
success: res => {
if (res.authSetting['scope.camera']) {
_this.setData({
cameraStatus: true
})
} else {
// 用户还没有授权,向用户发起授权请求
wx.authorize({
scope: 'scope.camera',
success() { // 用户同意授权
_this.setData({
cameraStatus: true
})
},
fail() { // 用户不同意授权
_this.openSetting().then(res => {
_this.setData({
cameraStatus: true
})
})
}
})
}
},
fail: res => {
console.log('获取用户授权信息失败')
}
})
wx.getSystemInfo({
success: res => {
const screenH = res.screenHeight,
screenW = res.screenWidth,
cut_ = screenW - 4;
let that = this;
that.setData({
screenW: screenW,
cut_: cut_
})
},
})
},
// 打开授权设置界面
openSetting() {
const _this = this
let promise = new Promise((resolve, reject) => {
wx.showModal({
title: '授权',
content: '请先授权获取摄像头权限',
success(res) {
if (res.confirm) {
wx.openSetting({
success(res) {
if (res.authSetting['scope.camera']) { // 用户打开了授权开关
resolve(true)
} else {
// 用户没有打开授权开关, 继续打开设置页面
_this.openSetting().then(res => {
resolve(true)
})
}
},
fail(res) {
console.log(res)
}
})
} else if (res.cancel) {
_this.openSetting().then(res => {
resolve(true)
})
}
}
})
})
return promise;
},
// 拍照
takePhoto() {
var that=this,
width = that.data.screenW,
height = that.data.screenW;
wx.chooseImage({
count:1,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success:function(res){
const tempFilePaths = res.tempFilePaths[0];
wx.getImageInfo({
src: tempFilePaths,
success: function(msg) {
let img_w = msg.width,
img_h = msg.height,
scale = img_w / img_h
//横向图片,宽不变
//横向图片,高变成固定,宽度自适应
if (scale > 1) {
that.setData({
width: scale * width,
height: height,
old_width: scale * width,
old_height: height,
imgSrc: tempFilePaths
})
} else { //纵向图片,短边是宽,宽变成系统固定,高自适应
that.setData({
width: width,
height: height / scale,
old_width: width,
old_height: height / scale,
imgSrc: tempFilePaths
})
}
}
})
},
fail:function(error){
console.error("调用本地相册文件时出错")
console.warn(error)
},
complete:function(back){
console.log(back)
}
})
},
start: function(res) {
let that = this;
if (res.touches.length == 2) {
let _x = res.touches[1].pageX - res.touches[0].pageX,
_y = res.touches[1].pageY - res.touches[0].pageY,
distance = Math.sqrt(Math.pow(_x, 2) + Math.pow(_y, 2));
that.setData({
distance: distance
})
}
},
move: function(res) {
let that = this;
console.log(res)
if (res.touches.length == 2) {
let _x = res.touches[1].pageX - res.touches[0].pageX,
_y = res.touches[1].pageY - res.touches[0].pageY,
old_width = that.data.old_width,
old_height = that.data.old_height,
newdistance = Math.sqrt(Math.pow(_x, 2) + Math.pow(_y, 2)),
width = that.data.width,
height = that.data.height,
distance = that.data.distance,
end_distance = newdistance - distance,
pic_scale = 1 + end_distance * 0.001;
that.setData({
width: width * pic_scale,
height: height * pic_scale
})
let max = width / old_width;
if (max > 2) {
that.setData({
width: old_width * 2,
height: old_height * 2
})
} else if (max < 1) {
that.setData({
width: old_width,
height: old_height
})
}
}
},
scroll: function(e) {
console.log('TOP:' + e.detail.scrollTop + 'Left:' + e.detail.scrollLeft)
let x = e.detail.scrollLeft,
y = e.detail.scrollTop,
that = this;
that.setData({
x: x,
y: y
})
},
crop: function() {
let that = this,
img = that.data.imgSrc,
width = that.data.width,
height = that.data.height,
crop = that.data.screenW,
x = that.data.x,
y = that.data.y;
wx.showToast({
title: 'loading...',
icon: 'loading',
duration: 1000
})
const canvas = wx.createCanvasContext('canvas');
canvas.drawImage(img, 0, 0, width, height)
canvas.draw(setTimeout(() => {
wx.canvasToTempFilePath({
x: x,
y: y,
width: crop,
height: crop,
canvasId: 'canvas',
success: suc => {
console.log(suc.tempFilePath)
that.setData({
crop_pic: suc.tempFilePath
})
},
fail: err => {
console.log('err:' + err)
}
}, this)
}, 1000))
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
})