当前位置: 首页 > news >正文

微信小程序uniapp开发附源码——长图拼接

开发三十个纯前端微信小程序工具之长图拼接(30/5)

一个开箱即用的长图拼接工具小程序

长图拼接页面截图
在这里插入图片描述

长图拼接采用canvas实现

支持垂直方向、水平方向图片拼接,支持设置图片间距
项目是使用uniapp开发的,下面直接贴一下核心代码:

长图拼接

<!-- 隐藏的canvas -->
<canvas type="2d" id="stitchCanvas" class="hidden-canvas"></canvas>
drawStitchedImage(images) {console.log('images', images);let canvasWidth, canvasHeight;if (this.direction === 'vertical') {// 纵向拼接:使用固定宽度400,高度自适应,避免垂直方向图片过大const fixedWidth = 400;const scaledHeights = images.map(img => img.height * (fixedWidth / img.width));canvasWidth = fixedWidth;canvasHeight = scaledHeights.reduce((sum, h) => sum + h, 0) + (images.length - 1) * this.spacing;} else {// 横向拼接:高度固定400,宽度自适应const fixedHeight = 400;const scaledWidths = images.map(img => img.width * (fixedHeight / img.height));canvasWidth = scaledWidths.reduce((sum, w) => sum + w, 0) + (images.length - 1) * this.spacing;canvasHeight = fixedHeight;}// 限制最大尺寸,避免内存问题const maxSize = 4000;if (canvasWidth > maxSize || canvasHeight > maxSize) {const scale = Math.min(maxSize / canvasWidth, maxSize / canvasHeight);canvasWidth = Math.floor(canvasWidth * scale);canvasHeight = Math.floor(canvasHeight * scale);}const query = uni.createSelectorQuery().in(this);query.select('#stitchCanvas').fields({ node: true, size: true }).exec((res) => {if (res[0]) {const canvas = res[0].node;const ctx = canvas.getContext('2d');const dpr = uni.getSystemInfoSync().pixelRatio;canvas.width = canvasWidth * dpr;canvas.height = canvasHeight * dpr;ctx.scale(dpr, dpr);ctx.fillStyle = '#ffffff';ctx.fillRect(0, 0, canvasWidth, canvasHeight);let currentX = 0, currentY = 0;// 按顺序依次加载并绘制图片,确保顺序一致const drawSequentially = async () => {for (let i = 0; i < images.length; i++) {const img = images[i];await new Promise((resolve, reject) => {const imageObj = canvas.createImage();imageObj.onload = () => {let drawWidth, drawHeight, x, y;if (this.direction === 'vertical') {drawWidth = canvasWidth;drawHeight = img.height * (canvasWidth / img.width);x = 0;y = currentY;ctx.drawImage(imageObj, x, y, drawWidth, drawHeight);currentY += drawHeight + this.spacing;} else {drawHeight = canvasHeight;drawWidth = img.width * (canvasHeight / img.height);x = currentX;y = 0;ctx.drawImage(imageObj, x, y, drawWidth, drawHeight);currentX += drawWidth + this.spacing;}resolve();};imageObj.onerror = (err) => {console.error('图片加载失败:', err);uni.hideLoading();uni.showToast({title: `${i + 1}张图片加载失败`,icon: 'none'});reject(err);};imageObj.src = img.path;});}// 全部绘制完成后导出setTimeout(() => {uni.canvasToTempFilePath({canvas: canvas,quality: 0.8,fileType: 'jpg',success: (res) => {this.resultPath = res.tempFilePath;uni.hideLoading();uni.showToast({title: '拼接完成',icon: 'success'});},fail: (err) => {console.error('canvas导出失败:', err);uni.hideLoading();uni.showToast({title: '拼接失败',icon: 'none'});}}, this);}, 100);};drawSequentially();} else {uni.hideLoading();uni.showToast({title: 'Canvas初始化失败',icon: 'none'});}});},

打代码不易,希望对你有帮助

源码链接:https://gitee.com/chenchongk/tool-box-stitch.git

有兴趣的同学可以在线体验一下小程序【口袋工具包】,还有更多好玩的功能,后续会继续开源源码

http://www.dtcms.com/a/600797.html

相关文章:

  • MySQL【表的内外连接】
  • 名字姓名起名打分评分抖音快手微信小程序看广告流量主开源
  • Windows下使用 Docker 安装MySQL
  • 微信小程序里用 setData() 修改数据并打印输出 的几种写法
  • 微信小程序map组件聚合簇样式自定义
  • 河北住房和城乡建设厅网站电话海报设计图片简单
  • 好的俄文网站设计大学学风建设专题网站
  • 领域驱动设计系列文章汇总
  • C++11拓展语法
  • 智慧医疗:FHIR R5、联邦学习与MLOps三位一体的AI产品化实战指南(下)
  • 创建一个达梦库需要多大空间
  • Redis_11_类型补充+命令补充+RESP
  • 网站设计哪家便宜seo网站做推广公司
  • 用于感知图像超分辨率的自编码监督(易于理解版本)
  • 地图可视化实践录:空间分析库Turf.js的学习
  • 长沙制作网站公司哪家好广州seo推广营销
  • 11、prometheus-PromQL-5-聚合计算函数
  • (114页PPT)上海x友集团管理咨询及IT规划项目第一期报告管理诊断分析咨询报告(附下载方式)
  • C语言编译器 Visual Studio:实现高效编程与调试环境优化
  • 王树森深度强化学习 DRL(六)连续控制 DDPG + 随机策略
  • 【SatWetCH4 第一期】全球湿地甲烷排放通量估算过程模型 SatWetCH4 介绍
  • Opencv(十一) : 图像缩放
  • 开源 Objective-C IOS 应用开发(四)Xcode工程文件结构
  • 儿童网站 源码html5网站开发教学
  • 编译类语言的特点与应用
  • Python 数组使用方法总结
  • 网站风格变化免费logo在线制作头像
  • 第三章深度解析:智能体“大脑”的底层逻辑——大语言模型技术基石全拆解
  • 100个云计算基础知识
  • 对比 DeepSeek(MLA)、Qwen 和 Llama 系列大模型在 Attention 架构/算法层面的核心设计及理解它们的本质区别。