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

相关文章

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

C#使用SQLite进行大数据量高效处理的代码示例

《C#使用SQLite进行大数据量高效处理的代码示例》在软件开发中,高效处理大数据量是一个常见且具有挑战性的任务,SQLite因其零配置、嵌入式、跨平台的特性,成为许多开发者的首选数据库,本文将深入探... 目录前言准备工作数据实体核心技术批量插入:从乌龟到猎豹的蜕变分页查询:加载百万数据异步处理:拒绝界面

使用Sentinel自定义返回和实现区分来源方式

《使用Sentinel自定义返回和实现区分来源方式》:本文主要介绍使用Sentinel自定义返回和实现区分来源方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Sentinel自定义返回和实现区分来源1. 自定义错误返回2. 实现区分来源总结Sentinel自定

Springboot处理跨域的实现方式(附Demo)

《Springboot处理跨域的实现方式(附Demo)》:本文主要介绍Springboot处理跨域的实现方式(附Demo),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不... 目录Springboot处理跨域的方式1. 基本知识2. @CrossOrigin3. 全局跨域设置4.

springboot security使用jwt认证方式

《springbootsecurity使用jwt认证方式》:本文主要介绍springbootsecurity使用jwt认证方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录前言代码示例依赖定义mapper定义用户信息的实体beansecurity相关的类提供登录接口测试提供一

springboot security之前后端分离配置方式

《springbootsecurity之前后端分离配置方式》:本文主要介绍springbootsecurity之前后端分离配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的... 目录前言自定义配置认证失败自定义处理登录相关接口匿名访问前置文章总结前言spring boot secu

SpringBoot中封装Cors自动配置方式

《SpringBoot中封装Cors自动配置方式》:本文主要介绍SpringBoot中封装Cors自动配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录SpringBoot封装Cors自动配置背景实现步骤1. 创建 GlobalCorsProperties

Flutter打包APK的几种方式小结

《Flutter打包APK的几种方式小结》Flutter打包不同于RN,Flutter可以在AndroidStudio里编写Flutter代码并最终打包为APK,本篇主要阐述涉及到的几种打包方式,通... 目录前言1. android原生打包APK方式2. Flutter通过原生工程打包方式3. Futte

python+opencv处理颜色之将目标颜色转换实例代码

《python+opencv处理颜色之将目标颜色转换实例代码》OpenCV是一个的跨平台计算机视觉库,可以运行在Linux、Windows和MacOS操作系统上,:本文主要介绍python+ope... 目录下面是代码+ 效果 + 解释转HSV: 关于颜色总是要转HSV的掩膜再标注总结 目标:将红色的部分滤