• uni-app微信小程序canvas中使用canvasToTempFilePath在手机上导出图片尺寸与实际不符


    问题描述:比如图片的尺寸是1125*2001像素,这样用微信开发者工具下载下来的图片尺寸是1125*2001像素,用不同的手机去操作,下载出来的图片尺寸都不一样,和原图片尺寸差距很大。

    解决方案:canvas写入的时候是按照当前设备像素比(pixelRatio)进行设置的,像素比pixelRatio=物理像素/设备独立像素(dips)

    像素比pixelRatio=物理像素/设备独立像素(dips)

    ctx.drawImage(图片对象, 图像裁剪的x位置, 图像裁剪的y位置, 裁剪的宽度, 裁剪的高度, x位置, y位置, 宽度, 高度

    我这台机器的设备像素比=3,分辨率是:1920*1080 以x轴为例,这里的物理像素=1920,得出设备独立像素=1920/3=640,而canvas这里设置的大小是设备独立像素,所以576的物理像素对应到设备独立像素就是=576/3=192,

    微信小程序提供了wx.getSystemInfo获取设备信息,其中pixelRatio就是设备像素比.

    canvas写入的时候是按照当前设备像素比(pixelRatio)进行设置的

    1. <button class="btn-link save-link" plain="true" @click="getCanvas">
    2. 保存图片
    3. </button>
    4. <!-- canvas保存图片 -->
    5. <canvas class="share-canvas" canvas-id="canvas" :style="canvasStyle"></canvas>
    1. data() {
    2. return {
    3. posterImg: {
    4. url: 'https://picsum.photos/1125/2001?random=1',
    5. width: '1125rpx',
    6. height: '2001rpx'
    7. }
    8. }
    9. },
    1. computed: {
    2. canvasStyle() {
    3. return `width: ${this.posterImg.width}px; height:${this.posterImg.height}px;`
    4. }
    5. },
    1. methods: {
    2. getCanvas() {
    3. uni.showLoading({
    4. title: '保存中' // 加载转圈显示
    5. })
    6. const ctx = uni.createCanvasContext('canvas', this)
    7. const that = this
    8. // 获取背景海报详细信息
    9. uni.getImageInfo({
    10. src: that.swiper2List[that.current].image,
    11. success: function(res) {
    12. // 微信小程序提供了wx.getSystemInfo获取设备信息,其中pixelRatio就是设备像素比
    13. wx.getSystemInfo({
    14. success: function(data) {
    15. // canvas写入的时候是按照当前设备像素比(pixelRatio)进行设置的
    16. const pixelRatio = data.pixelRatio
    17. that.posterImg.width = res.width / pixelRatio
    18. that.posterImg.height = res.height / pixelRatio
    19. // 绘制背景海报
    20. ctx.drawImage(res.path, 0, 0, that.posterImg.width, that.posterImg.height)
    21. that.saveCanvas()
    22. }
    23. })
    24. }
    25. })
    26. },
    27. // 保存canvas为图片
    28. saveCanvas() {
    29. const _this = this
    30. setTimeout(() => {
    31. uni.canvasToTempFilePath({
    32. canvasId: 'canvas',
    33. quality: 1,
    34. success(result) {
    35. // 保存在本地
    36. uni.saveImageToPhotosAlbum({
    37. filePath: result.tempFilePath,
    38. success: function() {
    39. uni.hideLoading()
    40. uni.showToast({
    41. title: '保存成功',
    42. icon: 'success',
    43. duration: 2000
    44. })
    45. console.log('save success')
    46. },
    47. fail: () => {
    48. uni.hideLoading()
    49. _this.setData({
    50. flag: false
    51. })
    52. }
    53. })
    54. },
    55. fail: () => {
    56. uni.hideLoading()
    57. // 重复请求一次
    58. _this.saveCanvas()
    59. }
    60. })
    61. }, 200)
    62. }
    63. }
  • 相关阅读:
    patient feature-based softmax embedding
    【算法-数组1】二分查找 和 移除元素
    不可变集合的详细概述
    【测试】用例测试设计方法
    你真的知道什么是正弦和余弦吗?使用 Python 和 Turtle 可视化数学
    flink-cdc同步mysql数据到kafka
    LeetCode530. Minimum Absolute Difference in BST
    软考证书含金量
    hadoop宕机的处理方法
    代码随想录算法训练营第六十天丨 单调栈03
  • 原文地址:https://blog.csdn.net/zhangjing1019/article/details/133040333