记录--uni-app实现京东canvas拍照识图功能

2023-10-13 09:40

本文主要是介绍记录--uni-app实现京东canvas拍照识图功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助

最近公司出了一个新的功能模块(如下图),大提上可以描述为实现拍照完上传图片,拖动四方框拍照完成上传功能,大体样子如下图。但是我找遍了 dcloud 插件市场,找到的插件都是移动背景图片来实现裁剪的,跟京东的功能是相反的,没办法只能自己来实现这么一个插件。

第一步

首先就需要实现一个四方框的功能了。从上图可知,四方框有一下几个特点

  1. 四个角粘连外框,随着框的大小和移动范围紧缚移动
  2. 四方框可随意四个方向拖动
  3. 方框外区域阴影不影响方框内

那么我们根据这个特性来实现下这个功能,对于 css 规范的话使用 bem 规范

<div class="clip__content"><div v-for="(item, index) in 4" :key="index" class="clip__edge"></div>
</div>

/more

$edge-border-width: 6rpx;
.clip {&__content {position: fixed;width: 400rpx;height: 400rpx;left: 0;top: 0;border: 1px solid red;z-index: 4;overflow: hidden;box-shadow: rgba(0, 0, 0, 0.5) 0 0 0 200vh;}&__edge {position: absolute;width: 34rpx;height: 34rpx;border: 10rpx solid red;pointer-events: auto;z-index: 2;&::before {content: "";position: absolute;z-index: 2;width: 40rpx;height: 40rpx;background-color: transparent;}&:nth-child(1) {left: $edge-border-width;top: $edge-border-width;border-bottom-width: 0 !important;border-right-width: 0 !important;&:before {top: -50%;left: -50%;}}&:nth-child(2) {right: $edge-border-width;top: $edge-border-width;border-bottom-width: 0 !important;border-left-width: 0 !important;&:before {top: -50%;left: 50%;}}&:nth-child(3) {left: $edge-border-width;bottom: $edge-border-width;border-top-width: 0 !important;border-right-width: 0 !important;&:before {bottom: -50%;left: -50%;}}&:nth-child(4) {right: $edge-border-width;bottom: $edge-border-width;border-top-width: 0 !important;border-left-width: 0 !important;&:before {bottom: -50%;left: 50%;}}}

根据上面的 html 和 css 出来的样式大概如下图 外部的阴影效果我们用: box-shadow: rgba(0, 0, 0, 0.5) 0 0 0 200vh 来达成

第二步

第二步的话就要实现移动功能了,这里是一个比较考验耐心的地方,因为涉及到多个方向的变化,需要不断地进行调试,在此之前需要先分析下四个角变化的特性,下面先看 4 个角的移动特性(以 H5 思维)

  1. 第一个角的移动会改变方框的 left,top,width,right4 个值
  2. 第二个角的移动会改变方框的 top,with,height3 个值
  3. 第三个角的移动会改变方框的 left, width,height3 个值
  4. 第四个角的移动会改变方框的 width,height2 个值
  5. 四个角的移动都不能小于 4 个角的宽高,四个角的移动都不能超过屏幕,相应的逻辑需要做一下限制

首先需要获取下屏幕宽度,区域高度(因为头部可能会有导航栏目占位,所以不拿屏幕高度),四方框初始宽高,

uni.getSystemInfo({success: res => {console.log(res)this.systemInfo = res}
})
uni.createSelectorQuery().select('.clip__content').fields({ size: true }, data => {this.width = data.widththis.height = data.height}).exec()
uni.createSelectorQuery().select('.clip').fields({ size: true }, data => {this.screenHeight = data.height}).exec()

后续的话就可以进行四个角拖拽了,这里用到了 touchStart 和 touchMove,动态地为方框绑定样式

<divv-for="(item, index) in 4"class="clip__edge"@touchstart.stop.prevent="edgeTouchStart"@touchmove.stop.prevent="e => edgeTouchMove(e, index)"@touchend.stop.prevent="edgeTouchEnd"
></div>

接下来开始写逻辑

edgeTouchStart(e) {// 记录坐标xy初始位置this.clientX = e.changedTouches[0].clientX;this.clientY = e.changedTouches[0].clientY;
},
edgeTouchMove(e, index) {const currX = e.changedTouches[0].clientX;const currY = e.changedTouches[0].clientY;// 记录坐标差const moveX = currX - this.clientX;const moveY = currY - this.clientY;// 更新坐标位置this.clientX = currX;this.clientY = currY;const { width, height, left, top, screenHeight } = this;const { screenWidth } = this.systemInfo;// 初始化最大宽高let maxWidth = 0,maxHeight = 0,maxTop = top + moveY < 0 ? 0 : top + moveY,maxLeft = left + moveX < 0 ? 0 : left + moveX;// 四个角的宽高限制if (index % 2 === 0) {maxWidth = width - moveX > screenWidth ? screenWidth : width - moveX;} else {maxWidth = width + moveX > screenWidth ? screenWidth : width + moveX;}if (index < 2) {maxHeight =height - moveY > screenHeight ? screenHeight : height - moveY;} else {maxHeight =height + moveY > screenHeight ? screenHeight : height + moveY;}// 四个角的规则计算逻辑 四边方框暂定40 更详细的要用.createSelectorQuery()去拿if (index === 0) {if (width - moveX <= 40 || height - moveY <= 40) return;console.log(maxLeft);this.clipStyle = {width: maxWidth,height: maxHeight,left: maxLeft,top: maxTop,};this.width = maxWidth;this.height = maxHeight;this.top = maxTop;this.left = maxLeft;// 右上角} else if (index === 1) {if (width + moveX <= 40 || height - moveY <= 40) return;this.clipStyle = {width: maxWidth,height: maxHeight,left,top: maxTop,};this.width = maxWidth;this.height = maxHeight;this.top = maxTop;} else if (index === 2) {if (width - moveX <= 40 || height + moveY <= 40) return;this.clipStyle = {width: maxWidth,height: maxHeight,left: maxLeft,top,};this.width = maxWidth;this.height = maxHeight;this.left = maxLeft;} else if (index === 3) {if (width + moveX <= 40 || height + moveY <= 40) return;this.clipStyle = {width: maxWidth,height: maxHeight,left,top,};this.width = maxWidth;this.height = maxHeight;}
}

效果如下图

第三步

四个角拖拽逻辑完善之后,下一步目标就是做四方框的拖拽,这边需要对四方框的拖拽做一次限制

<divclass="clip__content":style="style"@touchstart.stop.prevent="clipTouchStart"@touchmove.stop.prevent="clipTouchMove"
>...
</div>
clipTouchStart(e) {this.touchX = e.changedTouches[0].pageX;this.touchY = e.changedTouches[0].pageY;
},
clipTouchMove(e) {const { screenWidth } = this.systemInfo;const currX = e.changedTouches[0].pageX;const currY = e.changedTouches[0].pageY;const moveX = currX - this.touchX;const moveY = currY - this.touchY;this.touchX = currX;this.touchY = currY;// 边框限制逻辑if (this.left + moveX < 0) {this.left = 0;} else if (this.left + moveX > screenWidth - this.width) {this.left = screenWidth - this.width;} else {this.left = this.left + moveX;}if (this.top + moveY < 0) {this.top = 0;} else if (this.top + moveY > this.screenHeight - this.height) {this.top = this.screenHeight - this.height;} else {this.top = this.top + moveY;}this.clipStyle = {...this.clipStyle,left: this.left,top: this.top,};
},

效果如下图:

第四步就是做我们的截图了,这里用到了 canvas

<div class="clip__content">...<canvas class="clip-canvas" canvas-id="clip-canvas"></canvas>
</div>

逻辑的话目前这个例子是使用了网络的 url 图片 所以要进行 download,如果是不用网络图片,那么这一句可以删除换成其他的获取图片 api

initCanvas() {uni.showLoading({title: "加载中...",});uni.createSelectorQuery().select(".clip__content").fields({size: true,scrollOffset: true,rect: true,context: true,computedStyle: ["transform", "translateX"],scrollOffset: true,},(data) => {uni.downloadFile({url: this.imageUrl,success: (res) => {this.canvasInstance = uni.createCanvasContext("clip-canvas",this);this.canvasInstance.drawImage(res.tempFilePath,-data.left,-data.top,this.systemInfo.screenWidth,this.screenHeight,0,0);this.canvasInstance.draw(false,(() => {setTimeout(() => {uni.canvasToTempFilePath({x: 0,y: 0,width: data.width,height: data.height,dWidth: data.width,dHeight: data.height,fileType: "jpg",canvasId: "clip-canvas",success: (data) => {uni.hideLoading();this.url = data.tempFilePath;// this.canvasInstance.save();},},this);}, 500);})());},});}).exec();
},

效果如图所示:

本文转载于:

https://juejin.cn/post/6971977095652048910

如果对您有所帮助,欢迎您点个关注,我会定时更新技术文档,大家一起讨论学习,一起进步。

 

这篇关于记录--uni-app实现京东canvas拍照识图功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/202341

相关文章

Linux下删除乱码文件和目录的实现方式

《Linux下删除乱码文件和目录的实现方式》:本文主要介绍Linux下删除乱码文件和目录的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下删除乱码文件和目录方法1方法2总结Linux下删除乱码文件和目录方法1使用ls -i命令找到文件或目录

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

mybatis执行insert返回id实现详解

《mybatis执行insert返回id实现详解》MyBatis插入操作默认返回受影响行数,需通过useGeneratedKeys+keyProperty或selectKey获取主键ID,确保主键为自... 目录 两种方式获取自增 ID:1. ​​useGeneratedKeys+keyProperty(推

Spring Boot集成Druid实现数据源管理与监控的详细步骤

《SpringBoot集成Druid实现数据源管理与监控的详细步骤》本文介绍如何在SpringBoot项目中集成Druid数据库连接池,包括环境搭建、Maven依赖配置、SpringBoot配置文件... 目录1. 引言1.1 环境准备1.2 Druid介绍2. 配置Druid连接池3. 查看Druid监控

Linux在线解压jar包的实现方式

《Linux在线解压jar包的实现方式》:本文主要介绍Linux在线解压jar包的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux在线解压jar包解压 jar包的步骤总结Linux在线解压jar包在 Centos 中解压 jar 包可以使用 u

c++ 类成员变量默认初始值的实现

《c++类成员变量默认初始值的实现》本文主要介绍了c++类成员变量默认初始值,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录C++类成员变量初始化c++类的变量的初始化在C++中,如果使用类成员变量时未给定其初始值,那么它将被

Qt使用QSqlDatabase连接MySQL实现增删改查功能

《Qt使用QSqlDatabase连接MySQL实现增删改查功能》这篇文章主要为大家详细介绍了Qt如何使用QSqlDatabase连接MySQL实现增删改查功能,文中的示例代码讲解详细,感兴趣的小伙伴... 目录一、创建数据表二、连接mysql数据库三、封装成一个完整的轻量级 ORM 风格类3.1 表结构

基于Python实现一个图片拆分工具

《基于Python实现一个图片拆分工具》这篇文章主要为大家详细介绍了如何基于Python实现一个图片拆分工具,可以根据需要的行数和列数进行拆分,感兴趣的小伙伴可以跟随小编一起学习一下... 简单介绍先自己选择输入的图片,默认是输出到项目文件夹中,可以自己选择其他的文件夹,选择需要拆分的行数和列数,可以通过

Python中将嵌套列表扁平化的多种实现方法

《Python中将嵌套列表扁平化的多种实现方法》在Python编程中,我们常常会遇到需要将嵌套列表(即列表中包含列表)转换为一个一维的扁平列表的需求,本文将给大家介绍了多种实现这一目标的方法,需要的朋... 目录python中将嵌套列表扁平化的方法技术背景实现步骤1. 使用嵌套列表推导式2. 使用itert

Python使用pip工具实现包自动更新的多种方法

《Python使用pip工具实现包自动更新的多种方法》本文深入探讨了使用Python的pip工具实现包自动更新的各种方法和技术,我们将从基础概念开始,逐步介绍手动更新方法、自动化脚本编写、结合CI/C... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核