经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » 微信小程序 » 查看文章
微信小程序实现拍照和相册选取图片
来源:jb51  时间:2021/5/10 8:45:49  对本文有异议

本文实例为大家分享了微信小程序实现拍照和相册选取图片的具体代码,供大家参考,具体内容如下

布局:

  1. <!--pages/camera/camera.wxml-->
  2. <view
  3. class="tui-menu-list"
  4. id="view1"
  5. style="display:flex;flex-direction:space-between">
  6. <button
  7. id="b1"
  8. size="mini"
  9. type="primary"
  10. bindtap="chooseimage">
  11. 获取图片
  12. </button>
  13. <button
  14. id="b2"
  15. size="mini"
  16. type="primary"
  17. bindtap="savePhone">
  18. 保存
  19. </button>
  20. </view>
  21. <image
  22. src="{{tempFilePaths}}"
  23. catchtap="chooseImageTap"
  24. mode="aspectFit"
  25. style="width:100%;height:400px;background-color:#ffffff;">
  26. </image>

样式:

  1. /* pages/camera/camera.wxss */
  2. .view1 {
  3. height: 25px
  4. }
  5. .tui-menu-list {
  6. display: flex;
  7. flex-direction: row;
  8. margin: 20rpx;
  9. padding: 20rpx;
  10. }

获取图片路径,显示图片和保存

  1. // pages/camera/camera.js
  2. Page({
  3. data: {
  4. tempFilePaths: 'http://pic2.cxtuku.com/00/01/08/b207004f7104.jpg'
  5. },
  6. chooseimage: function () {
  7. var that = this;
  8. wx.showActionSheet({
  9. itemList: ['从相册选择', '拍照'],
  10. itemColor: "#CED63A",
  11. success: function (res) {
  12. if (!res.cancel) {
  13. if (res.tapIndex == 0) {
  14. that.chooseWxImage('album')
  15. } else if (res.tapIndex == 1) {
  16. that.chooseWxImage('camera')
  17. }
  18. }
  19. }
  20. })
  21. },
  22. chooseWxImage: function (type) {
  23. var that = this
  24. wx.chooseImage({
  25. sizeType: ['original', 'compressed'],
  26. sourceType: [type],
  27. count: 1,
  28. success: function (res) {
  29. console.log(res)
  30. that.setData({
  31. tempFilePaths: res.tempFilePaths[0],
  32. })
  33. }
  34. })
  35. },
  36. savePhone: function () {
  37. wx.getImageInfo({
  38. src: this.data.tempFilePaths,
  39. success: function (res) {
  40. var path = res.path
  41. wx.saveImageToPhotosAlbum({
  42. filePath: path,
  43. success: function () {
  44. wx.showToast({
  45. title: '保存成功',
  46. })
  47. },
  48. fail: function (res) {
  49. wx.showToast({
  50. title: '保存失败',
  51. icon: 'none'
  52. })
  53. }
  54. })
  55. }
  56. })
  57. }
  58. })

效果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持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号