konva Image 对上报不同数据格式,客户端处理方式

2024-08-29 22:04

本文主要是介绍konva Image 对上报不同数据格式,客户端处理方式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

vue3组件

<v-stageid="konva_stage":config="konvaConfig"ref="konvaStageRef"@wheel="wheelForScale($event)"><v-layer><v-group :config="group"><v-image:config="{image: pgmImage,}"/></v-group></v-layer></v-stage>


1. Blob 对象(推荐)


if (data instanceof Blob) {const url = URL.createObjectURL(data);pgmImage.value = new Image();pgmImage.value.src = url;pgmImage.value.onload = function () {console.log("pgmImage.width", pgmImage.value.naturalWidth);console.log("pgmImage.height", pgmImage.value.naturalHeight);};
}

2.base64(此种方式,读取图片内容转换成base64格式会比原图片大大概1/3)

const base64Str = imgData; // imgData 从后端接口或者websocket 上报数据
pgmImage.value = new Image();
pgmImage.value.src = base64Str;
pgmImage.value.onload = ()=> {// 获取图片参数 参与逻辑操作
}

3. pgm 这种非常不推荐pgm本身比其他图片格式大,且不能压缩处理,对图片较大情况下非常不友好!

解析接口传递过来的pgm 数据

const MapImage = function (data) {let exp = /^(\S+)\s+(\#.*?\n)*\s*(\d+)\s+(\d+)\s+(\d+)?\s*/;let match = data && data.match(exp);if (match) {let width = (this.width = parseInt(match[3], 10));let height = (this.height = parseInt(match[4], 10));let maxVal = parseInt(match[5], 10);let bytes = maxVal < 256 ? 1 : 2;let imgData = data.substr(match[0].length);switch (match[1]) {case "P1":this._parser = new ASCIIParser(maxVal + " " + imgData, bytes);this._formatter = new PBMFormatter(width, height);break;case "P2":this._parser = new ASCIIParser(imgData, bytes);this._formatter = new PGMFormatter(width, height, maxVal);break;case "P3":this._parser = new ASCIIParser(imgData, bytes);this._formatter = new PPMFormatter(width, height, maxVal);break;case "P4":this._parser = new BinaryParser(imgData, bytes);this._formatter = new PBMFormatter(width, height);break;case "P5":this._parser = new BinaryParser(imgData, bytes);this._formatter = new PGMFormatter(width, height, maxVal);break;case "P6":this._parser = new BinaryParser(data, bytes);this._formatter = new PPMFormatter(width, height, maxVal);break;default:throw new TypeError("Sorry, your file format is not supported. [" + match[1] + "]");}} else {throw new TypeError("Sorry, file does not appear to be a Netpbm file.");}
};MapImage.prototype.getInitMap = function (type = "image") {let canvas = this._formatter.getPainting(this._parser, type);return canvas;
};const BinaryParser = function (data, bytes) {this._data = data;this._bytes = bytes;this._pointer = 0;
};BinaryParser.prototype.getNextSample = function () {if (this._pointer >= this._data.length) return false;let val = 0;for (let i = 0; i < this._bytes; i++) {val = val * 255 + this._data.charCodeAt(this._pointer++);}return val;
};const ASCIIParser = function (data, bytes) {this._data = data.split(/\s+/);this._bytes = bytes;this._pointer = 0;
};ASCIIParser.prototype.getNextSample = function () {if (this._pointer >= this._data.length) return false;let val = 0;for (let i = 0; i < this._bytes; i++) {val = val * 255 + parseInt(this._data[this._pointer++], 10);}return val;
};const PPMFormatter = function (width, height, maxVal) {this._width = width;this._height = height;this._maxVal = maxVal;
};PPMFormatter.prototype.getPainting = function (parser, type) {let paintData = null;let canvas = null;let ctx = null;let img = null;if (type === "canvas") {canvas = document.createElement("canvas");ctx = canvas.getContext("2d");canvas.width = ctx.width = this._width;canvas.height = ctx.height = this._height;paintData = ctx.getImageData(0, 0, this._width, this._height);} else {paintData = new Uint8ClampedArray(this._width * this._height * 4);}let dataArr = type === "canvas" ? paintData.data : paintData;for (let row = 0; row < this._height; row++) {for (let col = 0; col < this._width; col++) {let factor = 255 / this._maxVal;let r = factor * parser.getNextSample();let g = factor * parser.getNextSample();let b = factor * parser.getNextSample();let pos = (row * this._width + col) * 4;dataArr[pos] = r;dataArr[pos + 1] = g;dataArr[pos + 2] = b;dataArr[pos + 3] = 255;}}if (type === "canvas") {ctx.putImageData(paintData, 0, 0);return canvas;} else {img = new ImageData(paintData, this._width, this._height);return img;}
};const PGMFormatter = function (width, height, maxVal) {this._width = width;this._height = height;this._maxVal = maxVal;
};PGMFormatter.prototype.getPainting = function (parser, type) {let paintData = null;let canvas = null;let ctx = null;let img = null;if (type === "canvas") {canvas = document.createElement("canvas");ctx = canvas.getContext("2d");canvas.width = ctx.width = this._width;canvas.height = ctx.height = this._height;paintData = ctx.getImageData(0, 0, this._width, this._height);} else {paintData = new Uint8ClampedArray(this._width * this._height * 4);}let dataArr = type === "canvas" ? paintData.data : paintData;for (let row = 0; row < this._height; row++) {for (let col = 0; col < this._width; col++) {let d = parser.getNextSample() * (255 / this._maxVal);let pos = (row * this._width + col) * 4;dataArr[pos] = d;dataArr[pos + 1] = d;dataArr[pos + 2] = d;dataArr[pos + 3] = 255;}}if (type === "canvas") {ctx.putImageData(paintData, 0, 0);return canvas;} else {img = new ImageData(paintData, this._width, this._height);return img;}
};const PBMFormatter = function (width, height) {this._width = width;this._height = height;
};PBMFormatter.prototype.getPainting = function (parser, type) {let paintData = null;let canvas = null;let ctx = null;let img = null;if (type === "canvas") {canvas = document.createElement("canvas");ctx = canvas.getContext("2d");canvas.width = ctx.width = this._width;canvas.height = ctx.height = this._height;paintData = ctx.getImageData(0, 0, this._width, this._height);}if (parser instanceof BinaryParser) {let data = "";let byte = null;let bytesPerLine = Math.ceil(this._width / 8);for (let i = 0; i < this._height; i++) {let line = parser._data.substr(i * bytesPerLine, bytesPerLine);let lineData = "";for (let j = 0; j < line.length; j++)lineData += ("0000000" + line.charCodeAt(j).toString(2)).substr(-8);data += lineData.substr(0, this._width);}while ((byte = parser.getNextSample()) !== false) {data += ("0000000" + byte.toString(2)).substr(-8);}parser = new ASCIIParser(data.split("").join(" "), 1);}let dataArr = type === "canvas" ? paintData.data : paintData;for (let row = 0; row < this._height; row++) {for (let col = 0; col < this._width; col++) {let d = (1 - parser.getNextSample()) * 255;let pos = (row * this._width + col) * 4;dataArr[pos] = d;dataArr[pos + 1] = d;dataArr[pos + 2] = d;dataArr[pos + 3] = 255;}}if (type === "canvas") {ctx.putImageData(paintData, 0, 0);return canvas;} else {img = new ImageData(paintData, this._width, this._height);return img;}
};export { MapImage };

vue2 使用方式:

if (pgmImageData) { //pgmImageData 接口返回let img = new MapImage(pgmImageData);let canvasImg = img.getInitMap("canvas");// canvasImg.width, canvasImg.heightthis.konvaConfig.pgmImage = new Image();this.konvaConfig.pgmImage.src = canvasImg.toDataURL();
}

这篇关于konva Image 对上报不同数据格式,客户端处理方式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

java脚本使用不同版本jdk的说明介绍

《java脚本使用不同版本jdk的说明介绍》本文介绍了在Java中执行JavaScript脚本的几种方式,包括使用ScriptEngine、Nashorn和GraalVM,ScriptEngine适用... 目录Java脚本使用不同版本jdk的说明1.使用ScriptEngine执行javascript2.

Mybatis官方生成器的使用方式

《Mybatis官方生成器的使用方式》本文详细介绍了MyBatisGenerator(MBG)的使用方法,通过实际代码示例展示了如何配置Maven插件来自动化生成MyBatis项目所需的实体类、Map... 目录1. MyBATis Generator 简介2. MyBatis Generator 的功能3

Python手搓邮件发送客户端

《Python手搓邮件发送客户端》这篇文章主要为大家详细介绍了如何使用Python手搓邮件发送客户端,支持发送邮件,附件,定时发送以及个性化邮件正文,感兴趣的可以了解下... 目录1. 简介2.主要功能2.1.邮件发送功能2.2.个性签名功能2.3.定时发送功能2. 4.附件管理2.5.配置加载功能2.6.

Go语言使用Buffer实现高性能处理字节和字符

《Go语言使用Buffer实现高性能处理字节和字符》在Go中,bytes.Buffer是一个非常高效的类型,用于处理字节数据的读写操作,本文将详细介绍一下如何使用Buffer实现高性能处理字节和... 目录1. bytes.Buffer 的基本用法1.1. 创建和初始化 Buffer1.2. 使用 Writ

Python视频处理库VidGear使用小结

《Python视频处理库VidGear使用小结》VidGear是一个高性能的Python视频处理库,本文主要介绍了Python视频处理库VidGear使用小结,文中通过示例代码介绍的非常详细,对大家的... 目录一、VidGear的安装二、VidGear的主要功能三、VidGear的使用示例四、VidGea

Python结合requests和Cheerio处理网页内容的操作步骤

《Python结合requests和Cheerio处理网页内容的操作步骤》Python因其简洁明了的语法和强大的库支持,成为了编写爬虫程序的首选语言之一,requests库是Python中用于发送HT... 目录一、前言二、环境搭建三、requests库的基本使用四、Cheerio库的基本使用五、结合req

Python数据处理之导入导出Excel数据方式

《Python数据处理之导入导出Excel数据方式》Python是Excel数据处理的绝佳工具,通过Pandas和Openpyxl等库可以实现数据的导入、导出和自动化处理,从基础的数据读取和清洗到复杂... 目录python导入导出Excel数据开启数据之旅:为什么Python是Excel数据处理的最佳拍档

SpringBoot项目启动后自动加载系统配置的多种实现方式

《SpringBoot项目启动后自动加载系统配置的多种实现方式》:本文主要介绍SpringBoot项目启动后自动加载系统配置的多种实现方式,并通过代码示例讲解的非常详细,对大家的学习或工作有一定的... 目录1. 使用 CommandLineRunner实现方式:2. 使用 ApplicationRunne

使用Python处理CSV和Excel文件的操作方法

《使用Python处理CSV和Excel文件的操作方法》在数据分析、自动化和日常开发中,CSV和Excel文件是非常常见的数据存储格式,ython提供了强大的工具来读取、编辑和保存这两种文件,满足从基... 目录1. CSV 文件概述和处理方法1.1 CSV 文件格式的基本介绍1.2 使用 python 内

VUE动态绑定class类的三种常用方式及适用场景详解

《VUE动态绑定class类的三种常用方式及适用场景详解》文章介绍了在实际开发中动态绑定class的三种常见情况及其解决方案,包括根据不同的返回值渲染不同的class样式、给模块添加基础样式以及根据设... 目录前言1.动态选择class样式(对象添加:情景一)2.动态添加一个class样式(字符串添加:情