在本文 微信小程序相机组件wx.createCameraContext()的使用模拟微信拍照之前需要看看

微信小程序-获取用户session_key,openid,unionid - 后端为nodejs

代码封装是在上文添加的。

本文知识点:

1、微信小程序相机组件wx.createCameraContext()使用

2、微信小程序拍照,录视频实现

3、微信小程序上传文件接口wx.uploadFile()的使用

4、微信小程序wx:if的使用

5、nodejs上传文件multer模块的使用

6、camera组件使用

效果

在笔记本上,没有前后置翻转效果,真机上是有的;
在微信开发工具里camera组件是透明的, 真机上没问题;
在微信开发工具里上传的视频格式为webm,真机上为mp4。

小程序代码

1、在utils下的wechat.js文件里添加代码
  /*** 将本地资源上传到开发者服务器,客户端发起一个 HTTPS POST 请求* @param {string} url 开发者服务器 url* @param {string} filePath 要上传文件资源的路径* @param {string} name * @param {object} formData HTTP 请求中其他额外的 form data*/static uploadFile(url, filePath, name, formData = { openid: "test" }) {return new Promise((resolve, reject) => {let opts = { url, filePath, name, formData, header: { 'Content-Type': "multipart/form-data" }, success: resolve, fail: reject };wx.uploadFile(opts);});}

js

//index.js
//获取应用实例
let app = getApp();
let wechat = require("../../utils/wechat");
Page({data: {device: true,tempImagePath: "", // 拍照的临时图片地址tempThumbPath: "", // 录制视频的临时缩略图地址tempVideoPath: "", // 录制视频的临时视频地址camera: false,ctx: {},type: "takePhoto",startRecord: false,time: 0,timeLoop: "",},onLoad() {this.setData({ctx: wx.createCameraContext()})},// 切换相机前后置摄像头devicePosition() {this.setData({device: !this.data.device,})console.log("当前相机摄像头为:", this.data.device ? "后置" : "前置");},camera() {let { ctx, type, startRecord } = this.data;// 拍照if (type == "takePhoto") {console.log("拍照");ctx.takePhoto({quality: "normal",success: (res) => {// console.log(res);this.setData({tempImagePath: res.tempImagePath,camera: false,});wechat.uploadFile("https://xx.xxxxxx.cn/api/upload", res.tempImagePath, "upload").then(d => {console.log(d);}).catch(e => {console.log(e);})},fail: (e) => {console.log(e);}})}// 录视频else if (type == "startRecord") {if (!startRecord) {console.log("开始录视频");this.setData({startRecord: true});// 30秒倒计时let t1 = 0;let timeLoop = setInterval(() => {t1++;this.setData({time: t1,})// 最长录制30秒if (t1 == 30) {clearInterval(timeLoop);this.stopRecord(ctx);}}, 1000);this.setData({timeLoop})// 开始录制ctx.startRecord({success: (res) => {console.log(res);},fail: (e) => {console.log(e);}})}else {this.stopRecord(ctx);}}},// 停止录制stopRecord(ctx) {console.log("停止录视频");clearInterval(this.data.timeLoop);ctx.stopRecord({success: (res) => {this.setData({tempThumbPath: res.tempThumbPath,tempVideoPath: res.tempVideoPath,camera: false,startRecord: false,time: 0});wechat.uploadFile("https://xx.xxxxxx.cn/api/upload", res.tempThumbPath, "tempThumbPath").then(d => {console.log(d);return wechat.uploadFile("https://xx.xxxxxx.cn/api/upload", res.tempVideoPath, "tempVideoPath")}).then(d => {console.log(d);}).catch(e => {console.log(e);})},fail: (e) => {console.log(e);}})},// 打开模拟的相机界面open(e) {let { type } = e.target.dataset;console.log("开启相机准备", type == "takePhoto" ? "拍照" : "录视频");this.setData({camera: true,type})},// 关闭模拟的相机界面close() {console.log("关闭相机");this.setData({camera: false})}
})

html

<view class="view"><view class="window"><image class="cover-9" src="{{tempImagePath}}" bindtap="img" wx:if="{{type=='takePhoto'}}"></image><video class="cover-9" src="{{tempVideoPath}}" poster="{{tempThumbPath}}" wx:if="{{type=='startRecord'}}"></video><view class="window-2"><button bindtap="open" type="primary" data-type="takePhoto">拍照</button><button bindtap="open" type="primary" data-type="startRecord">录制</button></view></view><camera class="camera" device-position="{{device?'back':'front'}}" wx:if="{{camera}}" flash="off"><cover-view class="cover-1" bindtap="camera"><cover-view class="cover-2"><cover-view class="cover-5" wx:if="{{type=='startRecord'&&startRecord}}">{{time}}S</cover-view></cover-view></cover-view><cover-image class="cover-3" src="/images/xx2.png" style="width:60rpx;height:60rpx;" bindtap="close"></cover-image><cover-image class="cover-4" src="/images/zh.png" style="width:80rpx;height:60rpx;" bindtap="devicePosition"></cover-image></camera>
</view>

css

page{height: 100%;
}
.view{width: 100%;height: 100%;
}
.window{width: 100%;height: 100%;position: absolute;
}
.window-2{display: flex;flex-direction: row;
}
.camera{width: 100%;height: 100%;
}.cover-1{width: 150rpx;height: 150rpx;border-radius: 150rpx;background-color: #dedcdc;display: flex;flex-direction: row;align-items: center;justify-content: center;position: absolute;bottom: 60rpx;left: 300rpx;
}
.cover-3{position: absolute;bottom: 105rpx;left:135rpx;
}
.cover-9{width: 728rpx;height: 80%;margin: 0 10rpx;border:2rpx solid;border-radius:5px;
}
button{margin: 0 10rpx;width: 100%;
}
.cover-4{position: absolute;top: 60rpx;right:40rpx;
}
.cover-2{width: 110rpx;height: 110rpx;border-radius: 110rpx;background-color: #ffffff;display: flex;flex-direction: row;align-items: center;justify-content: center;color:#da2121;font-size: 32rpx;
}

nodejs代码

文件上传使用multer模块,引入multer模块后配置好并添加一个上传处理的路由,创建上传的目录,
本文是uploads/tmp目录
const multer = require('multer');
let path = require("path");
//上传文件配置
const storage = multer.diskStorage({//文件存储位置destination: (req, file, cb) => {cb(null, path.resolve(__dirname, '../uploads/tmp/'));},//文件名filename: (req, file, cb) => {cb(null, `${Date.now()}_${Math.ceil(Math.random() * 1000)}_multer.${file.originalname.split('.').pop()}`);}
});
const uploadCfg = {storage: storage,limits: {//上传文件的大小限制,单位bytesfileSize: 1024 * 1024 * 20}
};
router.post("/api/upload", async (req, res) => {let upload = multer(uploadCfg).any();upload(req, res, async (err) => {if (err) {res.json({ path: `//uploads/tmp/${uploadFile.filename}` });console.log(err);return;};console.log(req.files);let uploadFile = req.files[0];res.json({ path: `//uploads/tmp/${uploadFile.filename}` });});
})
后端打印

上传的文件夹

素材

向下箭头:https://img-blog.csdn.net/20180226111545178
翻转图片:https://img-blog.csdn.net/20180226111559273

参考地址:

https://mp.weixin.qq.com/debug/wxadoc/dev/component/camera.html
https://mp.weixin.qq.com/debug/wxadoc/dev/api/api-camera.html

意外金喜的博客:http://blog.csdn.net/zzwwjjdj1

微信小程序相机组件wx.createCameraContext()的使用模拟微信拍照-后端为nodejs相关推荐

  1. 微信小程序相机组件的使用

    微信小程序相机组件的使用            这一段时间微信小程序更新频率越来越快,几乎每周一更.2017.10.13又更新了相机组件,可谓美翻了,下面让我们来看一下都更新了什么吧. 一.关于更新 ...

  2. 微信小程序地图组件和相机组件实现基于location的AR效果的尝试(失败)

    微信小程序地图组件和相机组件实现基于location的AR效果的尝试(失败) 最近无论AR还是微信小程序都是炒的火热.微信小程序的特点便是"无需安装,用完即走",而我们所说的AR用 ...

  3. 微信小程序image组件开发程序以及相关图片问题参考资料汇总

    微信小程序image组件开发程序以及相关图片问题参考资料汇总,希望对大家小程序开发能有一定的参考和借鉴价值.以下汇总主要涉及到微信小程序image组件有关资源路径.缩放和剪裁模式等进行的探讨,无论是对 ...

  4. 微信小程序自定义组件,提示组件

    微信小程序自定义组件,这里列举了一个常用的提示自定义组件,调用自定义组件中的方法和字段.仅供参考和学习. 编写组件: 在根目录下添加"components"目录,然后像添加Page ...

  5. 手把手教你写一个微信小程序日历组件

    今天我们一起写一个微信小程序日历组件 微信小程序日历组件 github.com/749264345/w- 好,我们先看一下要实现的模样,如下图 由以上截图我们可以看到 1.日历可以通过按钮[切换展示效 ...

  6. 微信小程序 MinUI 组件库系列之 abnor 异常流组件

    MinUI 是基于微信小程序自定义组件特性开发而成的一套简洁.易用.高效的组件库,适用场景广,覆盖小程序原生框架.各种小程序组件主流框架等,并且提供了高效的命令行工具.MinUI 组件库包含了很多功能 ...

  7. 微信小程序 常用组件

    欢迎体验个人小程序 表情小作坊 轻松定制表情包 三连图 文字转图片 吃什么都行 解决广大用户吃饭选择恐惧症的问题 微信小程序的组件也挺多了,还是把官网的组件介绍地址先贴出来吧 https://mp.w ...

  8. 记录一下使用微信小程序wx-open-launch-weapp组件

    微信小程序wx-open-launch-weapp组件 H5跳小程序的组件 官方文档:微信开放文档 开始配置 这个组件只能在微信内置浏览器里面使用 前期可以用微信开发者工具进行调试(公众号网页模式) ...

  9. 微信小程序_map组件实现定位

    微信小程序_map组件实现定位 map组件 这是官方提供的地图组件,很多复杂的功能我暂时没有接触到,而且有的效果似乎只有企业可以使用.我在这里就简单的实现一下map组件的定位用户的位置的功能. 下面的 ...

最新文章

  1. 线性回归之数学:求导公式
  2. 实现一个webpack模块解析器
  3. 项目百态:软件项目管理面面观
  4. 从EXCEL文件将数据导入数据库的向导程序设计!
  5. 深度学习数据更换背景_开始学习数据科学的最佳方法是了解其背景
  6. C语言的inline
  7. 烟花散尽漫说无(參考资料)
  8. 操作系统直接决定了计算机系统的整体性能
  9. 自定义UINavigationController push和pop动画
  10. 通过adb命令查看手机中sp xml文件
  11. TD幅度预测、幅度膨胀突破、TD通道
  12. Docker常见错误
  13. switch default多次触发
  14. java并发小说爬虫,多站点搜索下载,并实现Android客户端开发
  15. 寻找薛定谔的猫:量子物理的奇异世界
  16. mysql 时间 本周 本月_日本人脑洞最大的奇葩恋爱游戏,本周上架Steam,别在吃饭时玩...
  17. 目标检测网络之三叉戟TridentNet
  18. 英首相提交“脱欧”替代方案 重申不寻求二次公投
  19. SpringCloud停更服务一览表
  20. 计算机如何安装cpu风扇,CPU风扇安装过程图解

热门文章

  1. 数字信号处理(陈后金)课堂笔记1绪论
  2. 37页互联网+智慧园林整体解决方案
  3. Pytorch获取中间变量的梯度grad
  4. 网站扫描(Wker_网站探测工具)
  5. 加一度教你如何做好百度竞价数据分析
  6. 腾讯云tcp考试资料
  7. 中国顶级IC设计公司薪酬
  8. 超详细VMware Workstation 10安装OS X Mavericks
  9. ColorSchemer Studio 2 (专业配色软件) 中文破解版
  10. cmake:命令行工具cmake