VX小程序 实现区域转图片预览

图例

方法一

1、安装插件 wxml2canvas

npm install --save wxml2canvas

git地址参照:https://github.com/wg-front/wxml2canvas

2、类型

// 小程序页面
let data={
	list:[{
		type:'wxml',
		class:'.test_center .draw_canvas',
		limit:'.test_center',
		x:0,
		y:0
	}]
}

3、数据结构

​
let testData=[
		{
			PageIndex:1,
			ImageUrl:"https://minio.23544.com:9010/mk-sys-test/origin-paper/432531489095806/2023-06-26-15-42-38/9d43c15bb1834cacb0a8900b5e45dfb2/1.jpg",
			Height:1680.0,
			Width:1200.0,
			Questions:[
				{"QuestionNum":"1-2","X":140.00000450195313,"Y":515.989620895996,"Width":149.77778,"Height":54.77778,"score":1.00,"totalScore":2.00},
				{"QuestionNum":"1111","X":113.10765335144043,"Y":756.3195006567382,"Width":799.77778,"Height":199.77778,"score":0.0,"totalScore":0.0},
				{"QuestionNum":"2222","X":100.0000025946045,"Y":822.2222792822265,"Width":799.77778,"Height":199.77778,"score":0.0,"totalScore":0.0},
				{"QuestionNum":"12","X":95.9895980078125,"Y":1260.989620895996,"Width":1022.77778,"Height":305.77778,"score":1.00,"totalScore":14.00},
			]
		},
		{
			PageIndex:1,
			ImageUrl:"https://minio.23544.com:9010/mk-sys-test/origin-paper/432531489095806/2023-06-26-15-42-38/9d43c15bb1834cacb0a8900b5e45dfb2/1.jpg",
			Height:1680.0,
			Width:1200.0,
			Questions:[
				{"QuestionNum":"1-2","X":140.00000450195313,"Y":515.989620895996,"Width":149.77778,"Height":54.77778,"score":1.00,"totalScore":2.00},
				{"QuestionNum":"1111","X":113.10765335144043,"Y":756.3195006567382,"Width":799.77778,"Height":199.77778,"score":0.0,"totalScore":0.0},
				{"QuestionNum":"2222","X":100.0000025946045,"Y":822.2222792822265,"Width":799.77778,"Height":199.77778,"score":0.0,"totalScore":0.0},
				{"QuestionNum":"12","X":95.9895980078125,"Y":1260.989620895996,"Width":1022.77778,"Height":305.77778,"score":1.00,"totalScore":14.00},
			]
		}
	]

​

4、页面引用

<template>
    <canvas canvas-id="canvas1" class="test_center"></canvas>
</template>

<script setup>
import Wxml2Canvas from 'wxml2canvas';
const onclicks=()=> {
	person.drawImage = new Wxml2Canvas({
		width: 340,
		height: 210,
		element: 'canvas1',
		background: '#f0f0f0',
		finish(url) {
			console.log(7777,url)
			uni.previewImage({
				// 需要预览的图片链接列表
				urls: [url],
				// 为当前显示图片的链接/索引值
				current: url,
				// 图片指示器样式	
				indicator:'default',
				// 是否可循环预览
				loop:false
			});
		},
		error (res) {
		}
	});
	let data = {
		list: [
			{
			   type: 'image',
			   x: 0,
			   y: 0,
			   url: person.itemExam.ImageUrl,
			   style: {
				   width: person.itemExam.Width*person.scaleWidth,
				   height: person.itemExam.Height*person.scaleWidth,
				   border: '0 solid #aaaaaa',
				   boxShadow: '10 20 20 rgba(0, 0, 0, 0.4)'
			   }
		    }
		]
	}
	person.itemExam.Questions.forEach((quest,itIndex)=>{
		data.list.push({
			type: 'text',
			text: quest.score,
			x: quest.X*person.scaleWidth,
			y: quest.Y*person.scaleWidth,
			style: {
				textAlign: 'right',
				width: quest.Width*person.scaleWidth-40,
				height: quest.Height*person.scaleWidth-40,
				fontSize: 16,
				color: 'red',
				border: '0px solid red',
				padding: '6px 40px 0 0'
			}
		})
		data.list.push({
			type: 'text',
			text: '  /'+quest.totalScore,
			x: quest.X*person.scaleWidth,
			y: quest.Y*person.scaleWidth,
			style: {
				textAlign: 'right',
				width: quest.Width*person.scaleWidth-6,
				height: quest.Height*person.scaleWidth-6,
				fontSize: 16,
				color: 'blue',
				border: '1px solid red',
				padding: '6px 6px 0 0'
			}
		})
	})
	
	person.drawImage.draw(data);
}
</script>

方法二

1、使用canvas画图组件

有坑:uni.getImageInfo 方法转出的地址是http 不是https,而 uni.previewImage 识别 https的,否则图片会出不来

<template>
<canvas v-if="person.isShowExam" @click="saveFile" class="test_center" canvas-id="postCanvars" :width="person.itemExam.Width*person.scaleWidth" :height="person.itemExam.Height*person.scaleWidth"></canvas>
<view v-else @click="saveFile" class="test_center" :style="{width:person.itemExam.Width*person.scaleWidth+'px;',height:person.itemExam.Height*person.scaleWidth+'px;'}">
	<image class="draw_canvas" data-type="image" :src="person.itemExam.ImageUrl" mode=""></image>
	<view class="test_center_page draw_canvas">
		<view class="page_item flex draw_canvas" v-for="(quest,quIndex) in person.itemExam.Questions" :key="quIndex"
		:style="{ 
		top:quest.Y*person.scaleWidth +'px;',
		left:quest.X*person.scaleWidth+'px;',
		width:quest.Width*person.scaleWidth+'px;',
		height:quest.Height*person.scaleWidth+'px;'}">
		   <view class="text_flex flex draw_canvas">
			   <text class="color_red draw_canvas">{{quest.score}}</text>/
			   <text class="color_blue draw_canvas">{{quest.totalScore}}</text>
		   </view>
		</view>
	</view>
</view>

</template>
<script setup>
import { reactive, ref, watch } from 'vue'
import { onLoad, onShow } from "@dcloudio/uni-app";
const person=reactive({
	itemExam:{
		PageIndex:1,
		ImageUrl:"https://minio.23544.com:9010/mk-sys-test/origin-paper/432531489095806/2023-06-26-15-42-38/9d43c15bb1834cacb0a8900b5e45dfb2/1.jpg",
		Height:1680.0,
		Width:1200.0,
		Questions:[
			{"QuestionNum":"1-2","X":140.00000450195313,"Y":515.989620895996,"Width":149.77778,"Height":54.77778,"score":1.00,"totalScore":2.00},
			{"QuestionNum":"1111","X":113.10765335144043,"Y":756.3195006567382,"Width":799.77778,"Height":199.77778,"score":0.0,"totalScore":0.0},
			{"QuestionNum":"2222","X":100.0000025946045,"Y":822.2222792822265,"Width":799.77778,"Height":199.77778,"score":0.0,"totalScore":0.0},
			{"QuestionNum":"12","X":95.9895980078125,"Y":1260.989620895996,"Width":1022.77778,"Height":305.77778,"score":1.00,"totalScore":14.00},
		]
	},
	scaleWidth:0,
	scaleHeight:0,
	sharePoster:'',
	isShowExam:false
})

onLoad(()=>{
	// 获取屏幕宽
	uni.getSystemInfo({
		success:(res)=> {
			person.scaleWidth = res.windowWidth/person.itemExam.Width
			person.scaleHeight = res.windowHeight/person.itemExam.Height
		}
	})
}) 
// 保存照片
const saveFile=()=>{
	person.isShowExam=true
	uni.getImageInfo({
		src: person.itemExam.ImageUrl,
		success(res) {
			let context = uni.createCanvasContext('postCanvars')
			
			// 绘制图片
			context.drawImage(res.path, 0, 0, person.itemExam.Width*person.scaleWidth, person.itemExam.Height*person.scaleWidth)
			context.stroke()
			
			person.itemExam.Questions.forEach((quest,itIndex)=>{
				// 绘制矩形
				context.setStrokeStyle('red')
				context.rect(quest.X*person.scaleWidth, quest.Y*person.scaleWidth, quest.Width*person.scaleWidth, quest.Height*person.scaleWidth)
				context.textAlign ='right'
				context.stroke()
				// 红色文字
				context.setFontSize(16)
				context.setFillStyle('red')
				context.fillText(quest.score+'', quest.X*person.scaleWidth+24, quest.Y*person.scaleWidth+20)
				context.textAlign ='right'
				// 分割斜线
				context.setFillStyle('#000')
				context.fillText('/', quest.X*person.scaleWidth+32, quest.Y*person.scaleWidth+20)
				// 蓝色文字
				context.setFillStyle('blue')
				context.fillText(quest.totalScore, quest.X*person.scaleWidth+50, quest.Y*person.scaleWidth+20)
				context.textAlign='right'
				context.stroke()
			
				return context
			})
			
			// 生成照片保存
			context.draw(true, () => {
				uni.canvasToTempFilePath({
					canvasId: 'postCanvars',
					success: (res) => {
						uni.previewImage({
							// 需要预览的图片链接列表
							urls: [res.tempFilePath],
							// 为当前显示图片的链接/索引值
							current: res.tempFilePath,
							// 图片指示器样式	
							indicator:'default',
							// 是否可循环预览
							loop:false
						});
					},
					fail: (res) => {
						uni.showToast({
							title:'生成照片失败'
						})
					} 
				})
			})
		}
	})
}

</script>

<style lang="scss" scoped>
.test_center{
	width: 99%;
	height: calc(100vh - 310rpx);
	position: relative;
	overflow: hidden;
	image{
		width: 100%;
		height: 100%;
	}
	.page_item{
		position: absolute;
		border: 2rpx solid red;
		.text_flex{
			position: absolute;
			right: 20rpx;
			top: 0;
			text{
				font-size: 36rpx;
				font-weight: 600;
			}
		}
	}
}
.color_red{
	color: #FF2929;
}
.color_blue{
	color: #6B86FF;
}
</style>

     希望我的愚见能够帮助你哦~,若有不足之处,还望指出,你们有更好的解决方法,欢迎大家在评论区下方留言支持,大家一起相互学习参考呀~

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/91765.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

熊猫:完整的初学者指南

pandas&#xff1a;完整的初学者指南 一、说明 在你的Python开发人员或数据科学之旅中&#xff0c;你可能已经多次遇到“熊猫”这个词&#xff0c;但仍然需要弄清楚它的作用。以及数据和熊猫之间的关系。所以让我向你解释一下。 根据最新估计&#xff0c;每天创建 328.77 亿 TB…

36k字从Attention讲解Transformer及其在Vision中的应用(pytorch版)

文章目录 0.卷积操作1.注意力1.1 注意力概述(Attention)1.1.1 Encoder-Decoder1.1.2 查询、键和值1.1.3 注意力汇聚: Nadaraya-Watson 核回归1.2 注意力评分函数1.2.1 加性注意力1.2.2 缩放点积注意力1.3 自注意力(Self-Attention)1.3.1 自注意力的定义和计算1.3.2 自注意…

python爬虫11:实战3

python爬虫11&#xff1a;实战3 前言 ​ python实现网络爬虫非常简单&#xff0c;只需要掌握一定的基础知识和一定的库使用技巧即可。本系列目标旨在梳理相关知识点&#xff0c;方便以后复习。 申明 ​ 本系列所涉及的代码仅用于个人研究与讨论&#xff0c;并不会对网站产生不好…

nonlocal关键字声明

nonlocal关键字声明 作用 使得内层函数可以使用/修改外层函数的变量 值得注意的是&#xff0c;在未使用nonlocal声明时 对于外层函数中的可变对象&#xff0c;内层函数即可访问&#xff0c;也可以修改 def outer():x, y [1], [2]def inner(z):x.append(1)print(x)print(z)r…

历史最佳二季度表现后,爱奇艺想为用户提供更多价值

以爱奇艺为首&#xff0c;随着长视频平台相继转变运营思路&#xff0c;走向盈利目标&#xff0c;最早完成蜕变的爱奇艺&#xff0c;已开始迈向下一阶段。 近日&#xff0c;爱奇艺发布了截至6月30日的2023年第二季度财报。除了依然亮眼的内容表现、业绩成果外&#xff0c;爱奇艺…

1.Flink源码编译

目录 1.环境版本 1.1 jdk 1.2.maven 1.3.node 1.4.scala 2.下载flink源码 3.编译源码 4.idea打开flink源码 5.运行wordcount 1.环境版本 软件地址 链接&#xff1a;https://pan.baidu.com/s/1ZxYydR8rBfpLCcIdaOzxVg 提取码&#xff1a;12xq 1.1 jdk 1.2 maven 1.…

Bean 作用域和生命周期

前言&#xff1a; &#x1f4d5;作者简介&#xff1a;热爱编程的小七&#xff0c;致力于C、Java、Python等多编程语言&#xff0c;热爱编程和长板的运动少年&#xff01; &#x1f4d8;相关专栏Java基础语法&#xff0c;JavaEE初阶&#xff0c;数据库&#xff0c;数据结构和算法…

GPT4模型架构的泄漏与分析

迄今为止&#xff0c;GPT4 模型是突破性的模型&#xff0c;可以免费或通过其商业门户&#xff08;供公开测试版使用&#xff09;向公众提供。它为许多企业家激发了新的项目想法和用例&#xff0c;但对参数数量和模型的保密却扼杀了所有押注于第一个 1 万亿参数模型到 100 万亿参…

【Mac】编译Spring 源码和Idea导入

今天我们开始Spring源码的阅读之旅。阅读Spring的源码的第一步当然是编译Spring源码。首先我们要去GitHub上将spring源码给clone下来。 笔者编译环境如下&#xff1a; Spring版本&#xff1a;5.28 https://github.com/spring-projects/spring-framework/tree/v5.2.8.RELEASE …

LoadRunner操作教程

日升时奋斗&#xff0c;日落时自省 目录 1、Virtual User Generator &#xff08;VUG&#xff09; 1.1、WebTours系统 1.1.1、WebTours启动 1.1.2、WebTours配置 1.2、脚本录制 1.3、编译 1.4、脚本运行 1.5、加强脚本 1.5.1、事务插入 1.5.2、插入集合点 1.5.3、参…

【C++ 学习 ⑰】- 继承(下)

目录 一、派生类的默认成员函数 二、继承与友元 三、继承与静态成员 四、复杂的菱形继承及菱形虚拟继承 五、继承和组合 一、派生类的默认成员函数 派生类的构造函数必须调用基类的构造函数初始化基类的那一部分成员。如果基类没有默认构造函数&#xff0c;那么必须在派生…

Python基础学习第一天:关于Python的简单介绍

前言 最近一批批大一新生都要开始踏入校园了&#xff0c;计算机专业 emmm…如果有需要学习python的&#xff0c;尤其是还没开学的&#xff0c;确实可以开始找找资料看看python了&#xff0c;如果是自己本来就对python感兴趣&#xff0c;更应该需要看看了&#xff0c;毕竟学校到…

阿里云 Serverless 应用引擎 2.0,正式公测!

阿里云 Serverless 应用引擎 SAE2.0 正式公测上线&#xff01;全面升级后的 SAE2.0 具备极简体验、标准开放、极致弹性三大优势&#xff0c;应用冷启动全面提效&#xff0c;秒级完成创建发布应用&#xff0c;应用成本下降 40% 以上。 此外&#xff0c;阿里云还带来容器服务 Se…

无涯教程-聚类算法 - Mean-Shift

如前所述&#xff0c;它是在无监督学习中使用的另一种强大的聚类算法&#xff0c;与K均值聚类不同&#xff0c;它不做任何假设&#xff0c;因此&#xff0c;它是一种非参数算法。 均值平移算法基本上是通过将数据点移向最高密度的数据点(即群集质心)来迭代地将数据点分配给群集…

【日常积累】Linux中vi/vim的使用

概述 vim是由vi发展演变过来的文本编辑器&#xff0c;因其具有语法高亮显示、多视窗编辑、代码折叠、支持插件等功能&#xff0c;由于其功能相比vi来说更加强大&#xff0c;所以在实际工作中的使用更加广泛。 vim工作模式 Vim具有多种工作模式&#xff0c;常用的工作模式有&…

去除wps段落柄,删除空白页

如图&#xff0c;有一个段落柄在左端&#xff0c;无法删除&#xff0c;只能编辑。 导致本来是8页内容&#xff0c;现在是9页&#xff0c;多了一空白页 后面新建一个空白页&#xff0c;发现默认会自带一个段落柄&#xff0c;所以有可能这个段落柄是不能消除的&#xff0c;那么如…

【LeetCode-面试经典150题-day15】

目录 104.二叉树的最大深度 100.相同的树 226.翻转二叉树 101.对称二叉树 105.从前序与中序遍历序列构造二叉树 106.从中序与后序遍历序列构造二叉树 117.填充每个节点的下一个右侧节点指针Ⅱ 104.二叉树的最大深度 题意&#xff1a; 给定一个二叉树 root &#xff0c;返回其…

智能井盖传感器,物联网智能井盖系统

随着城市人口的不断增加和城市化进程的不断推进&#xff0c;城市基础设施的安全和可靠性变得愈发重要&#xff0c;城市窨井盖作为城市基础设施重要组成部分之一&#xff0c;其安全性事关城市安全有序运行和居民生产生活安全保障。 近年来&#xff0c;各地都在加强城市窨井盖治理…

【C/C++】多态的概念 | 虚函数 | 虚函数指针

创作不易&#xff0c;本篇文章如果帮助到了你&#xff0c;还请点赞 关注支持一下♡>&#x16966;<)!! 主页专栏有更多知识&#xff0c;如有疑问欢迎大家指正讨论&#xff0c;共同进步&#xff01; &#x1f525;c系列专栏&#xff1a;C/C零基础到精通 &#x1f525; 给大…

STM32 BOOT 启动配置 ISP升级 介绍

启动配置 在STM32F10xxx里&#xff0c;可以通过BOOT[1:0]引脚选择三种不同启动模式。 启动模式选择引脚启动模式说明BOOT1BOOT0X0主闪存存储器主闪存存储器被选为启动区域01系统存储器系统存储器被选为启动区域11内置SRAM内置SRAM被选为启动区域 在系统复位后&#xff0c; S…