当前位置:   article > 正文

uniapp-微信小程序 长截图功能,此功能价值5000~1W_uniapp 长截图

uniapp 长截图

1,首先说明一下,我之前遇到一个需求 让我点击一个按钮可以对着当前整个页面进行长截图

这个功能折磨了我一个星期 之后我去问了某宝的人 百分之70说 做不出来,剩下的开会后收我一万元,我就呵呵呵了 于是我苦心钻研 后 借助一个插件(自己修改了些代码)完成了这一个功能,分享给大家----前人栽树 后人乘凉!

---------------------------------------------------------------------------------------------------------------------------------

正式开始

第一步

uniapp插件市场下载导入到你的项目中(感谢这位插件作者)

tk-view2canvas 页面截屏组件 - DCloud 插件市场

安装完成后 里面的代码需要改写--以下是我的改写后的 你可以直接复制粘贴代替掉他的

  1. <template>
  2. <view>
  3. </view>
  4. </template>
  5. <script>
  6. let promiseIndex = 0
  7. let promiseArray = []
  8. let imageArray = []
  9. let imageIndex = 0
  10. export default {
  11. name: "kt-view2canvas",
  12. data() {
  13. return {
  14. };
  15. },
  16. props: {
  17. canvasWidth: {
  18. type: Number,
  19. default: 1080
  20. },
  21. canvasHeight: {
  22. type: Number,
  23. default: 2500
  24. }
  25. },
  26. methods: {
  27. //rpx转换为px的工具函数
  28. rpxtopx(rpx) {
  29. let deviceWidth = wx.getSystemInfoSync().windowWidth; //获取设备屏幕宽度
  30. let px = (deviceWidth / 750) * Number(rpx)
  31. return Math.floor(px);
  32. },
  33. drawCanvas(arrayInfo) {
  34. promiseIndex = 0
  35. promiseArray = []
  36. imageArray = []
  37. imageIndex = 0
  38. uni.showLoading({
  39. title: '保存中...'
  40. })
  41. // 0.创建canvas及ctx,并获取屏幕的dpr
  42. const canvas = wx.createOffscreenCanvas({
  43. type: '2d',
  44. width: this.canvasWidth,
  45. height: this.canvasHeight
  46. })
  47. const ctx = canvas.getContext('2d')
  48. const dpr = uni.getWindowInfo().pixelRatio
  49. // 1.根据数组数量创建promise数组
  50. for (let i = 0; i < arrayInfo.length; i++) {
  51. promiseArray.push(promiseIndex)
  52. promiseIndex++
  53. }
  54. // 2.根据数组给promise数组赋值
  55. let that = this
  56. for (let i = 0; i < arrayInfo.length; i++) {
  57. promiseArray[i] = new Promise(function(resolve, reject) {
  58. if (arrayInfo[i].type == 'text') {
  59. resolve()
  60. } else if (arrayInfo[i].type == 'view') {
  61. resolve()
  62. } else if (arrayInfo[i].type == 'image') {
  63. let img = canvas.createImage()
  64. img.src = arrayInfo[i].src
  65. img.onload = function() {
  66. imageArray.push(img)
  67. resolve()
  68. }
  69. img.src = arrayInfo[i].src + `?${new Date().getTime()}`
  70. }
  71. })
  72. }
  73. // 3.异步全部加载完成后,绘制图片
  74. let p = Promise.all(promiseArray)
  75. p.then(function() {
  76. //根据数组顺序绘制图片
  77. for (let i = 0; i < arrayInfo.length; i++) {
  78. if (arrayInfo[i].type == 'text') {
  79. ctx.fillStyle = arrayInfo[i].color
  80. ctx.font = that.rpxtopx(arrayInfo[i].size) * dpr + 'px sans-serif'
  81. ctx.textBaseline = 'top'
  82. ctx.fillText(arrayInfo[i].word, arrayInfo[i].posX * dpr, (arrayInfo[i].posY + that
  83. .rpxtopx(20)) * dpr)
  84. } else if (arrayInfo[i].type == 'view') {
  85. ctx.fillStyle = arrayInfo[i].bgColor
  86. ctx.fillRect(arrayInfo[i].posX * dpr, arrayInfo[i].posY * dpr, arrayInfo[i].width *
  87. dpr, arrayInfo[i].height * dpr)
  88. } else if (arrayInfo[i].type == 'image') {
  89. ctx.drawImage(imageArray[imageIndex], arrayInfo[i].posX * dpr, arrayInfo[i].posY * dpr,
  90. arrayInfo[i].width * dpr, arrayInfo[i].height * dpr)
  91. imageIndex++
  92. }
  93. }
  94. const fileData = canvas.toDataURL()
  95. const fs = uni.getFileSystemManager()
  96. const path = `${wx.env.USER_DATA_PATH}/` + new Date().getTime() + `hello.png`
  97. //数据归零
  98. promiseIndex = 0
  99. promiseArray = []
  100. imageArray = []
  101. imageIndex = 0
  102. fs.writeFile({
  103. filePath: path,
  104. data: fileData.replace(/^data:image\/\w+;base64,/, ""),
  105. encoding: 'base64',
  106. success(res) {
  107. uni.showShareImageMenu({
  108. path: path,
  109. success: res => {
  110. console.log(res);
  111. }
  112. })
  113. },
  114. fail(res) {
  115. console.error(res)
  116. },
  117. complete() {
  118. uni.hideLoading()
  119. }
  120. })
  121. })
  122. }
  123. }
  124. }
  125. </script>
  126. <style>
  127. </style>

第二步

去到你需要截图的页面   引入(如下图)

  * canvasWidth 是你画布的宽度 ----   canvasHeight 是你画布的高度(他们决定着你画出来图片的宽高 后期可以自行调整)

第三步

把canvasWidth     canvasHeight  写进data中

第四步

建立触发事件--我这里就拿点击事件举例子  

  1. view中
  2. <button @click="toCanvas" /> 这是触发 图片生成的事件
  3. JavaScript
  4. toCanvas() {
  5. let that = this
  6. arrayInfo = []
  7. // 1.先查询要绘制的view,image,text相关属性数据
  8. uni.createSelectorQuery().selectAll('.queryInfo').boundingClientRect().exec((res) => {
  9. // console.log('获取到信息:', res[0])
  10. let resArray = res[0]
  11. for (let i = 0; i < resArray.length; i++) {
  12. //1.判断查询到的组件类别,如果是view类,就按照view对应的格式将创建的对象添加到数组中
  13. if (resArray[i].dataset.type == 'view') {
  14. // console.log('view')
  15. let bgColor = resArray[i].dataset.bgcolor
  16. let width = resArray[i].width
  17. let height = resArray[i].height
  18. let posX = resArray[i].left
  19. let posY = resArray[i].top
  20. arrayInfo.push({
  21. type: 'view',
  22. width: width,
  23. height: height,
  24. bgColor: bgColor,
  25. posX: posX,
  26. posY: posY
  27. })
  28. } //2.判断查询到的组件类别,如果是image类,就按照image对应的格式将创建的对象添加到数组中
  29. else if (resArray[i].dataset.type == 'image') {
  30. // console.log('image')
  31. let type = 'image'
  32. let src = resArray[i].dataset.src
  33. let posX = resArray[i].left
  34. let posY = resArray[i].top
  35. let width = resArray[i].width
  36. let height = resArray[i].height
  37. arrayInfo.push({
  38. type: 'image',
  39. src: src,
  40. width: width,
  41. height: height,
  42. posX: posX,
  43. posY: posY
  44. })
  45. } //3.判断查询到的组件类别,如果是text类,就按照text对应的格式将创建的对象添加到数组中
  46. else if (resArray[i].dataset.type == 'text') {
  47. // console.log('text')
  48. let type = 'text'
  49. let color = resArray[i].dataset.color
  50. let word = resArray[i].dataset.word
  51. let size = resArray[i].dataset.size
  52. let posX = resArray[i].left
  53. let posY = resArray[i].top
  54. arrayInfo.push({
  55. type: 'text',
  56. color: color,
  57. word: word,
  58. size: size,
  59. posX: posX,
  60. posY: posY
  61. })
  62. }
  63. }
  64. //查询完毕后,也就创建好了arrayInfo数组,下面将arrayInfo数组作为参数,传给组件的drawCanvas()方法
  65. that.$refs.tkCanvas.drawCanvas(arrayInfo)
  66. })
  67. },

事件里面的代码你也可以先复制粘贴我的 后面你自己慢慢研究

第五步(最繁琐的一步-加油你快成功了)

开始 设置你要绘制的当前页的内容(需要出现在图片上的内容就 设置  不需要的就不设置,不设置的就不会在图片上出现)

1,只要是view  就在盒子里加上  并且 在class中必须加入 queryInfo

(需要出现在图片上的内容就加 不需要的不加 你之前有的class内容 不动他)

<view class="queryInfo" data-type="view" />---data-type="view" 是他是什么类型的盒子必写

2,颜色设置  -- data-bgcolor="#ffffff"     data-color ="#000" 

<view class="queryInfo" data-type="view" data-bgcolor="#ffffff"     data-color ="#000"  />

3,所有文字必须用text包裹起来 且和上面一样 clas,也是必须加 还有内容要放在data中切记

<text class="queryInfo" data-type="text" :data-word="xuexiaoname" data-font-weight="800" data-margin-top="60"  data-color="#000" data-size="45">{{xuexiaoname}}</text>

---样式前面必须加data-    text盒子 必须加data-type="text"   文字内容必须在data中 要配合:data-word="xuexiaoname"  切记不然没办法画出来

4,图片内容 image

<image class="queryInfo" data-type="image" :data-src="item" :src="item" mode=""></image>

:data-src="这里是图片地址 可以是本地 也可以是 网络(我这里前面遍历了你随意)"

:src="和上面一样"

图片就写 data-type="image"   而且class里面的 queryInfo不能少

到这里就结束了,切记  不需要现实的 就原封不动不管他  需要显示在图片上上的内容 就按上面的操作,生成的图片大小不合适 可以调整data中的 canvasWidth     canvasHeight。

如果您觉得有用 麻烦帮我点个赞收藏一下,还是老话--前人栽树 后人乘凉

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/940429
推荐阅读
相关标签
  

闽ICP备14008679号