当前位置:   article > 正文

微信小程序-生成保存图文海报-canvas生成图片、二维码、自定义文字样式_小程序canvas生成二维码海报

小程序canvas生成二维码海报

代码仓库:https://gitee.com/DerekAndroid/miniProgramAgen/tree/master/pages/canvasTest

效果:

 点击保存图片-    

//步骤流程: 1.把二维码生成临时图片文件-2.canvas画文字+二维码图片-3.保存新的临时文件-4.保存到相册

canvas图片宽度根据设备进行适配

let unit = systemInfo.screenWidth / 375

以375px基准进行宽度适配:切换iphone6和iphone6plus对面效果

iphone6:  iphone6plus: 

canvas两种方式实现文字居中:

1.measureText测量文字宽度实现文字居中

  1. /********文字居中方式1:measureText测量文字宽度实现文字居中 */
  2. ctx.setFillStyle('#000')
  3. var name = 'textAlign文字居中';
  4. ctx.font = '28px serif'
  5. var measureText = ctx.measureText(name);//测量文字宽度
  6. console.warn('measureText', measureText)
  7. //思路:(父类宽度-文字宽度)/2=文字居中
  8. //在iphone6plus上,measureText.width= 238是实际的宽度,需要先转换成iPhone6的基准尺寸,然后再计算,然后再转换成iphone6plus是尺寸
  9. ctx.fillText(name, (375 - measureText.width / unit) / 2 * unit, 150 * unit)

2.textAlign文字居中

  1. /********文字居中方式2:textAlign文字居中 */
  2. ctx.setFillStyle('#000')
  3. var name = 'textAlign文字居中';
  4. ctx.font = '28px serif'
  5. ctx.textAlign = "center";//基于中线文字左右两边居中
  6. // ctx.setTextAlign('center');
  7. ctx.fillText(name, 375 / 2 * unit, 210 * unit) //居中开始写文字
  8. ctx.textAlign = "left"; //使用完后恢复左对齐

wxml:

  1. <view>
  2. <view>生成展示二维码js库:https://github.com/yingye/weapp-qrcode</view>
  3. <canvas class="imageTestMa show" canvas-id="canvas"></canvas>
  4. </view>
  5. <button bindtap="commitTamp">保存图片</button>
  6. <!-- 1.把二维码生成临时图片文件-2.canvas画文字+二维码图片-3.保存新的临时文件-4.保存到相册 -->
  7. <canvas class="canvas" canvas-id="myCanvas"></canvas>

js:

  1. // pages/authSetting/authSetting.js
  2. import drawQrcode from './weapp.qrcode'
  3. Page({
  4. data: {
  5. },
  6. onLoad: function (options) {
  7. this.showQrcode()
  8. // this.canvasPromise()
  9. },
  10. onShow: function () {
  11. // this.drawCanvas()
  12. },
  13. async commitTamp() {
  14. wx.showLoading({
  15. title: '保存中',
  16. })
  17. setTimeout(function () {
  18. wx.hideLoading()
  19. }, 2000)
  20. let self = this;
  21. //步骤流程: 1.把二维码生成临时图片文件-2.canvas画文字+二维码图片-3.保存新的临时文件-4.保存到相册
  22. //1.把二维码生成临时图片文件
  23. var codePath = await self.canvasPromise();
  24. //2.canvas画文字+二维码图片-3.保存新的临时文件
  25. var tempFilePath = await self.drawCanvas(codePath);
  26. //4.保存到相册
  27. // var tempFilePath = self.checkScopeAsync(tempFilePath);
  28. var options = { scope: 'scope.writePhotosAlbum', content: '请前往设置页打开保存相册开关' }
  29. var flag = await this.getSettingRecord(options)
  30. if (flag) {
  31. this.saveImageToPhotosAlbum(tempFilePath)
  32. }
  33. },
  34. // 0.显示二维码
  35. showQrcode() {
  36. let systemInfo = wx.getSystemInfoSync();
  37. console.warn('systemInfo', systemInfo, this.json)
  38. let unit = systemInfo.windowWidth / 375;
  39. var text = '312wqeqwe';
  40. drawQrcode({
  41. width: 160 * unit,
  42. height: 160 * unit,
  43. canvasId: 'canvas',
  44. text: text,
  45. correctLevel: 3,
  46. callback(e) {
  47. console.warn('callback')
  48. }
  49. })
  50. },
  51. //1.把二维码生成临时图片文件
  52. canvasPromise() {
  53. var self = this;
  54. return new Promise((resolve, reject) => {
  55. let systemInfo = wx.getSystemInfoSync();
  56. let unit = systemInfo.windowWidth / 375;
  57. wx.canvasToTempFilePath({
  58. x: 0,
  59. y: 0,
  60. width: 160 * unit,
  61. height: 160 * unit,
  62. canvasId: 'canvas',
  63. success(res) {
  64. console.warn('canvas', res);
  65. resolve(res.tempFilePath)
  66. },
  67. fail(res) {
  68. console.warn('canvas=Fail', res);
  69. },
  70. })
  71. })
  72. },
  73. //2.canvas画文字+二维码图片-3.保存新的临时文件
  74. drawCanvas(codePath) {
  75. let self = this;
  76. var ctx = wx.createCanvasContext("myCanvas", self);
  77. var systemInfo = wx.getSystemInfoSync();
  78. //以375px基准进行宽度适配:切换iphone6和iphone6plus对面效果
  79. let unit = systemInfo.screenWidth / 375
  80. ctx.setFillStyle('#000')
  81. var name = '使用固定的px值';
  82. ctx.font = '22px serif'
  83. ctx.fillText(name, 10 * unit, 20 * unit)
  84. ctx.setFillStyle('#1086FF')
  85. ctx.fillRect(0, 40, 188, 10);
  86. ctx.fillRect(0, 45, 375, 5);
  87. ctx.fillRect(0, 90, 188 * unit, 10);
  88. ctx.fillRect(0, 95, 375 * unit, 5);
  89. ctx.setFillStyle('#000')
  90. var name = '根据不同屏幕动态的转换对应的px值';
  91. ctx.font = '22px serif'
  92. ctx.fillText(name, 10 * unit, 75 * unit)
  93. ctx.setFillStyle('#1086FF')
  94. ctx.fillRect(0, 125 * unit, 375 * unit, 50 * unit);
  95. ctx.setFillStyle('#000')
  96. var name = 'measureText测量文字宽度实现文字居中';
  97. ctx.font = '22px serif'
  98. ctx.fillText(name, 10 * unit, 115 * unit)
  99. ctx.setFillStyle('#000')
  100. var name = 'textAlign文字居中';
  101. ctx.font = '28px serif'
  102. var measureText = ctx.measureText(name);//测量文字宽度
  103. console.warn('measureText', measureText)
  104. //思路:(父类宽度-文字宽度)/2=文字居中
  105. //在iphone6plus上,measureText.width= 238是实际的宽度,需要先转换成iPhone6的基准尺寸,然后再计算,然后再转换成iphone6plus是尺寸
  106. ctx.fillText(name, (375 - measureText.width / unit) / 2 * unit, 150 * unit)
  107. ctx.setFillStyle('#1086FF')
  108. ctx.fillRect(0, 185 * unit, 375 * unit, 50 * unit);
  109. ctx.setFillStyle('#000')
  110. var name = 'textAlign文字居中';
  111. ctx.font = '28px serif'
  112. ctx.textAlign = "center";//基于中线文字左右两边居中
  113. // ctx.setTextAlign('center');
  114. ctx.fillText(name, 375 / 2 * unit, 210 * unit) //居中开始写文字
  115. ctx.textAlign = "left"; //使用完后恢复左对齐
  116. ctx.setFillStyle('#1086FF')
  117. ctx.fillRect(112 * unit, 310 * unit, 150 * unit, 150 * unit);
  118. //把生成好的临时二维码图片地址,画二维码
  119. codePath && ctx.drawImage(codePath, 112 * unit, 270 * unit, 150 * unit, 150 * unit)
  120. // ctx.fillRect(112*unit, 460*unit, 150*unit, 150*unit);
  121. //设置固定的高度375*667,这是设计图纸的尺寸,防止生成的图片变形
  122. let screenWidth = 375 * unit;
  123. let screenHeight = 667 * unit;
  124. //把画板内容绘制成图片,并回调 画板图片路径
  125. return new Promise((resolve, reject) => {
  126. ctx.draw(false, () => {
  127. wx.canvasToTempFilePath({
  128. x: 0,
  129. y: 0,
  130. width: screenWidth * unit,
  131. height: screenHeight * unit,
  132. destWidth: screenWidth * unit * 10, // 设置大一点清晰度会高
  133. destHeight: screenHeight * unit * 10,
  134. canvasId: 'myCanvas',
  135. success(res) {
  136. console.warn('canvasToTempFilePathRes', res);
  137. resolve(res.tempFilePath)
  138. },
  139. fail(res) {
  140. console.warn('canvasToTempFilePathResFail', res);
  141. reject()
  142. },
  143. })
  144. })
  145. })
  146. },
  147. //申请获取保存到相册权限-同步函数方式
  148. async checkScopeAsync(tempFilePath) {
  149. var options = { scope: 'scope.writePhotosAlbum', content: '请前往设置页打开保存相册开关' }
  150. var flag = await this.getSettingRecord(options)
  151. console.warn('保存相册权限flag=', flag)
  152. if (flag) {
  153. this.saveImageToPhotosAlbum(tempFilePath)
  154. }
  155. },
  156. //检查相册权限
  157. getSettingRecord(options = { scope: 'scope.record', content: '请前往设置页打开麦克风' }) {
  158. var self = this;
  159. return new Promise((resolve, reject) => {
  160. wx.getSetting({
  161. success: (res) => {
  162. let auth = res.authSetting[options.scope]
  163. console.warn('scope.record=', auth, typeof auth)
  164. if (auth === true) { // 用户已经同意授权
  165. resolve(true)
  166. }
  167. else if (auth === undefined) {// 首次发起授权
  168. wx.authorize({
  169. scope: options.scope,
  170. success() {
  171. resolve(true)
  172. },
  173. fail(res) {
  174. }
  175. })
  176. }
  177. else if (auth === false) { // 非首次发起授权,用户拒绝过 => 弹出提示对话框
  178. wx.showModal({
  179. title: '授权提示',
  180. content: options.content,
  181. success: (tipRes) => {
  182. if (tipRes.confirm) {
  183. wx.openSetting({
  184. success: (settingRes) => {
  185. if (settingRes.authSetting[options.scope]) {
  186. resolve(true)
  187. }
  188. console.warn('settingRes', settingRes)
  189. },
  190. })
  191. }
  192. }
  193. })
  194. }
  195. },
  196. })
  197. })
  198. },
  199. //画板路径保存成功后,调用方法吧图片保存到用户相册
  200. saveImageToPhotosAlbum(filePath) {
  201. let self = this;
  202. return new Promise((resolve, reject) => {
  203. wx.saveImageToPhotosAlbum({
  204. filePath: filePath,
  205. success(res) {
  206. wx.showToast({
  207. title: '已保存',
  208. icon: 'none',
  209. duration: 2000,
  210. })
  211. resolve();
  212. },
  213. fail(res) {
  214. console.warn('saveImageToPhotosAlbum-res', res);
  215. //技巧2:加强展示时间
  216. wx.showToast({
  217. title: '保存失败' + res.errMsg,
  218. icon: 'none',
  219. duration: 2000,
  220. })
  221. reject();
  222. },
  223. complete(res) {
  224. console.warn('complete-saveImageToPhotosAlbum-res', res);
  225. }
  226. })
  227. })
  228. },
  229. })

wxss:

  1. /* pages/authSetting/authSetting.wxss */
  2. .canvas{
  3. width: 750rpx;
  4. height: 1334rpx;
  5. margin-top: 20rpx;
  6. border: 1rpx solid #666;
  7. }
  8. .imageTestMa{
  9. height: 320rpx;
  10. width: 320rpx;
  11. margin: 0rpx 0rpx 40rpx 0rpx;
  12. border: 1px solid aqua;
  13. }

参考:生成展示二维码js库:https://github.com/yingye/weapp-qrcode

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

闽ICP备14008679号