经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » 微信小程序 » 查看文章
微信小程序调用摄像头实现拍照功能
来源:jb51  时间:2022/7/19 9:55:49  对本文有异议

本文实例为大家分享了微信小程序调用摄像头实现拍照的具体代码,供大家参考,具体内容如下

微信小程序开发文档

首先,需要用户授权摄像头权限,这一步是必须的

具体步骤:

1、获取用户当前授权状态,看是否已经授权,如果已经授权直接显示摄像头
2、如果用户还没有授权,则调起授权弹框,用户允许授权则显示摄像头
3、如果用户不允许,则提示用户去设置页面打开摄像头权限

用户授权之后,就可以进行拍摄了,微信的camera组件无法显示为圆形,我这里是用一张图片遮盖了

上代码:

wxml:

  1. <view class='camera'>
  2. ? <image src="/images/border.png" mode="widthFix"></image>
  3. ? <camera wx:if="{{isAuth}}" device-position="back" flash="off" binderror="error"></camera>
  4. </view>
  5. <button class="takePhoto" type="primary" bindtap="takePhoto">拍照</button>

wxss:

  1. .camera {
  2. ? width: 430rpx;
  3. ? height: 430rpx;
  4. ? border-radius: 50%;
  5. ? margin: 20px auto 0;
  6. ? position: relative;
  7. }
  8.  
  9. .camera image {
  10. ? position: absolute;
  11. ? width: 100%;
  12. ? height: 100%;
  13. ? z-index: 10;
  14. }
  15.  
  16. .camera camera {
  17. ? width: 428rpx;
  18. ? height: 428rpx;
  19. }
  20.  
  21. button.takePhoto:not([size='mini']) {
  22. ? position: fixed;
  23. ? bottom: 0;
  24. ? left: 0;
  25. ? width: 100vw;
  26. ? height: 90rpx;
  27. ? border-radius: 0;
  28. }

js:

  1. Page({
  2. ? data: {
  3. ? ? isAuth: false,
  4. ? ? src: ''
  5. ? },
  6. ? onLoad() {
  7. ? ? const _this = this
  8. ? ? wx.getSetting({
  9. ? ? ? success: res => {
  10. ? ? ? ? if (res.authSetting['scope.camera']) {
  11. ? ? ? ? ? // 用户已经授权
  12. ? ? ? ? ? _this.setData({
  13. ? ? ? ? ? ? isAuth: true
  14. ? ? ? ? ? })
  15. ? ? ? ? } else {
  16. ? ? ? ? ? // 用户还没有授权,向用户发起授权请求
  17. ? ? ? ? ? wx.authorize({
  18. ? ? ? ? ? ? scope: 'scope.camera',
  19. ? ? ? ? ? ? success() { // 用户同意授权
  20. ? ? ? ? ? ? ? _this.setData({
  21. ? ? ? ? ? ? ? ? isAuth: true
  22. ? ? ? ? ? ? ? })
  23. ? ? ? ? ? ? },
  24. ? ? ? ? ? ? fail() { // 用户不同意授权
  25. ? ? ? ? ? ? ? _this.openSetting().then(res => {
  26. ? ? ? ? ? ? ? ? _this.setData({
  27. ? ? ? ? ? ? ? ? ? isAuth: true
  28. ? ? ? ? ? ? ? ? })
  29. ? ? ? ? ? ? ? })
  30. ? ? ? ? ? ? }
  31. ? ? ? ? ? })
  32. ? ? ? ? }
  33. ? ? ? },
  34. ? ? ? fail: res => {
  35. ? ? ? ? console.log('获取用户授权信息失败')
  36. ? ? ? }
  37. ? ? })
  38. ? },
  39.  
  40. ? // 打开授权设置界面
  41. ? openSetting() {
  42. ? ? const _this = this
  43. ? ? let promise = new Promise((resolve, reject) => {
  44. ? ? ? wx.showModal({
  45. ? ? ? ? title: '授权',
  46. ? ? ? ? content: '请先授权获取摄像头权限',
  47. ? ? ? ? success(res) {
  48. ? ? ? ? ? if (res.confirm) {
  49. ? ? ? ? ? ? wx.openSetting({
  50. ? ? ? ? ? ? ? success(res) {
  51. ? ? ? ? ? ? ? ? if (res.authSetting['scope.camera']) { // 用户打开了授权开关
  52. ? ? ? ? ? ? ? ? ? resolve(true)
  53. ? ? ? ? ? ? ? ? } else { // 用户没有打开授权开关, 继续打开设置页面
  54. ? ? ? ? ? ? ? ? ? _this.openSetting().then(res => {
  55. ? ? ? ? ? ? ? ? ? ? resolve(true)
  56. ? ? ? ? ? ? ? ? ? })
  57. ? ? ? ? ? ? ? ? }
  58. ? ? ? ? ? ? ? },
  59. ? ? ? ? ? ? ? fail(res) {
  60. ? ? ? ? ? ? ? ? console.log(res)
  61. ? ? ? ? ? ? ? }
  62. ? ? ? ? ? ? })
  63. ? ? ? ? ? } else if (res.cancel) {
  64. ? ? ? ? ? ? _this.openSetting().then(res => {
  65. ? ? ? ? ? ? ? resolve(true)
  66. ? ? ? ? ? ? })
  67. ? ? ? ? ? }
  68. ? ? ? ? }
  69. ? ? ? })
  70. ? ? })
  71. ? ? return promise;
  72. ? },
  73.  
  74. ? takePhoto() {
  75. ? ? const ctx = wx.createCameraContext()
  76. ? ? ctx.takePhoto({
  77. ? ? ? quality: 'high',
  78. ? ? ? success: (res) => {
  79. ? ? ? ? this.setData({
  80. ? ? ? ? ? src: res.tempImagePath
  81. ? ? ? ? })
  82. ? ? ? ? wx.previewImage({
  83. ? ? ? ? ? current: res.tempImagePath, // 当前显示图片的http链接
  84. ? ? ? ? ? urls: [res.tempImagePath] // 需要预览的图片http链接列表
  85. ? ? ? ? })
  86. ? ? ? }
  87. ? ? })
  88. ? }
  89. })

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号