获取png的元信息

这段代码定义了一个JavaScript函数,通过FileReader读取PNG文件,检查其元数据,特别是tEXt、comf和iTXt类型的文本数据。

通过读取字节数据判断

export function getPngMetadata(file) {
	return new Promise((r) => {
		const reader = new FileReader();
		reader.onload = (event) => {
			// Get the PNG data as a Uint8Array
			const pngData = new Uint8Array(event.target.result);
			const dataView = new DataView(pngData.buffer);

			// Check that the PNG signature is present
			if (dataView.getUint32(0) !== 0x89504e47) {
				console.error("Not a valid PNG file");
				r();
				return;
			}

			// Start searching for chunks after the PNG signature
			let offset = 8;
			let txt_chunks = {};
			// Loop through the chunks in the PNG file
			while (offset < pngData.length) {
				// Get the length of the chunk
				const length = dataView.getUint32(offset);
				// Get the chunk type
				const type = String.fromCharCode(...pngData.slice(offset + 4, offset + 8));
				if (type === "tEXt" || type == "comf" || type === "iTXt") {
					// Get the keyword
					let keyword_end = offset + 8;
					while (pngData[keyword_end] !== 0) {
						keyword_end++;
					}
					const keyword = String.fromCharCode(...pngData.slice(offset + 8, keyword_end));
					// Get the text
					const contentArraySegment = pngData.slice(keyword_end + 1, offset + 8 + length);
					const contentJson = new TextDecoder("utf-8").decode(contentArraySegment);
					txt_chunks[keyword] = contentJson;
				}

				offset += 12 + length;
			}

			r(txt_chunks);
		};

		reader.readAsArrayBuffer(file);
	});
}
获取PNG图像中的附加信息,可以使用JavaScript中的Canvas API和ImageData对象。PNG图像中的附加信息存储在tEXt块中,每个块有一个关键字和一个值。 下面是一个简单的JavaScript代码示例,演示如何获取PNG图像中的附加信息: ```javascript // 创建一个Image对象 var img = new Image(); img.onload = function() { // 创建一个Canvas对象 var canvas = document.createElement('canvas'); canvas.width = img.width; canvas.height = img.height; var ctx = canvas.getContext('2d'); // 将图像绘制到Canvas上 ctx.drawImage(img, 0, 0, img.width, img.height); // 获取ImageData对象 var imageData = ctx.getImageData(0, 0, img.width, img.height); // 获取PNG元数据 var pngMeta = imageData.data.slice(-1024); // 查找tEXt关键字 var keyword = 'mykeyword'; var idx = -1; for (var i = 0; i < pngMeta.length; i++) { if (pngMeta[i] === 116 && pngMeta[i+1] === 69 && pngMeta[i+2] === 88 && pngMeta[i+3] === 116) { var len = pngMeta[i-4] * 0x1000000 + pngMeta[i-3] * 0x10000 + pngMeta[i-2] * 0x100 + pngMeta[i-1]; if (pngMeta.slice(i+4, i+4+len).toString().indexOf(keyword) !== -1) { idx = i+4; break; } } } // 获取tEXt值 if (idx !== -1) { var value = ''; while (pngMeta[idx] !== 0) { value += String.fromCharCode(pngMeta[idx]); idx++; } console.log('tEXt value: ' + value); } else { console.log('tEXt keyword not found'); } }; img.src = 'image.png'; ``` 在上面的代码中,我们首先创建一个Image对象,然后在其onload回调中创建一个Canvas对象,并将图像绘制到Canvas上。接下来,我们使用Canvas的getImageData方法获取ImageData对象,然后从图像数据的末尾获取PNG元数据。我们查找tEXt关键字,并获取其值。 请注意,上面的代码假定tEXt值以Null字符(ASCII码为0)结尾。如果你的PNG图像中的tEXt值不以Null字符结尾,则需要使用其他方法来获取tEXt值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

isyoungboy

给钱是不可能的

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值